vicgoyso
vicgoyso

Reputation: 616

Testing a service method in component method, jest

Im new to jest, however wrote a unit test how one would write the test in jasmine...

      it('should run onOpenChannelModal correctly', fakeAsync(() => {
    const extractChannelDataSpy = jest.spyOn(channelService, 'extractChannelData');

    expect(component.showFullChannelList).toEqual(false);
    component.onOpenChannelModal(selectedChannelData);
    fixture.detectChanges();
    expect(extractChannelDataSpy).toHaveBeenCalled(); // <= fails here
    expect(component.showFullChannelList).toEqual(true);
  }));

However fails on expect(extractChannelDataSpy).toHaveBeenCalled();

Could someone inform me where I am going wrong?

Here is the component method:

  onOpenChannelModal($event: any) {
    this.selectedChannel = $event;
    this.selectedChannelMedia = this.channelService.extractChannelData(this.selectedChannel);
    this.showFullChannelList = true;
  }

Upvotes: 2

Views: 1905

Answers (1)

Chaka15
Chaka15

Reputation: 1361

You can try something like this:

  it('should run onOpenChannelModal correctly', () => {
    spyOn(channelService, 'extractChannelData');

    expect(component.showFullChannelList).toEqual(false);
    component.onOpenChannelModal(selectedChannelData);
  
    expect(channelService.extractChannelData).toHaveBeenCalled();
    expect(component.showFullChannelList).toEqual(true);
  });

Upvotes: 2

Related Questions