Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UnitTests] Popup window not close after some time #1165

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions src/navigators/PopupWindow.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { mocked } from "jest-mock";
import { PopupWindow } from "./PopupWindow";
import { Log } from "../utils";

function firstSuccessfulResult<T>(fn: () => T): T {
expect(fn).toHaveReturned();
return mocked(fn).mock.results[0].value as T;
}

function definePopupWindowClosedProperty(closed: boolean) {
const popupFromWindowOpen = firstSuccessfulResult(window.open);
Object.defineProperty(popupFromWindowOpen, "closed", {
enumerable: true,
value: closed,
});
}

describe("PopupWindow", () => {
beforeEach(() => {
Object.defineProperty(window, "location", {
Expand Down Expand Up @@ -115,11 +124,7 @@ describe("PopupWindow", () => {
const popupWindow = new PopupWindow({});

const promise = popupWindow.navigate({ url: "http://sts/authorize?x=y", state: "someid" });
const popupFromWindowOpen = firstSuccessfulResult(window.open);
Object.defineProperty(popupFromWindowOpen, "closed", {
enumerable: true,
value: true,
});
definePopupWindowClosedProperty(true);

jest.runOnlyPendingTimers();
await expect(promise).rejects.toThrow("Popup closed by user");
Expand All @@ -145,7 +150,7 @@ describe("PopupWindow", () => {
}, window.location.origin);
});

it("should close the window after closePopupWindowAfter is greater than 0", async () => {
it("should run setTimeout when closePopupWindowAfterInSeconds is greater than 0", async () => {
jest.spyOn(global, "setTimeout");

new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
Expand All @@ -155,7 +160,7 @@ describe("PopupWindow", () => {
jest.runAllTimers();
});

it("shouldnt close the window after closePopupWindowAfter is equal to 0", async () => {
it("shouldn't run setTimeout when closePopupWindowAfterInSeconds is equal to 0", async () => {
jest.spyOn(global, "setTimeout");

new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 0 } });
Expand All @@ -165,7 +170,7 @@ describe("PopupWindow", () => {
jest.runAllTimers();
});

it("shouldnt close the window after closePopupWindowAfter is less than 0", async () => {
it("shouldn't run setTimeout when closePopupWindowAfterInSeconds is less than 0", async () => {
jest.spyOn(global, "setTimeout");

new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: -120 } });
Expand All @@ -174,4 +179,39 @@ describe("PopupWindow", () => {
expect(setTimeout).toHaveBeenCalledTimes(0);
jest.runAllTimers();
});

it("should invoke close popup window when closePopupWindowAfterInSeconds is greater than 0 and window is open", async () => {
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
definePopupWindowClosedProperty(false);
const closeWindowSpy = jest.spyOn(popupWindow, "close");

jest.runOnlyPendingTimers();

expect(closeWindowSpy).toHaveBeenCalledTimes(1);
jest.runAllTimers();
});

it("shouldn't invoke close popup window when closePopupWindowAfterInSeconds is greater than 0 and window is not open", async () => {
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
definePopupWindowClosedProperty(true);
const closeWindowSpy = jest.spyOn(popupWindow, "close");

jest.runOnlyPendingTimers();

expect(closeWindowSpy).not.toBeCalled();
jest.runAllTimers();
});

it("should show error when closePopupWindowAfterInSeconds is greater than 0 and window is not open", async () => {
Log.setLevel(Log.DEBUG);
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
const consoleDebugSpy = jest.spyOn(console, "debug");
const promise = popupWindow.navigate({ url: "http://sts/authorize?x=y", state: "someid" });

jest.runOnlyPendingTimers();

await expect(promise).rejects.toThrow("Popup blocked by user");
expect(consoleDebugSpy).toHaveBeenCalled();
jest.runAllTimers();
});
});
2 changes: 1 addition & 1 deletion src/navigators/PopupWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class PopupWindow extends AbstractChildWindow {
const centeredPopup = PopupUtils.center({ ...DefaultPopupWindowFeatures, ...popupWindowFeatures });
this._window = window.open(undefined, popupWindowTarget, PopupUtils.serialize(centeredPopup));
if (popupWindowFeatures.closePopupWindowAfterInSeconds && popupWindowFeatures.closePopupWindowAfterInSeconds > 0) {
setTimeout(() => {
setTimeout(() => {
if (!this._window || typeof this._window.closed !== "boolean" || this._window.closed) {
this._abort.raise(new Error("Popup blocked by user"));
return;
Expand Down