Angular4 +单元测试中没有打开ngx-bootstrap引导程序菜单

时间:2018-05-01 18:02:47

标签: angular drop-down-menu ngx-bootstrap

下拉菜单在应用程序中打开正常,但在单元测试中不起作用。我不确定我是否遗漏了一些东西,但我已经看了很长时间了,我找不到任何用户错误。我还在github上提交了一个问题。 https://github.com/valor-software/ngx-bootstrap/issues/4282

这是stackblitz链接:https://stackblitz.com/edit/angular-euyvq4-khflez?file=src%2Fmain.ts

运行应用

// bootstrap(); // to run karma
platformBrowserDynamic().bootstrapModule(AppModule); // to run the app

运行业力

bootstrap(); // to run karma
// platformBrowserDynamic().bootstrapModule(AppModule); // to run the app

任何人都可以确认是否有错误或用户错误?提前致谢

2 个答案:

答案 0 :(得分:0)

我认为你遇到了问题,因为事件循环如何在DOM节点上手动执行click方法。

杰克阿奇博尔德(谷歌的开发者倡导者)在JSConf Asia 2018上发表了关于事件循环的精彩演讲。我强烈建议观看整个事情,但我认为你在29:57 he talks about the issue。基本上,由于您手动触发此单击事件,因此您需要将测试断言作为事件循环上的单独任务执行。

您可以在测试中使用setTimeout(...)(并使用done方法)完成此操作,或者在使用fixture.whenStable()方法的Angular测试中执行此操作的首选方法。更改此项将允许您的测试通过:

fit('should open dropdown menu', () => {
  const h2: HTMLElement = fixture.nativeElement.querySelector('button#button-basic');
  h2.click();

  fixture.whenStable().then(() => {
    fixture.detectChanges();
    expect(fixture.nativeElement.querySelectorAll('li a.dropdown-item').length).toEqual(4);
  });
});

答案 1 :(得分:0)

我无法让您的Stackblitz正常工作,以防万一其他人遇到此问题,如果您动态生成了列表项(使用ngFor),则需要在tick()之后添加另一个detectChanges()< / p>

def ExceltoCSV(excel_file, csv_file_base_path):
    workbook = xlrd.open_workbook(excel_file)

    ## get the worksheet names
    for sheet_name in workbook.sheet_names():
        print('processing - ' + sheet_name)

        ## extract the data from each worksheet
        worksheet = workbook.sheet_by_name(sheet_name)

        ## create a new csv file, with the name being the original Excel worksheet name; tidied up a bit replacing spaces and dashes
        csv_file_full_path = csv_file_base_path + sheet_name.lower().replace(" - ", "_").replace(" ","_") + '.csv'
        csvfile = open(csv_file_full_path, 'w')

        ## write into the new csv file, one row at a time
        writetocsv = csv.writer(csvfile, quoting = csv.QUOTE_ALL)

        for rownum in range(worksheet.nrows):
            writetocsv.writerow(
                list(x.encode('utf-8') if type(x) == type(u'') else x for x in worksheet.row_values(rownum)
            )
        )

        csvfile.close()
        print(sheet_name + ' has been saved at - ' + csv_file_full_path)

## Paths as strings
p = r'//Network/TestingFolder/'
nf_p = r'//Network/TestingFolder/CSV_Only/'

## directory reference
directory = r'//Network/TestingFolder/' # for os.listdir() function below
file_list = []

## for iterating over directory and spitting out the paths for each file { to be used in conjunction 
with ExceltoCSV() }
for filename in os.listdir(directory):
    if filename.endswith(".xlsx"):  # or filename.endswith(".csv")
        file_path = os.path.join(directory, filename)
        file_list.append(file_path)
    else:
        continue

for paths in file_list:
    print(paths)
    ExceltoCSV(paths, nf_p)