Open History to see the last commit
1import { test, expect } from '@playwright/test'
2import { DevtoolsPage, SELECTORS } from '@tanstack/devtools-e2e'
3β
4/**
5 * The devtools shell opens picture-in-picture by calling
6 * `window.open('', 'TSDT-Devtools-Panel', ...)` from inside the PiP button's
7 * click handler (see packages/devtools/src/context/pip-context.tsx). It then
8 * touches the returned window's `.document` (head/body innerHTML, title,
9 * body.style.margin), copies stylesheets via `.document.head.appendChild(...)`,
10 * registers a `pagehide` listener via `.addEventListener`, and may call
11 * `.close()`.
12 *
13 * Rather than drive a real popup, we stub `window.open` before the app loads so
14 * that:
15 * - a flag (`__pipRequested`) records that a PiP window was requested, and
16 * - a minimal fake window is returned that satisfies every property the shell
17 * accesses without throwing.
18 *
19 * The stable, meaningful assertion is simply that the request was made.
20 */
21test('clicking the PiP button requests a picture-in-picture window', async ({
22 page,
23}) => {
24 await page.addInitScript(() => {
25 const originalOpen = window.open.bind(window)
26 // Keep a reference in case anything wants the real implementation back.
27 ;(window as any).__originalOpen = originalOpen
28β
29 // A no-op DOM-ish node that swallows whatever the shell does to it.
30 const makeFakeNode = () => {
31 const node: any = {
32 innerHTML: '',
33 textContent: '',
34 style: {} as Record<string, string>,
35 setAttribute: () => {},
36 appendChild: (child: unknown) => child,
37 removeChild: (child: unknown) => child,
38 }
39 return node
40 }
41β
42 const fakeDocument: any = {
43 head: makeFakeNode(),
44 body: makeFakeNode(),
45 title: '',
46 styleSheets: [],
47 createElement: () => makeFakeNode(),
48 querySelector: () => null,
49 querySelectorAll: () => [],
50 addEventListener: () => {},
51 removeEventListener: () => {},
52 }
53β
54 const fakeWindow: any = {
55 document: fakeDocument,
56 addEventListener: () => {},
57 removeEventListener: () => {},
58 close: () => {},
59 focus: () => {},
60 location: { href: '' },
61 }
62β
63 window.open = ((..._args: Array<unknown>) => {
64 ;(window as any).__pipRequested = true
65 return fakeWindow
66 }) as typeof window.open
67 })
68β
69 const dt = new DevtoolsPage(page)
70 await dt.goto()
71 await dt.openViaTrigger()
72β
73 await page.getByTestId(SELECTORS.pipButton).click()
74β
75 await expect
76 .poll(() => page.evaluate(() => (window as any).__pipRequested))
77 .toBe(true)
78})