Fix test to work with new jsdom restriction

since jsdom 21, it's no longer possible to mock window.location
See : https://github.com/jsdom/jsdom/issues/3492
This commit is contained in:
Gaetan Craig-Riou
2025-11-11 14:37:36 +11:00
parent 2729fb14d6
commit 4e62e20fa8
5 changed files with 21 additions and 41 deletions

View File

@@ -1,32 +1,18 @@
/**
* @jest-environment jsdom
* @jest-environment-options {"url": "http://www.example.com/"}
*/
import { Application } from "stimulus";
import out_of_stock_modal_controller from "../../../app/webpacker/controllers/out_of_stock_modal_controller";
import * as locationWrapper from "js/window_location_wrapper";
import out_of_stock_modal_controller from "controllers/out_of_stock_modal_controller";
describe("OutOfStockModalController", () => {
beforeAll(() => {
const application = Application.start();
application.register("out-of-stock-modal", out_of_stock_modal_controller);
});
let originalWindowLocation = window.location;
beforeEach(() => {
Object.defineProperty(window, "location", {
configurable: true,
enumerable: true,
value: new URL(window.location.href),
});
});
afterEach(() => {
Object.defineProperty(window, "location", {
configurable: true,
enumerable: true,
value: originalWindowLocation,
});
jest.spyOn(locationWrapper, "locationPathName").mockImplementation(() => undefined);
});
// We use window to dispatch the closing event so we don't need to set up another controller
@@ -46,7 +32,7 @@ describe("OutOfStockModalController", () => {
const event = new Event("closing");
window.dispatchEvent(event);
expect(window.location.href).not.toBe("/shop");
expect(locationWrapper.locationPathName).not.toHaveBeenCalled();
});
});
@@ -65,7 +51,7 @@ describe("OutOfStockModalController", () => {
const event = new Event("closing");
window.dispatchEvent(event);
expect(window.location.pathname).toBe("/shop");
expect(locationWrapper.locationPathName).toHaveBeenCalledWith("/shop");
});
});
});

View File

@@ -3,7 +3,7 @@
*/
import { Application } from "stimulus";
import tabs_and_panels_controller from "../../../app/webpacker/controllers/tabs_and_panels_controller";
import tabs_and_panels_controller from "controllers/tabs_and_panels_controller";
describe("TabsAndPanelsController", () => {
beforeAll(() => {
@@ -90,16 +90,8 @@ describe("TabsAndPanelsController", () => {
});
describe("when valid anchor is specified in the url", () => {
const oldWindowLocation = window.location;
beforeAll(() => {
Object.defineProperty(window, "location", {
value: new URL("http://example.com/#boo_panel"),
configurable: true,
});
});
afterAll(() => {
delete window.location;
window.location = oldWindowLocation;
history.pushState({}, "", "#boo_panel");
});
it("#activateFromWindowLocationOrDefaultPanelTarget show panel based on anchor", () => {
@@ -121,16 +113,8 @@ describe("TabsAndPanelsController", () => {
});
describe("when non valid anchor is specified in the url", () => {
const oldWindowLocation = window.location;
beforeAll(() => {
Object.defineProperty(window, "location", {
value: new URL("http://example.com/#non_valid_panel"),
configurable: true,
});
});
afterAll(() => {
delete window.location;
window.location = oldWindowLocation;
history.pushState({}, "", "#non_valid_panel");
});
it("#activateFromWindowLocationOrDefaultPanelTarget show default panel", () => {