mirror of
https://github.com/formbricks/formbricks.git
synced 2026-03-13 19:30:36 -05:00
fix: adds monkey patching for replaceState (#7475)
This commit is contained in:
@@ -175,6 +175,16 @@ export const addPageUrlEventListeners = (): void => {
|
||||
window.dispatchEvent(event);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method -- We need to access the original method
|
||||
const originalReplaceState = history.replaceState;
|
||||
|
||||
// eslint-disable-next-line func-names -- We need an anonymous function here
|
||||
history.replaceState = function (...args) {
|
||||
originalReplaceState.apply(this, args);
|
||||
const event = new Event("replacestate");
|
||||
window.dispatchEvent(event);
|
||||
};
|
||||
|
||||
isHistoryPatched = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
removeExitIntentListener,
|
||||
removePageUrlEventListeners,
|
||||
removeScrollDepthListener,
|
||||
setIsHistoryPatched,
|
||||
} from "@/lib/survey/no-code-action";
|
||||
import { setIsSurveyRunning } from "@/lib/survey/widget";
|
||||
import { type TActionClassNoCodeConfig } from "@/types/survey";
|
||||
@@ -638,6 +639,32 @@ describe("addPageUrlEventListeners additional cases", () => {
|
||||
(window.addEventListener as Mock).mockRestore();
|
||||
dispatchEventSpy.mockRestore();
|
||||
});
|
||||
|
||||
test("patched history.replaceState dispatches a 'replacestate' event", () => {
|
||||
const addEventListenerMock = vi.fn();
|
||||
const removeEventListenerMock = vi.fn();
|
||||
const originalReplaceState = vi.fn();
|
||||
const dispatchEventMock = vi.fn();
|
||||
vi.stubGlobal("window", {
|
||||
addEventListener: addEventListenerMock,
|
||||
removeEventListener: removeEventListenerMock,
|
||||
dispatchEvent: dispatchEventMock,
|
||||
});
|
||||
vi.stubGlobal("history", { pushState: vi.fn(), replaceState: originalReplaceState });
|
||||
|
||||
// Reset patching state so addPageUrlEventListeners patches history fresh
|
||||
setIsHistoryPatched(false);
|
||||
removePageUrlEventListeners();
|
||||
addPageUrlEventListeners();
|
||||
|
||||
// Call the patched replaceState
|
||||
history.replaceState({}, "", "/replaced-url");
|
||||
|
||||
expect(originalReplaceState).toHaveBeenCalledWith({}, "", "/replaced-url");
|
||||
expect(dispatchEventMock).toHaveBeenCalledWith(expect.objectContaining({ type: "replacestate" }));
|
||||
|
||||
(window.addEventListener as Mock).mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("removePageUrlEventListeners additional cases", () => {
|
||||
|
||||
Reference in New Issue
Block a user