简体   繁体   English

茉莉花触发搜索成员函数,但无法获得任何响应或错误

[英]Jasmine trigger the search member function but can't get any response or error

I am new to Jasmine and I am testing via UI so after entering the values in the search input field and triggering keyup event the search function is called which I confirmed by logging at different points but the API call is not triggered which is inside the search function. 我是Jasmine的新手,正在通过UI进行测试,因此在搜索输入字段中输入值并触发keyup事件后,将调用搜索功能,该功能已通过在不同点登录进行确认,但未触发搜索内的API调用功能。 Do I have to use await or something like that? 我是否必须使用await或类似的东西?

dashboard.spec file dashboard.spec文件

  beforeEach(() => {
    inputElement = fixture.nativeElement.querySelector('input[name=search]');
    inputElement.focus()
    inputElement.value = '131420'
    inputElement.dispatchEvent(new Event('input'));;
    fixture.detectChanges();
    keyUp().then(async () => {
      expect(component.searchText).toEqual(inputElement.value)
    });
  });

  it('should update the input', () => {
    fixture.detectChanges();
    console.log(component, 'component')
  })

  function keyUp() {
    inputElement.dispatchEvent(new KeyboardEvent('keyup', { 'key': 'Space' }));
    fixture.detectChanges();
    inputElement.blur();
    return fixture.whenStable();
  }

search function that called on triggering keyup event from UI: 从UI触发触发keyup事件的搜索功能:

 search(event) {
    if (event) {
      console.log(event, 'event');
    }
    vm.tabHeight = 385;
    vm.selectedContract = 0;
    vm.member = {};
    vm.finance = null;
    vm.accountId = null;
    vm.searchLoading = true;
    var searchText = vm.searchText;
    console.log(searchText, 'searchText')
    if (searchText) {
      searchText = searchText.replace(/\(/g, '');
      searchText = searchText.replace(/\)/g, '');
      searchText = searchText.replace(/\-/g, '');
      searchText = searchText.replace(/\&/g, '');
      console.log('here')
      vm.dashboardService.search(searchText)
        .subscribe(res => {
          console.log('got search response', res);
          vm.searchResult = res.hits;
          vm.searchLoading = false;
        }, err => {
          console.error('got search error', err);
          vm.searchLoading = false;
        });
    } else {
      vm.searchLoading = false;
    }
  }

You are doing asynchronous stuff in beforeEach . 您正在beforeEach中进行异步beforeEach To do so, there is a callback function you should use which will be invoked when all async work has been completed : 为此,您应该使用一个回调函数,当所有异步工作都完成后,该函数将被调用:

beforeEach((done) => {
  promise.then(() => {
     done();
  });
}

Also, you probably should'nt write assertions (expect) in a setup function (beforeEach) but in your test case (it). 另外,您可能不应该在设置函数(beforeEach)中而是在测试用例(it)中写断言(expect)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM