@@ -80,7 +80,12 @@ describe('BrowserWindow module', () => {
80
80
} )
81
81
it ( 'should emit beforeunload handler' , async ( ) => {
82
82
await w . loadFile ( path . join ( fixtures , 'api' , 'beforeunload-false.html' ) )
83
- const beforeunload = emittedOnce ( w , 'onbeforeunload' )
83
+ const beforeunload = new Promise ( resolve => {
84
+ ipcMain . once ( 'onbeforeunload' , ( e ) => {
85
+ e . returnValue = null
86
+ resolve ( )
87
+ } )
88
+ } )
84
89
w . close ( )
85
90
await beforeunload
86
91
} )
@@ -164,8 +169,9 @@ describe('BrowserWindow module', () => {
164
169
expect ( content ) . to . equal ( 'close' )
165
170
} )
166
171
it ( 'should emit beforeunload event' , async ( ) => {
167
- w . loadFile ( path . join ( fixtures , 'api' , 'close-beforeunload-false.html' ) )
168
- await emittedOnce ( w , 'onbeforeunload' )
172
+ w . loadFile ( path . join ( __dirname , 'fixtures' , 'api' , 'close-beforeunload-false.html' ) )
173
+ const [ e ] = await emittedOnce ( ipcMain , 'onbeforeunload' )
174
+ e . returnValue = null
169
175
} )
170
176
} )
171
177
@@ -1552,7 +1558,7 @@ describe('BrowserWindow module', () => {
1552
1558
expect ( test ) . to . eql ( 'preload' )
1553
1559
} )
1554
1560
it ( 'can successfully delete the Buffer global' , async ( ) => {
1555
- const preload = path . join ( fixtures , 'module' , 'delete-buffer.js' )
1561
+ const preload = path . join ( __dirname , ' fixtures' , 'module' , 'delete-buffer.js' )
1556
1562
const w = new BrowserWindow ( {
1557
1563
show : false ,
1558
1564
webPreferences : {
@@ -1676,7 +1682,7 @@ describe('BrowserWindow module', () => {
1676
1682
describe ( '"enableRemoteModule" option' , ( ) => {
1677
1683
const generateSpecs = ( description : string , sandbox : boolean ) => {
1678
1684
describe ( description , ( ) => {
1679
- const preload = path . join ( fixtures , 'module' , 'preload-remote.js' )
1685
+ const preload = path . join ( __dirname , ' fixtures' , 'module' , 'preload-remote.js' )
1680
1686
1681
1687
it ( 'enables the remote module by default' , async ( ) => {
1682
1688
const w = new BrowserWindow ( {
@@ -1794,7 +1800,7 @@ describe('BrowserWindow module', () => {
1794
1800
preload
1795
1801
}
1796
1802
} )
1797
- const htmlPath = path . join ( fixtures , 'api' , 'sandbox.html?exit-event' )
1803
+ const htmlPath = path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html?exit-event' )
1798
1804
const pageUrl = 'file://' + htmlPath
1799
1805
w . loadURL ( pageUrl )
1800
1806
const [ , url ] = await emittedOnce ( ipcMain , 'answer' )
@@ -1815,7 +1821,7 @@ describe('BrowserWindow module', () => {
1815
1821
w . webContents . once ( 'new-window' , ( event , url , frameName , disposition , options ) => {
1816
1822
options . webPreferences ! . preload = preload
1817
1823
} )
1818
- const htmlPath = path . join ( fixtures , 'api' , 'sandbox.html?window-open' )
1824
+ const htmlPath = path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html?window-open' )
1819
1825
const pageUrl = 'file://' + htmlPath
1820
1826
const answer = emittedOnce ( ipcMain , 'answer' )
1821
1827
w . loadURL ( pageUrl )
@@ -1844,7 +1850,7 @@ describe('BrowserWindow module', () => {
1844
1850
options . webPreferences ! . preload = preload
1845
1851
} )
1846
1852
w . loadFile (
1847
- path . join ( fixtures , 'api' , 'sandbox.html' ) ,
1853
+ path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html' ) ,
1848
1854
{ search : 'window-open-external' }
1849
1855
)
1850
1856
@@ -1921,7 +1927,11 @@ describe('BrowserWindow module', () => {
1921
1927
prefs . foo = 'bar'
1922
1928
} )
1923
1929
w . loadFile ( path . join ( fixtures , 'api' , 'new-window.html' ) )
1924
- const [ , , webPreferences ] = await emittedOnce ( ipcMain , 'answer' )
1930
+ const [ [ , childWebContents ] ] = await Promise . all ( [
1931
+ emittedOnce ( app , 'web-contents-created' ) ,
1932
+ emittedOnce ( ipcMain , 'answer' )
1933
+ ] )
1934
+ const webPreferences = ( childWebContents as any ) . getLastWebPreferences ( )
1925
1935
expect ( webPreferences . foo ) . to . equal ( 'bar' )
1926
1936
} )
1927
1937
@@ -1952,7 +1962,7 @@ describe('BrowserWindow module', () => {
1952
1962
'parent-answer' ,
1953
1963
'child-answer'
1954
1964
] , done )
1955
- w . loadFile ( path . join ( fixtures , 'api' , 'sandbox.html' ) , { search : 'verify-ipc-sender' } )
1965
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html' ) , { search : 'verify-ipc-sender' } )
1956
1966
} )
1957
1967
1958
1968
describe ( 'event handling' , ( ) => {
@@ -1988,7 +1998,7 @@ describe('BrowserWindow module', () => {
1988
1998
'did-frame-finish-load' ,
1989
1999
'dom-ready'
1990
2000
] , done )
1991
- w . loadFile ( path . join ( fixtures , 'api' , 'sandbox.html' ) , { search : 'webcontents-events' } )
2001
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html' ) , { search : 'webcontents-events' } )
1992
2002
} )
1993
2003
} )
1994
2004
@@ -2021,7 +2031,7 @@ describe('BrowserWindow module', () => {
2021
2031
sandbox : true
2022
2032
}
2023
2033
} )
2024
- w . loadFile ( path . join ( fixtures , 'api' , 'sandbox.html' ) , { search : 'reload-remote' } )
2034
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html' ) , { search : 'reload-remote' } )
2025
2035
2026
2036
ipcMain . on ( 'get-remote-module-path' , ( event ) => {
2027
2037
event . returnValue = path . join ( fixtures , 'module' , 'hello.js' )
@@ -2057,7 +2067,7 @@ describe('BrowserWindow module', () => {
2057
2067
options . webPreferences ! . preload = preload
2058
2068
} )
2059
2069
2060
- w . loadFile ( path . join ( fixtures , 'api' , 'sandbox.html' ) , { search : 'reload-remote-child' } )
2070
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'sandbox.html' ) , { search : 'reload-remote-child' } )
2061
2071
2062
2072
ipcMain . on ( 'get-remote-module-path' , ( event ) => {
2063
2073
event . returnValue = path . join ( fixtures , 'module' , 'hello-child.js' )
@@ -2233,7 +2243,11 @@ describe('BrowserWindow module', () => {
2233
2243
prefs . foo = 'bar'
2234
2244
} )
2235
2245
w . loadFile ( path . join ( fixtures , 'api' , 'new-window.html' ) )
2236
- const [ , , webPreferences ] = await emittedOnce ( ipcMain , 'answer' )
2246
+ const [ [ , childWebContents ] ] = await Promise . all ( [
2247
+ emittedOnce ( app , 'web-contents-created' ) ,
2248
+ emittedOnce ( ipcMain , 'answer' )
2249
+ ] )
2250
+ const webPreferences = ( childWebContents as any ) . getLastWebPreferences ( )
2237
2251
expect ( webPreferences . foo ) . to . equal ( 'bar' )
2238
2252
} )
2239
2253
it ( 'should have nodeIntegration disabled in child windows' , async ( ) => {
@@ -2359,35 +2373,41 @@ describe('BrowserWindow module', () => {
2359
2373
beforeEach ( ( ) => {
2360
2374
w = new BrowserWindow ( { show : false , webPreferences : { nodeIntegration : true } } )
2361
2375
} )
2376
+ afterEach ( ( ) => {
2377
+ ipcMain . removeAllListeners ( 'onbeforeunload' )
2378
+ } )
2362
2379
afterEach ( closeAllWindows )
2363
2380
it ( 'returning undefined would not prevent close' , ( done ) => {
2364
2381
w . once ( 'closed' , ( ) => { done ( ) } )
2365
- w . loadFile ( path . join ( fixtures , 'api' , 'close-beforeunload-undefined.html' ) )
2382
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'close-beforeunload-undefined.html' ) )
2366
2383
} )
2367
- it ( 'returning false would prevent close' , ( done ) => {
2368
- w . once ( 'onbeforeunload' as any , ( ) => { done ( ) } )
2369
- w . loadFile ( path . join ( fixtures , 'api' , 'close-beforeunload-false.html' ) )
2384
+ it ( 'returning false would prevent close' , async ( ) => {
2385
+ w . loadFile ( path . join ( __dirname , 'fixtures' , 'api' , 'close-beforeunload-false.html' ) )
2386
+ const [ e ] = await emittedOnce ( ipcMain , 'onbeforeunload' )
2387
+ e . returnValue = null
2370
2388
} )
2371
2389
it ( 'returning empty string would prevent close' , ( done ) => {
2372
- w . once ( 'onbeforeunload' as any , ( ) => { done ( ) } )
2373
- w . loadFile ( path . join ( fixtures , 'api' , 'close-beforeunload-empty-string.html' ) )
2390
+ ipcMain . once ( 'onbeforeunload' , ( e ) => { e . returnValue = null ; done ( ) } )
2391
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'close-beforeunload-empty-string.html' ) )
2374
2392
} )
2375
2393
it ( 'emits for each close attempt' , ( done ) => {
2376
2394
let beforeUnloadCount = 0
2377
- w . on ( 'onbeforeunload' as any , ( ) => {
2395
+ ipcMain . on ( 'onbeforeunload' , ( e ) => {
2396
+ e . returnValue = null
2378
2397
beforeUnloadCount += 1
2379
2398
if ( beforeUnloadCount < 3 ) {
2380
2399
w . close ( )
2381
2400
} else if ( beforeUnloadCount === 3 ) {
2382
2401
done ( )
2383
2402
}
2384
2403
} )
2385
- w . webContents . once ( 'did-finish-load' , ( ) => { w . close ( ) } )
2386
- w . loadFile ( path . join ( fixtures , 'api' , 'beforeunload-false-prevent3.html' ) )
2404
+ w . webContents . once ( 'did-finish-load' , ( ) => { w . webContents . executeJavaScript ( 'window. close()' , true ) } )
2405
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'beforeunload-false-prevent3.html' ) )
2387
2406
} )
2388
2407
it ( 'emits for each reload attempt' , ( done ) => {
2389
2408
let beforeUnloadCount = 0
2390
- w . on ( 'onbeforeunload' as any , ( ) => {
2409
+ ipcMain . on ( 'onbeforeunload' , ( e ) => {
2410
+ e . returnValue = null
2391
2411
beforeUnloadCount += 1
2392
2412
if ( beforeUnloadCount < 3 ) {
2393
2413
w . reload ( )
@@ -2401,11 +2421,12 @@ describe('BrowserWindow module', () => {
2401
2421
} )
2402
2422
w . reload ( )
2403
2423
} )
2404
- w . loadFile ( path . join ( fixtures , 'api' , 'beforeunload-false-prevent3.html' ) )
2424
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'beforeunload-false-prevent3.html' ) )
2405
2425
} )
2406
2426
it ( 'emits for each navigation attempt' , ( done ) => {
2407
2427
let beforeUnloadCount = 0
2408
- w . on ( 'onbeforeunload' as any , ( ) => {
2428
+ ipcMain . on ( 'onbeforeunload' , ( e ) => {
2429
+ e . returnValue = null
2409
2430
beforeUnloadCount += 1
2410
2431
if ( beforeUnloadCount < 3 ) {
2411
2432
w . loadURL ( 'about:blank' )
@@ -2419,7 +2440,7 @@ describe('BrowserWindow module', () => {
2419
2440
} )
2420
2441
w . loadURL ( 'about:blank' )
2421
2442
} )
2422
- w . loadFile ( path . join ( fixtures , 'api' , 'beforeunload-false-prevent3.html' ) )
2443
+ w . loadFile ( path . join ( __dirname , ' fixtures' , 'api' , 'beforeunload-false-prevent3.html' ) )
2423
2444
} )
2424
2445
} )
2425
2446
0 commit comments