Skip to content

Commit 25009cd

Browse files
authored
test(query-devtools/contexts/PiPContext): add tests for 'requestPipWindow' opening a PiP window and persisting 'pip_open' (#10797)
1 parent 335975d commit 25009cd

1 file changed

Lines changed: 96 additions & 2 deletions

File tree

packages/query-devtools/src/__tests__/contexts/PiPContext.test.tsx

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,66 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { afterEach, describe, expect, it, vi } from 'vitest'
22
import { render } from '@solidjs/testing-library'
3-
import { usePiPWindow } from '../../contexts'
3+
import { createEffect } from 'solid-js'
4+
import { createLocalStorage } from '@solid-primitives/storage'
5+
import { PiPProvider, usePiPWindow } from '../../contexts'
6+
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 = {
20+
document: pipDocument,
21+
innerWidth: 800,
22+
innerHeight: 600,
23+
addEventListener: vi.fn(),
24+
removeEventListener: vi.fn(),
25+
close: vi.fn(),
26+
...overrides,
27+
}
28+
const open = vi.fn(() => fakeWindow)
29+
vi.stubGlobal('open', open)
30+
return { pipDocument, fakeWindow, open }
31+
}
432

533
describe('PiPContext', () => {
34+
afterEach(() => {
35+
vi.unstubAllGlobals()
36+
})
37+
38+
function renderAndAct(
39+
action: (pip: ReturnType<typeof usePiPWindow>) => void,
40+
) {
41+
render(() => {
42+
const [localStore, setLocalStore] = createLocalStorage({
43+
prefix: 'TanstackQueryDevtools',
44+
})
45+
return (
46+
<PiPProvider localStore={localStore} setLocalStore={setLocalStore}>
47+
<PiPActor run={action} />
48+
</PiPProvider>
49+
)
50+
})
51+
}
52+
53+
function PiPActor(props: {
54+
run: (pip: ReturnType<typeof usePiPWindow>) => void
55+
}) {
56+
const pip = usePiPWindow()
57+
createEffect(() => {
58+
pip()
59+
props.run(pip)
60+
})
61+
return null
62+
}
63+
664
describe('usePiPWindow', () => {
765
it('should throw when used outside a "PiPProvider"', () => {
866
function PiPProbe() {
@@ -15,4 +73,40 @@ describe('PiPContext', () => {
1573
)
1674
})
1775
})
76+
77+
describe('"requestPipWindow"', () => {
78+
it('should call "window.open" with the expected target and features', () => {
79+
const { open } = stubPipWindow()
80+
81+
renderAndAct((pip) => pip().requestPipWindow(640, 480))
82+
83+
expect(open).toHaveBeenCalledWith(
84+
'',
85+
'TSQD-Devtools-Panel',
86+
'width=640,height=480,popup',
87+
)
88+
})
89+
90+
it('should set the "pipWindow" signal to the opened window', () => {
91+
const { fakeWindow } = stubPipWindow()
92+
let observed: Window | null = null
93+
94+
renderAndAct((pip) => {
95+
pip().requestPipWindow(640, 480)
96+
observed = pip().pipWindow
97+
})
98+
99+
expect(observed).toBe(fakeWindow)
100+
})
101+
102+
it('should persist "pip_open" as "true" to "localStore" after a successful open', () => {
103+
stubPipWindow()
104+
105+
renderAndAct((pip) => pip().requestPipWindow(640, 480))
106+
107+
expect(localStorage.getItem('TanstackQueryDevtools.pip_open')).toBe(
108+
'true',
109+
)
110+
})
111+
})
18112
})

0 commit comments

Comments
 (0)