Jose Raul Perera
Jose Raul Perera

Reputation: 878

Angular Test error: Cannot read property 'pipe' of undefined

I'm having the following test error:

✖ should create Chrome 81.0.4044 (Mac OS X 10.14.6) TypeError: Cannot read property 'pipe' of undefined at AccountsInfoComponent.loadCompanyTypes (webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:75:13) at AccountsInfoComponent.initAccountsInfoForm (webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:129:14) at AccountsInfoComponent.updateContactAccounts (webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:89:22) at AccountsInfoComponent.set [as contact] (webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:47:18) at UserContext.eval (webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.spec.ts?:120:27)

I have a service with this method:

getCompanyType(contact_id: string) {
    const url = `api/v1/contact/sponsors_list?contact_id=${contact_id}`
    return this.http.get<any>(url)
  }

And a component with this method:

loadCompanyTypes() {
    this.editService
      .getCompanyType(this.contact.id)
      .pipe(untilDestroyed(this))
      .subscribe(classic_where_held => {
        const arrayOfTypes = classic_where_held.where_held_classic_id
        this.companyTypes = arrayOfTypes.map(x => {
          return { classic_id: x[0], name: x[1] }
        })
      })
  }

I mocked my service method in the tests:

const mockContactEditService = {
    addEditSection: jasmine.createSpy('addEditSection'),
    setSaveUpdateContact: jasmine.createSpy('addEditSection'),
    getCompanyType: jasmine.createSpy('getCompanyType')
  }

The tests break here .pipe(untilDestroyed(this)) it says getCompanyType doesn't return nothing and it should return an Observable???

Upvotes: 2

Views: 5560

Answers (1)

Jose Raul Perera
Jose Raul Perera

Reputation: 878

I found the problem, I needed change my mock in this way:

getCompanyType: jasmine.createSpy('getCompanyType').and.returnValue(of(mockCompanyTypes))

This solve the problem!

Upvotes: 4

Related Questions