2

I am trying to create cover all the line (Jasmine / Karma) but I am getting the error as Cannot read property 'search' of undefined

Here is my code for component code.

public search() {
  if (this.searchCompany.length) {
    let term = this.searchCompany;
    this.tempList = this.tempNameList.filter(tag => {
      if (tag.companyName.toLowerCase().indexOf(term.toLowerCase()) > -1) {
        return tag;
      }
    });
  } else {
    this.resetCompanies();
  }
}

Here is the below code for spec which I tried:

it('should search the data', () => {
  component.search;
  expect(component.search()).toBeUndefined();
});

What I am doing wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mr.M
  • 1,472
  • 3
  • 29
  • 76
  • What are you trying to achieve? And where is test config? And why `component.search`? You are not even calling the method properly like `component.search()`. – Amit Chigadani Aug 28 '18 at 19:18
  • Thanks for replying I am tying to cover entire code coverage in third line i have expect where I am calling component.search() ... – Mr.M Aug 28 '18 at 19:22
  • Can you show your test config? – Amit Chigadani Aug 28 '18 at 19:29
  • https://stackoverflow.com/questions/73049405/angular-jasmine-karma-error-typeerror-cannot-read-properties-of-undefined-read ....Please have a look – dhS Jul 20 '22 at 09:50

1 Answers1

3

Since your search method has if statement - we can write at least two unit tests.

This one is for case when there is no search tag - we expect that resetCompanies will be called if we have empty searchCompany:

  it('should resetCompanies if search is empty', () => {
    component.searchCompany = '';
    spyOn(component, 'resetCompanies').and.callFake(() => null);

    component.search();

    expect(component.resetCompanies).toHaveBeenCalled();
  });

And this one is for case when we have search tag and search works - we expect that tempList array will eventually consists of one item { companyName: 'test' }, since our search tag test matches the condition in the filter logic:

  it('should search company', () => {
    component.searchCompany = 'test';
    component.tempList = [];
    component.tempNameList = [
      { companyName: 'abc' },
      { companyName: 'test' },
      { companyName: 'def' },
    ];

    component.search();

    expect(component.tempList).toEqual([{ companyName: 'test' }]);
  });
shohrukh
  • 2,989
  • 3
  • 23
  • 38
  • Also created a [stackblitz demo](https://stackblitz.com/edit/angular6-testing-sherlock-6zdcqx?file=app%2Fhello.component.spec.ts), you check it as well – shohrukh Aug 28 '18 at 19:23
  • Thank you so much @Sherlock one question so for each and every if condition i have to create SpyOn – Mr.M Aug 28 '18 at 19:31
  • in this case - you can use `beforeEach` jasmine function, where you can initialize your test configs before each test. – shohrukh Aug 28 '18 at 19:33
  • I've updated the [stackblitz example](https://stackblitz.com/edit/angular6-testing-sherlock-6zdcqx?file=app%2Fhello.component.spec.ts) and you can see the usage example of `beforeEach` – shohrukh Aug 28 '18 at 19:35
  • @shohrukh please look at this https://stackoverflow.com/questions/73049405/angular-jasmine-karma-error-typeerror-cannot-read-properties-of-undefined-read – dhS Jul 20 '22 at 09:50