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 moduleimportsarray with the rootreducersmap. - Define a block scoped local
storevariable at the top of the firstdescribe()block. - Get the
Storeobject using theTestBed.get()method.
Create tests for the SearchDialogComponent that:
- Assert the
onResortSelectedmethod dispatches theSelectResortaction when invoked.
Create tests for the ShellComponent that:
- Assert the
ngOnInit()method declares the opened observable property. - Assert the
closeSidenavmethod dispatches theHideSidenavaction. - Assert the
openDialogmethod dispatch theOpenDialogaction when the<a class="search">element is clicked. - Assert the
openSidenavmethod dispatches theShowSidenavaction. - Assert the
toggleSidenavmethod dispatches theShowSidenavthe first time the<button>is clicked, and then subsequently, thetoggleSidenavmethod dispatches theHideSidenavthe 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 theSearchResultsaction 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 aMouseClickevent.
Try it Out
Execute the jest test runner via:
npm test
yarn test