lundi 28 octobre 2019

Angular Shallow unit test with subscribe

I have inter component communication done via a service with RxJS subscriber.

Component that subscribes to this is configured as below:

export class MyComponent implements OnInit, OnDestroy {
  constructor(
    private myService: myService,
    private datePipe: DatePipe
  ) {}

  ngOnInit(): void {
    this.mySubscription = this.myservice
      .getSubject()
      .subscribe(() => {
          //DO..SOMEYHING
          })
   }

 }

We are using shallow for testing, and myservice's subscription's next event is called by another component. How can we test above code block, as we are mocking service inside test.

  beforeEach(async () => {
    shallow = new Shallow(MyComponent, MyModule)
      .mock(MyService, {

        getCount: () => of(25)
      })
  });

There is no way for us to send myservice.setSubject in test which would have triggered code in ngInit section.

Aucun commentaire:

Enregistrer un commentaire