0

I'm pretty new to jasmine testing, and have no idea how to work with observables and stuff like '.subscribe' etc. Need some help with workaround for a snippet like:

getOrder(partId: number, projectId: number) {
if (projectId != null || undefined) {
  this.getOrdersForPartwithObservable(partId, projectId).subscribe(
    (order) => {
      if (
        (order as ordersView[]).length != 0 &&
        order != (undefined || null)
      ) {
        this.ordersHeader = Object.keys(order[0]);
        this.parts.find(
          (x) => x.partId === partId && x.projectId === projectId
        ).orders = order as ordersView[];

        // console.log(this.parts);
      } else {
        this.notififyService.show(
          'No Orders are associated for this part',
          'danger'
        );
      }
    },
    (error) => {
      console.log('Error while fetching orders');
      this.notififyService.show(error.message, 'danger');
    }
  );
} else {
  this.notififyService.show(
    'Part is not associated with any project',
    'danger'
  );
}

} I have no clue on how to proceed with testing the individual subscribe and error components and would really appreciate some help!

  • You have to first mock `getOrdersForPartwithObservable` with a `spyOn` with `projectId` and return `of` with object of what the method should return for the success part and `throwError` for the error scenario. – AliF50 May 20 '21 at 12:53
  • Makes sense. however what do we assert for the success part though? Since there is no return statement in the block per se. Sorry I'm really amateur. – comfortablecow May 20 '21 at 14:09
  • You can assert that `this.ordersHeader` is the accurate value and you can assert that `notifyService.show` was called in the else block. – AliF50 May 20 '21 at 14:52

0 Answers0