Search code examples
angularunit-testingjasminengrx-store

How can I mock data for this.getResults$.subscribe in angular for unit test cases?


displayMesssage(){
this.getResults$.subscribe(data =>{
if(data !== undefined && data !== null){
 this.message = data.message;
}else{
 this.message="Empty";
});
}

How can I mock data for the above use case in jasmine?


Solution

  • Something like this, it may work. If this doesn't work, I need to see the full spec and full component.

    import { of } from 'rxjs';
    ....
    it('should set message to data.message when not null or undefined', () => {
      component.getResults$ = of({ message: 'hello world' });
      component.displayMessage();
      expect(component.message).toBe('hello world');
    });
    
    it('should set message to "Empty" if getResults$ is falsy', () => {
      component.getResults$ = of(null);
      component.displayMessage();
      expect(component.message).toBe('Empty');
    });