@@ -4,26 +4,22 @@ import { createEffect } from 'solid-js'
44import { createLocalStorage } from '@solid-primitives/storage'
55import { PiPProvider , usePiPWindow } from '../../contexts'
66
7- type FakePipWindow = Pick <
8- Window ,
9- | 'document'
10- | 'innerWidth'
11- | 'innerHeight'
12- | 'addEventListener'
13- | 'removeEventListener'
14- | 'close'
15- >
16-
17- function stubPipWindow ( overrides : Partial < FakePipWindow > = { } ) {
18- const pipDocument = document . implementation . createHTMLDocument ( 'PiP' )
19- const fakeWindow : FakePipWindow = {
7+ type FakePipWindowOverrides = {
8+ document ?: Document
9+ innerWidth ?: number
10+ innerHeight ?: number
11+ }
12+
13+ function stubPipWindow ( overrides : FakePipWindowOverrides = { } ) {
14+ const pipDocument =
15+ overrides . document ?? document . implementation . createHTMLDocument ( 'PiP' )
16+ const fakeWindow = {
2017 document : pipDocument ,
21- innerWidth : 800 ,
22- innerHeight : 600 ,
18+ innerWidth : overrides . innerWidth ?? 800 ,
19+ innerHeight : overrides . innerHeight ?? 600 ,
2320 addEventListener : vi . fn ( ) ,
2421 removeEventListener : vi . fn ( ) ,
2522 close : vi . fn ( ) ,
26- ...overrides ,
2723 }
2824 const open = vi . fn ( ( ) => fakeWindow )
2925 vi . stubGlobal ( 'open' , open )
@@ -37,13 +33,18 @@ describe('PiPContext', () => {
3733
3834 function renderAndAct (
3935 action : ( pip : ReturnType < typeof usePiPWindow > ) => void ,
36+ options : { disabled ?: boolean } = { } ,
4037 ) {
4138 render ( ( ) => {
4239 const [ localStore , setLocalStore ] = createLocalStorage ( {
4340 prefix : 'TanstackQueryDevtools' ,
4441 } )
4542 return (
46- < PiPProvider localStore = { localStore } setLocalStore = { setLocalStore } >
43+ < PiPProvider
44+ localStore = { localStore }
45+ setLocalStore = { setLocalStore }
46+ disabled = { options . disabled }
47+ >
4748 < PiPActor run = { action } />
4849 </ PiPProvider >
4950 )
@@ -54,9 +55,13 @@ describe('PiPContext', () => {
5455 run : ( pip : ReturnType < typeof usePiPWindow > ) => void
5556 } ) {
5657 const pip = usePiPWindow ( )
58+ let hasRun = false
5759 createEffect ( ( ) => {
5860 pip ( )
59- props . run ( pip )
61+ if ( ! hasRun ) {
62+ hasRun = true
63+ props . run ( pip )
64+ }
6065 } )
6166 return null
6267 }
@@ -231,4 +236,29 @@ describe('PiPContext', () => {
231236 }
232237 } )
233238 } )
239+
240+ describe ( '"pagehide" lifecycle' , ( ) => {
241+ it ( 'should reset the "pipWindow" signal and "pip_open" when the "pagehide" event fires on the opened window' , ( ) => {
242+ const { fakeWindow } = stubPipWindow ( )
243+ const observed : Array < Window | null > = [ ]
244+
245+ renderAndAct (
246+ ( pip ) => {
247+ pip ( ) . requestPipWindow ( 640 , 480 )
248+ observed . push ( pip ( ) . pipWindow )
249+ const pagehideHandler = fakeWindow . addEventListener . mock . calls . find (
250+ ( [ event ] ) => event === 'pagehide' ,
251+ ) ?. [ 1 ]
252+ pagehideHandler ?.( )
253+ observed . push ( pip ( ) . pipWindow )
254+ } ,
255+ { disabled : true } ,
256+ )
257+
258+ expect ( observed ) . toEqual ( [ fakeWindow , null ] )
259+ expect ( localStorage . getItem ( 'TanstackQueryDevtools.pip_open' ) ) . toBe (
260+ 'false' ,
261+ )
262+ } )
263+ } )
234264} )
0 commit comments