Skip to content

Commit db19ccf

Browse files
test(query-devtools/contexts/PiPContext): add tests for the 'pip_open' auto-open createEffect (TanStack#10803)
* test(query-devtools/contexts/PiPContext): add tests for the 'pip_open' auto-open createEffect * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent d7c27dc commit db19ccf

1 file changed

Lines changed: 72 additions & 1 deletion

File tree

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ function stubPipWindow(overrides: FakePipWindowOverrides = {}) {
2929
describe('PiPContext', () => {
3030
afterEach(() => {
3131
vi.unstubAllGlobals()
32+
localStorage.clear()
3233
})
3334

3435
function renderAndAct(
3536
action: (pip: ReturnType<typeof usePiPWindow>) => void,
36-
options: { disabled?: boolean } = {},
37+
options: {
38+
disabled?: boolean
39+
initialStorage?: Record<string, string>
40+
} = {},
3741
) {
42+
Object.entries(options.initialStorage ?? {}).forEach(([key, value]) => {
43+
localStorage.setItem(key, value)
44+
})
3845
render(() => {
3946
const [localStore, setLocalStore] = createLocalStorage({
4047
prefix: 'TanstackQueryDevtools',
@@ -306,4 +313,68 @@ describe('PiPContext', () => {
306313
expect(fakeWindow.close).toHaveBeenCalledTimes(1)
307314
})
308315
})
316+
317+
describe('"pip_open" auto-open createEffect', () => {
318+
it('should auto-open a PiP window when "pip_open" is "true" on mount', () => {
319+
const { open } = stubPipWindow()
320+
321+
renderAndAct(() => {}, {
322+
initialStorage: { 'TanstackQueryDevtools.pip_open': 'true' },
323+
})
324+
325+
expect(open).toHaveBeenCalled()
326+
})
327+
328+
it('should not auto-open a PiP window when "disabled" is true', () => {
329+
const { open } = stubPipWindow()
330+
331+
renderAndAct(() => {}, {
332+
disabled: true,
333+
initialStorage: { 'TanstackQueryDevtools.pip_open': 'true' },
334+
})
335+
336+
expect(open).not.toHaveBeenCalled()
337+
})
338+
339+
it('should reset "pip_open"/"open" and log when "window.open" returns null on auto-open', () => {
340+
vi.stubGlobal(
341+
'open',
342+
vi.fn(() => null),
343+
)
344+
const consoleError = vi
345+
.spyOn(console, 'error')
346+
.mockImplementation(() => {})
347+
348+
try {
349+
renderAndAct(() => {}, {
350+
initialStorage: { 'TanstackQueryDevtools.pip_open': 'true' },
351+
})
352+
353+
expect(consoleError).toHaveBeenCalledWith(
354+
expect.stringContaining('Failed to open popup'),
355+
)
356+
expect(localStorage.getItem('TanstackQueryDevtools.pip_open')).toBe(
357+
'false',
358+
)
359+
expect(localStorage.getItem('TanstackQueryDevtools.open')).toBe('false')
360+
} finally {
361+
consoleError.mockRestore()
362+
}
363+
})
364+
365+
it('should re-throw non-"PipOpenError" errors from "window.open" on auto-open', () => {
366+
vi.stubGlobal(
367+
'open',
368+
vi.fn(() => {
369+
throw new Error('unexpected')
370+
}),
371+
)
372+
373+
expect(() =>
374+
renderAndAct(() => {}, {
375+
initialStorage: { 'TanstackQueryDevtools.pip_open': 'true' },
376+
}),
377+
).toThrow('unexpected')
378+
})
379+
})
309380
})

0 commit comments

Comments
 (0)