Skip to content

Commit 9a1aa92

Browse files
test(query-devtools/contexts/PiPContext): add tests for the '#_goober' MutationObserver observing and mirroring parent style mutations (TanStack#10804)
* test(query-devtools/contexts/PiPContext): add tests for the '#_goober' MutationObserver observing and mirroring parent style mutations * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent db19ccf commit 9a1aa92

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,79 @@ describe('PiPContext', () => {
377377
).toThrow('unexpected')
378378
})
379379
})
380+
381+
describe('"#_goober" MutationObserver', () => {
382+
function stubMutationObserver() {
383+
let observerCallback: MutationCallback | undefined
384+
const observeSpy = vi.fn()
385+
const disconnectSpy = vi.fn()
386+
class FakeMutationObserver {
387+
observe = observeSpy
388+
disconnect = disconnectSpy
389+
takeRecords = () => []
390+
constructor(cb: MutationCallback) {
391+
observerCallback = cb
392+
}
393+
}
394+
vi.stubGlobal('MutationObserver', FakeMutationObserver)
395+
return {
396+
observeSpy,
397+
disconnectSpy,
398+
fire: () => observerCallback?.([], {} as MutationObserver),
399+
}
400+
}
401+
402+
it('should observe the parent "#_goober" style for childList and subtree mutations', () => {
403+
const gooberStyle = document.createElement('style')
404+
gooberStyle.id = '_goober'
405+
gooberStyle.textContent = '.initial { color: red; }'
406+
document.head.appendChild(gooberStyle)
407+
const { observeSpy } = stubMutationObserver()
408+
stubPipWindow()
409+
410+
try {
411+
renderAndAct((pip) => pip().requestPipWindow(640, 480), {
412+
disabled: true,
413+
})
414+
415+
expect(observeSpy).toHaveBeenCalledWith(
416+
gooberStyle,
417+
expect.objectContaining({ childList: true, subtree: true }),
418+
)
419+
} finally {
420+
gooberStyle.remove()
421+
}
422+
})
423+
424+
it('should copy parent "#_goober" "textContent" into the PiP mirror when the observer fires', () => {
425+
const gooberStyle = document.createElement('style')
426+
gooberStyle.id = '_goober'
427+
gooberStyle.textContent = '.initial { color: red; }'
428+
document.head.appendChild(gooberStyle)
429+
const { fire } = stubMutationObserver()
430+
const { pipDocument } = stubPipWindow()
431+
432+
try {
433+
const pipGooberStyle = pipDocument.createElement('style')
434+
pipGooberStyle.id = '_goober'
435+
436+
renderAndAct(
437+
(pip) => {
438+
pip().requestPipWindow(640, 480)
439+
// `requestPipWindow` clears the PiP document head, so install
440+
// the goober mirror style after that point.
441+
pipDocument.head.appendChild(pipGooberStyle)
442+
},
443+
{ disabled: true },
444+
)
445+
446+
gooberStyle.textContent = '.next { color: blue; }'
447+
fire()
448+
449+
expect(pipGooberStyle.textContent).toBe('.next { color: blue; }')
450+
} finally {
451+
gooberStyle.remove()
452+
}
453+
})
454+
})
380455
})

0 commit comments

Comments
 (0)