From 2e2c980ae6d6d3fe4d853089ab86a5d20fb8dfea Mon Sep 17 00:00:00 2001 From: David Date: Tue, 21 Feb 2023 13:23:25 -0500 Subject: [PATCH] frontend/../Banner.test.js: change replace window.history.pushState(...) 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: https://github.com/facebook/jest/issues/890 (note that the original method stopped working -- see https://github.com/facebook/jest/issues/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). --- frontend/src/components/Banner/Banner.test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/Banner/Banner.test.js b/frontend/src/components/Banner/Banner.test.js index 07022790..d15ba27d 100644 --- a/frontend/src/components/Banner/Banner.test.js +++ b/frontend/src/components/Banner/Banner.test.js @@ -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`, () => {