-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Angular router link tests to use RouterTestingHarness (#2430)
# Pull Request ## 🤨 Rationale We have several tests that verify Angular router link behavior by clicking links and inspecting router state. These tests exhibit a couple problems: 1. currently they report warnings in the output log like: `[web-server]: 404: /_karma_webpack_/page1?param1=true` 2. In #2387 when we bring in a newer version of Chromium, these cause other tests to fail to execute and report timeouts The root cause of these issues is that the tests are actually trying to navigate the page when the link is clicked. ## 👩💻 Implementation ### "with-href" `nimbleRouterLink` tests In researching best practices for writing tests like this I learned that the `RouterTestingModule` we had been using has been deprecated and replaced with a more powerful `RouterTestingHarness`. [This blog and video](https://www.rainerhahnekamp.com/en/how-do-i-test-using-the-routertestingharness/) does a good job of explaining it, much better than [the angular docs](https://angular.dev/api/router/testing/RouterTestingHarness). The basic idea is that the harness sets up a parent component and router which host your component under test. When something tries to navigate the harness captures information about the navigation but doesn't actually navigate the page. The fixes in this PR are to use `RouterTestingHarness` instead of `RouterTestingModule`. This has these side effects: 1. The routes are configured with `provideRouter` instead of `withRoutes` 2. The navigated route started to be relative to the current route, so starting from `/start` and clicking a link to `page` resulted in a URL of `/start/page`. The simplest fix I found for this was to change the starting page to `/`. 3. Some setup code could be deleted and made sync/async instead of fakeAsync. In addition even if the router doesn't navigate the page we are still invoking click handlers on anchors which try to navigate the page. To address that I'm calling `preventDefault()` from those handlers. ### error `routerLink` tests These tests included a `RouterTestingModule` but didn't actually need it or `RouterTestingHarness` so I deleted those imports. ### package.json Added a couple dev scripts that I found useful while running tests locally. We didn't have an obvious way to debug angular tests or to run just the tests for one project and now we do. ## 🧪 Testing 1. Verified the 404 warnings are no longer printed to the console 5. Verified the tests don't navigate the page in the newer version of Chromium 6. Verified tests still fail with various changes like changing the URL or not clicking the link ## ✅ Checklist <!--- Review the list and put an x in the boxes that apply or ~~strike through~~ around items that don't (along with an explanation). --> - [ ] I have updated the project documentation to reflect my changes or determined no changes are needed. --------- Co-authored-by: Milan Raj <[email protected]>
- Loading branch information
Showing
15 changed files
with
137 additions
and
150 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@ni-nimble-angular-16d68d67-9ecc-41b3-b7aa-cb52dbd66da4.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "none", | ||
"comment": "Angular router link test improvements", | ||
"packageName": "@ni/nimble-angular", | ||
"email": "[email protected]", | ||
"dependentChangeType": "none" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.