Exercise 2: Test Components
Complete the unit tests:
- src/app/containers/search-dialog/search-dialog.component.spec.ts
- src/app/core/shell/shell.component.spec.ts
- src/app/shared/components/resort-autocomplete/resort-autocomplete.component.spec.ts
Steps
For all tests:
- Import the
StoreModule
. - Use the
StoreModule.forRoot()
to wire up the store in the testing moduleimports
array with the rootreducers
map. - Define a block scoped local
store
variable at the top of the firstdescribe()
block. - Get the
Store
object using theTestBed.get()
method.
Create tests for the SearchDialogComponent
that:
- Assert the
onResortSelected
method dispatches theSelectResort
action when invoked.
Create tests for the ShellComponent
that:
- Assert the
ngOnInit()
method declares the opened observable property. - Assert the
closeSidenav
method dispatches theHideSidenav
action. - Assert the
openDialog
method dispatch theOpenDialog
action when the<a class="search">
element is clicked. - Assert the
openSidenav
method dispatches theShowSidenav
action. - Assert the
toggleSidenav
method dispatches theShowSidenav
the first time the<button>
is clicked, and then subsequently, thetoggleSidenav
method dispatches theHideSidenav
the second time the<button>
is clicked.
Create tests for the ResortAutocompleteComponent
that:
- Asset the
ngOnInit()
method declares the resorts observable property. - Assert the
ngOnInit()
method dispatches theSearchResults
action when the search form control value changes.
Hints
- Use the
jest.spyOn()
method to create spies. - Use the
DebugElement.query()
method to query for elements within the component's host element. - Use the
By.css()
method to find elements along with theDebugElement.query()
method. - Use the
DebugElementtriggerEventHandler()
method to trigger aMouseClick
event.
Try it Out
Execute the jest test runner via:
npm test
yarn test