Exercise 3: Test Effects
Practice writing unit tests for effects.
ResortEffects
Create tests for the following effects in src/app/state/resort.resort.effects.spec.ts:
hideSidenavOnSelectsetMapZoom
For each test:
- Declare the
actionthat will be mocked. - Declare the
outcomeaction. - set the
actionhot observable to emit a next notification of theactionto test. - Declare the
expectedcold observable that emits a next notification of theoutcomeaction. - Expect the effect to be the
expectedobservable.
Challenge: DialogEffects
Create tests for the following effects in src/app/state/dialog/dialog.effects.spec.ts:
closeopen
Hints
- Use the
generateResort()function generate a fakeResortobject. - Use the
provideMockActions()function to create a factory function that returns theactionsobservable. - Use the
hot()function to create a test hot observable to mock theactionsobservable. - Use the
cold()function to create a test cold observable that is the expected observable that is returned from an action. - Use the
jest.spyOn()method to create spies. Combine this with themockReturnValue()method for a spy instance to specify the value returned. - If the effect does not dispatch an action then
subscribe()to the observable and assert the next notification value.