fix: adds monkey patching for replaceState (#7475)

This commit is contained in:
Anshuman Pandey
2026-03-13 19:10:20 +05:30
committed by GitHub
parent 92677e1ec0
commit bddcec0466
2 changed files with 37 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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", () => {