mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
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:
@@ -1,4 +1,5 @@
|
||||
import { Controller } from "stimulus";
|
||||
import { locationPathName } from "js/window_location_wrapper";
|
||||
|
||||
// This is meant to be used with the "modal:closing" event, ie:
|
||||
//
|
||||
@@ -13,7 +14,7 @@ export default class extends Controller {
|
||||
|
||||
redirect() {
|
||||
if (this.redirectValue) {
|
||||
window.location.pathname = "/shop";
|
||||
locationPathName("/shop");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ export default class PopoutController extends Controller {
|
||||
};
|
||||
|
||||
connect() {
|
||||
this.displayElements = Array.from(this.element.querySelectorAll('input:not([type="hidden"]'));
|
||||
this.displayElements = Array.from(this.element.querySelectorAll('input:not([type="hidden"])'));
|
||||
this.first_input = this.displayElements[0];
|
||||
|
||||
// Show when click or down-arrow on button
|
||||
|
||||
9
app/webpacker/js/window_location_wrapper.js
Normal file
9
app/webpacker/js/window_location_wrapper.js
Normal file
@@ -0,0 +1,9 @@
|
||||
// Wrapper around location window.location
|
||||
//
|
||||
// It's mainly needed because we can't mock window.location in jsdom
|
||||
//
|
||||
const locationPathName = (pathName) => {
|
||||
window.location.pathname = pathName;
|
||||
};
|
||||
|
||||
export { locationPathName };
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user