Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/query-devtools/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getMutationStatusColor,
getQueryStatusColorByLabel,
getSidedProp,
setupStyleSheet,
updateNestedDataByPath,
} from '../utils'
import type { MutationStatus } from '@tanstack/query-core'
Expand Down Expand Up @@ -887,4 +888,69 @@ describe('Utils tests', () => {
expect(convertRemToPixels(1)).toBe(15.5)
})
})

describe('setupStyleSheet', () => {
afterEach(() => {
document.head.querySelector('#_goober')?.remove()
})

it('should not insert any style tag when "nonce" is missing', () => {
setupStyleSheet()

expect(document.head.querySelector('#_goober')).toBeNull()
})

it('should not insert any style tag when "nonce" is an empty string', () => {
setupStyleSheet('')

expect(document.head.querySelector('#_goober')).toBeNull()
})

it('should append a style tag with id "_goober" to "document.head"', () => {
setupStyleSheet('test-nonce')

const styleTag = document.head.querySelector('#_goober')
expect(styleTag).not.toBeNull()
expect(styleTag?.tagName).toBe('STYLE')
})

it('should set the "nonce" attribute on the inserted style tag', () => {
setupStyleSheet('test-nonce')

expect(
document.head.querySelector('#_goober')?.getAttribute('nonce'),
).toBe('test-nonce')
})

it('should not insert a duplicate style tag when "document.head" already has one', () => {
setupStyleSheet('first-nonce')
setupStyleSheet('second-nonce')

const styleTags = document.head.querySelectorAll('#_goober')
expect(styleTags).toHaveLength(1)
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
})

it('should append the style tag to the provided "ShadowRoot" target', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })

setupStyleSheet('test-nonce', shadow)

expect(shadow.querySelector('#_goober')).not.toBeNull()
expect(document.head.querySelector('#_goober')).toBeNull()
})

it('should not insert a duplicate style tag when the target already has one', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })

setupStyleSheet('first-nonce', shadow)
setupStyleSheet('second-nonce', shadow)

const styleTags = shadow.querySelectorAll('#_goober')
expect(styleTags).toHaveLength(1)
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
})
Comment on lines +934 to +954
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Missing regression test for head-vs-shadow precedence bug.

On Line 932/Line 942, ShadowRoot behavior is only tested when document.head is clean. In packages/query-devtools/src/utils.tsx (Lines 305-323), styleExists checks document.querySelector('#_goober') first, so an existing head tag can incorrectly block insertion into the provided ShadowRoot target. Please add a test that seeds document.head first, then asserts target insertion still occurs.

Suggested test addition
   it('should not insert a duplicate style tag when the target already has one', () => {
     const host = document.createElement('div')
     const shadow = host.attachShadow({ mode: 'open' })

     setupStyleSheet('first-nonce', shadow)
     setupStyleSheet('second-nonce', shadow)

     const styleTags = shadow.querySelectorAll('#_goober')
     expect(styleTags).toHaveLength(1)
     expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
   })
+
+  it('should still append to ShadowRoot when document.head already has "_goober"', () => {
+    setupStyleSheet('head-nonce')
+    const host = document.createElement('div')
+    const shadow = host.attachShadow({ mode: 'open' })
+
+    setupStyleSheet('shadow-nonce', shadow)
+
+    expect(document.head.querySelectorAll('#_goober')).toHaveLength(1)
+    expect(shadow.querySelectorAll('#_goober')).toHaveLength(1)
+    expect(shadow.querySelector('#_goober')?.getAttribute('nonce')).toBe(
+      'shadow-nonce',
+    )
+  })
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('should append the style tag to the provided "ShadowRoot" target', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })
setupStyleSheet('test-nonce', shadow)
expect(shadow.querySelector('#_goober')).not.toBeNull()
expect(document.head.querySelector('#_goober')).toBeNull()
})
it('should not insert a duplicate style tag when the target already has one', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })
setupStyleSheet('first-nonce', shadow)
setupStyleSheet('second-nonce', shadow)
const styleTags = shadow.querySelectorAll('#_goober')
expect(styleTags).toHaveLength(1)
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
})
it('should append the style tag to the provided "ShadowRoot" target', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })
setupStyleSheet('test-nonce', shadow)
expect(shadow.querySelector('#_goober')).not.toBeNull()
expect(document.head.querySelector('#_goober')).toBeNull()
})
it('should not insert a duplicate style tag when the target already has one', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })
setupStyleSheet('first-nonce', shadow)
setupStyleSheet('second-nonce', shadow)
const styleTags = shadow.querySelectorAll('#_goober')
expect(styleTags).toHaveLength(1)
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
})
it('should still append to ShadowRoot when document.head already has "_goober"', () => {
setupStyleSheet('head-nonce')
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })
setupStyleSheet('shadow-nonce', shadow)
expect(document.head.querySelectorAll('#_goober')).toHaveLength(1)
expect(shadow.querySelectorAll('#_goober')).toHaveLength(1)
expect(shadow.querySelector('#_goober')?.getAttribute('nonce')).toBe(
'shadow-nonce',
)
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/query-devtools/src/__tests__/utils.test.ts` around lines 928 - 948,
Add a regression test that ensures a pre-existing head style tag does not block
insertion into a provided ShadowRoot: create and append a style element with id
"_goober" and a known nonce to document.head, then create a host with
attachShadow and call setupStyleSheet('test-nonce', shadow); assert that
shadow.querySelector('#_goober') is not null (and has the expected nonce if
relevant) and that document.head still contains the original style (so insertion
happened into the shadow target despite the head entry). Also add a variant that
calls setupStyleSheet twice (first seeding head, then calling setupStyleSheet on
the same shadow) to ensure no duplicate style is added inside the ShadowRoot
while preserving the head tag; reference setupStyleSheet and the internal style
id "_goober" (and styleExists in utils.tsx) when locating the behavior to test.

})
})
Loading