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:
hideSidenavOnSelect
setMapZoom
For each test:
- Declare the
action
that will be mocked. - Declare the
outcome
action. - set the
action
hot observable to emit a next notification of theaction
to test. - Declare the
expected
cold observable that emits a next notification of theoutcome
action. - Expect the effect to be the
expected
observable.
Challenge: DialogEffects
Create tests for the following effects in src/app/state/dialog/dialog.effects.spec.ts:
close
open
Hints
- Use the
generateResort()
function generate a fakeResort
object. - Use the
provideMockActions()
function to create a factory function that returns theactions
observable. - Use the
hot()
function to create a test hot observable to mock theactions
observable. - 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.