11import { mocked } from "jest-mock" ;
22import { PopupWindow } from "./PopupWindow" ;
3+ import { Log } from "../utils" ;
34
45function 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+
918describe ( "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} ) ;
0 commit comments