Skip to content

Commit

Permalink
frontend/../Banner.test.js: change replace window.history.pushState(.…
Browse files Browse the repository at this point in the history
…..) with `window.location` method used in List.test.js [+]

Reasons:

- Consistency with List.test.js
- Altering `window` with custom `location` is the method used by Facebook, the owners of Jest: jestjs/jest#890 (note that the original method stopped working -- see jestjs/jest#5124)
- `window.history.pushState` simulates something slightly different than what we are testing for, and in a real browser changes state and also triggers events.  While here in these tests we are not dealing with a browser `window` object but the one provided by jsdom, it would still seem more brittle, because whether or not the jsdom project implements history to work the way it does in actual browsers, it might have an API in flux, and we don't want to have to keep track of the fluctuations and potential side effects.  The `delete window.location` followed by replacement with our own `location` is not subject to changes to the jsdom API (in theory, although see the 2nd link in the previous point).
  • Loading branch information
da70 committed Feb 21, 2023
1 parent bda76ee commit 2e2c980
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion frontend/src/components/Banner/Banner.test.js
Expand Up @@ -9,7 +9,14 @@ const institutionNamesUpperCase = Object.keys(bannerInstitutionInfo).map(institu
describe.each(institutionNamesUpperCase)(
'Institution name: %s', (institutionNameUpperCase) => {
beforeEach( () => {
window.history.pushState({}, null, `/?institution=${institutionNameUpperCase}`);
delete window.location;
window.location = new URL(`${process.env.REACT_APP_API_URL}?institution=${institutionNameUpperCase}`);
});

afterEach(() => {
delete window.location;
window.location = new URL(process.env.REACT_APP_API_URL);
jest.clearAllMocks();
});

test(`renders ${institutionNameUpperCase} page correctly`, () => {
Expand Down

0 comments on commit 2e2c980

Please sign in to comment.