Skip to content

Commit 1c4a2e4

Browse files
[UnitTests] Popup window not close after some time
1 parent 9f6ec0d commit 1c4a2e4

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

src/navigators/PopupWindow.test.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { mocked } from "jest-mock";
22
import { PopupWindow } from "./PopupWindow";
3+
import { Log } from "../utils";
34

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

10+
function definePopupWindowClosedProperty(closed: boolean) {
11+
const popupFromWindowOpen = firstSuccessfulResult(window.open);
12+
Object.defineProperty(popupFromWindowOpen, "closed", {
13+
enumerable: true,
14+
value: closed,
15+
});
16+
}
17+
918
describe("PopupWindow", () => {
1019
beforeEach(() => {
1120
Object.defineProperty(window, "location", {
@@ -115,11 +124,7 @@ describe("PopupWindow", () => {
115124
const popupWindow = new PopupWindow({});
116125

117126
const promise = popupWindow.navigate({ url: "http://sts/authorize?x=y", state: "someid" });
118-
const popupFromWindowOpen = firstSuccessfulResult(window.open);
119-
Object.defineProperty(popupFromWindowOpen, "closed", {
120-
enumerable: true,
121-
value: true,
122-
});
127+
definePopupWindowClosedProperty(true);
123128

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

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

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

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

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

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

171176
new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: -120 } });
@@ -174,4 +179,39 @@ describe("PopupWindow", () => {
174179
expect(setTimeout).toHaveBeenCalledTimes(0);
175180
jest.runAllTimers();
176181
});
182+
183+
it("should invoke close popup window when closePopupWindowAfterInSeconds is greater than 0 and window is open", async () => {
184+
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
185+
definePopupWindowClosedProperty(false);
186+
const closeWindowSpy = jest.spyOn(popupWindow, "close");
187+
188+
jest.runOnlyPendingTimers();
189+
190+
expect(closeWindowSpy).toHaveBeenCalledTimes(1);
191+
jest.runAllTimers();
192+
});
193+
194+
it("shouldn't invoke close popup window when closePopupWindowAfterInSeconds is greater than 0 and window is not open", async () => {
195+
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
196+
definePopupWindowClosedProperty(true);
197+
const closeWindowSpy = jest.spyOn(popupWindow, "close");
198+
199+
jest.runOnlyPendingTimers();
200+
201+
expect(closeWindowSpy).not.toBeCalled();
202+
jest.runAllTimers();
203+
});
204+
205+
it("should show error when closePopupWindowAfterInSeconds is greater than 0 and window is not open", async () => {
206+
Log.setLevel(Log.DEBUG);
207+
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfterInSeconds: 1 } });
208+
const consoleDebugSpy = jest.spyOn(console, "debug");
209+
const promise = popupWindow.navigate({ url: "http://sts/authorize?x=y", state: "someid" });
210+
211+
jest.runOnlyPendingTimers();
212+
213+
await expect(promise).rejects.toThrow("Popup blocked by user");
214+
expect(consoleDebugSpy).toHaveBeenCalled();
215+
jest.runAllTimers();
216+
});
177217
});

src/navigators/PopupWindow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class PopupWindow extends AbstractChildWindow {
3333
const centeredPopup = PopupUtils.center({ ...DefaultPopupWindowFeatures, ...popupWindowFeatures });
3434
this._window = window.open(undefined, popupWindowTarget, PopupUtils.serialize(centeredPopup));
3535
if (popupWindowFeatures.closePopupWindowAfterInSeconds && popupWindowFeatures.closePopupWindowAfterInSeconds > 0) {
36-
setTimeout(() => {
36+
setTimeout(() => {
3737
if (!this._window || typeof this._window.closed !== "boolean" || this._window.closed) {
3838
this._abort.raise(new Error("Popup blocked by user"));
3939
return;

0 commit comments

Comments
 (0)