Skip to content

Commit 1fe4036

Browse files
committed
test(query-devtools/utils): add tests for 'setupStyleSheet'
1 parent 24d0834 commit 1fe4036

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

packages/query-devtools/src/__tests__/utils.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getMutationStatusColor,
77
getQueryStatusColorByLabel,
88
getSidedProp,
9+
setupStyleSheet,
910
updateNestedDataByPath,
1011
} from '../utils'
1112
import type { MutationStatus } from '@tanstack/query-core'
@@ -887,4 +888,63 @@ describe('Utils tests', () => {
887888
expect(convertRemToPixels(1)).toBe(15.5)
888889
})
889890
})
891+
892+
describe('setupStyleSheet', () => {
893+
afterEach(() => {
894+
document.head.querySelector('#_goober')?.remove()
895+
})
896+
897+
it('should not insert any style tag when "nonce" is missing', () => {
898+
setupStyleSheet()
899+
900+
expect(document.head.querySelector('#_goober')).toBeNull()
901+
})
902+
903+
it('should append a style tag with id "_goober" to "document.head"', () => {
904+
setupStyleSheet('test-nonce')
905+
906+
const styleTag = document.head.querySelector('#_goober')
907+
expect(styleTag).not.toBeNull()
908+
expect(styleTag?.tagName).toBe('STYLE')
909+
})
910+
911+
it('should set the "nonce" attribute on the inserted style tag', () => {
912+
setupStyleSheet('test-nonce')
913+
914+
expect(
915+
document.head.querySelector('#_goober')?.getAttribute('nonce'),
916+
).toBe('test-nonce')
917+
})
918+
919+
it('should not insert a duplicate style tag when "document.head" already has one', () => {
920+
setupStyleSheet('first-nonce')
921+
setupStyleSheet('second-nonce')
922+
923+
const styleTags = document.head.querySelectorAll('#_goober')
924+
expect(styleTags).toHaveLength(1)
925+
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
926+
})
927+
928+
it('should append the style tag to the provided "ShadowRoot" target', () => {
929+
const host = document.createElement('div')
930+
const shadow = host.attachShadow({ mode: 'open' })
931+
932+
setupStyleSheet('test-nonce', shadow)
933+
934+
expect(shadow.querySelector('#_goober')).not.toBeNull()
935+
expect(document.head.querySelector('#_goober')).toBeNull()
936+
})
937+
938+
it('should not insert a duplicate style tag when the target already has one', () => {
939+
const host = document.createElement('div')
940+
const shadow = host.attachShadow({ mode: 'open' })
941+
942+
setupStyleSheet('first-nonce', shadow)
943+
setupStyleSheet('second-nonce', shadow)
944+
945+
const styleTags = shadow.querySelectorAll('#_goober')
946+
expect(styleTags).toHaveLength(1)
947+
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
948+
})
949+
})
890950
})

0 commit comments

Comments
 (0)