Reputation: 1244
How I can mock the window.open with jest? I tried several options but each of them is failed
I need to check if the window.open is called and it should be called with certain params
Assume that I have something like this
open(): void {
window.open('/link');
}
Upvotes: 0
Views: 4376
Reputation: 3277
Install these packages:
jest-environment-jsdom
jest-environment-jsdom-global
Add "testEnvironment": "jest-environment-jsdom-global"
in your jest configurations.
Suppose you have a function like this:
open() {
window.open("abc");
}
Then inside test file:
it("should open the url in window", () => {
const openSpy = jest.spyOn(window, "open");
open();
expect(openSpy).toHaveBeenCalledTimes(1);
expect(openSpy).toHaveBeenCalledWith("abc");
});
Upvotes: 1