6

I am now facing a problem. not sure how to test if the action$ with filter operator.

I am also trying to follow the rules of https://github.com/vsavkin/testing_ngrx_effects/tree/309b84883c2709a34ab98b696398332d33c2104f

make it simple, I just set the filter if the length of array is 0 return true.

for example:

loadDatas$: Observable<Action> = this.actions$.ofType(LOAD_DATAS_ACTION).pipe(

withLatestFrom(this.store.select(getDatas), (action, datas) =>datas),

filter(data => !data.length),

switchMap(() => {

console.log(‘run api’);

return this.dataApi.find().pipe(

map((datas: Data[]) => new DatasLoadedAction(datas))

………

…..

so I try to write two test cases, one is

expect(effects.loadDatas$).toBeObservable(expected);

when filter return true.

but I don’t know how to test if the filter return false.

Do you have any suggestion for this ? thank a lot

Anandhu Nadesh
  • 672
  • 2
  • 11
  • 20
Wei YuTseng
  • 63
  • 1
  • 3

1 Answers1

13

You expect the effect to not return a new action, so you can compare it with an 'empty' observable:

const expected = cold('----');
expect(effects.loadDatas$).toBeObservable(expected);
Manduro
  • 833
  • 8
  • 15
  • 1
    woow, you r right, but I wonder why .not.toBeobservable is not working – Wei YuTseng Jan 03 '18 at 04:05
  • That’s a bit strange yes. I think not doesn’t work as expected with observable testing and when it’s not an exact match but still a partly match the not ends up false. Only when the variable does not hold an observable at all it is true. Anyway, it’s always better to test for an exact match instead of a negated one, as it’s more strict. – Manduro Jan 03 '18 at 07:28