tomastrajan / angular-ngrx-material-starter

Angular, NgRx, Angular CLI & Angular Material Starter Project
https://tomastrajan.github.io/angular-ngrx-material-starter
MIT License
2.82k stars 919 forks source link

test(stock-market): add stock market effects spec, closes #267 #389

Closed realtomaszkula closed 5 years ago

realtomaszkula commented 5 years ago

I wrote the tests without the TestBed as advised by Viktor Savkin in this example repo. If you feel that the tests should use TestBed I can refactor it without a problem.

I refactored the retrieveStock effect into a function, which allows for easier overrides of the debounceTime value and scheduler during testing. It follows the official effects testing guide.

Finally, I fixed a bug with distinctUntilChanged() operator. Let me demonstrate it with the example:

Assuming we dispatch the following actions:

{ type: 'RETRIEVE', payload: { symbol: 'TSLA' } }
{ type: 'RETRIEVE', payload: { symbol: 'TSLA' } }
{ type: 'RETRIEVE', payload: { symbol: 'TSLA' } }

By default distinctUntilChanged() uses === operator for equality check. Because every action is a new object, the operator will not filter anything, even though the actions have the exact same payload structure.

Luckily, distinctUntilChanged() accepts a compare function as an argument and we can just compare the symbol to get the desired behavior.

distinctUntilChanged((x, y) => x.payload.symbol === y.payload.symbol)
tomastrajan commented 5 years ago

So, to summarize, this will be merged as soon as it is refactored to use beforeEach.

realtomaszkula commented 5 years ago

Fixed everything as requested :)