Skip to content

Commit 3bfa156

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

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

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

Lines changed: 76 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,79 @@ 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 not insert any style tag when "nonce" is an empty string', () => {
904+
setupStyleSheet('')
905+
906+
expect(document.head.querySelector('#_goober')).toBeNull()
907+
})
908+
909+
it('should append a style tag with id "_goober" to "document.head"', () => {
910+
setupStyleSheet('test-nonce')
911+
912+
const styleTag = document.head.querySelector('#_goober')
913+
expect(styleTag).not.toBeNull()
914+
expect(styleTag?.tagName).toBe('STYLE')
915+
})
916+
917+
it('should set the "nonce" attribute on the inserted style tag', () => {
918+
setupStyleSheet('test-nonce')
919+
920+
expect(
921+
document.head.querySelector('#_goober')?.getAttribute('nonce'),
922+
).toBe('test-nonce')
923+
})
924+
925+
it('should not insert a duplicate style tag when "document.head" already has one', () => {
926+
setupStyleSheet('first-nonce')
927+
setupStyleSheet('second-nonce')
928+
929+
const styleTags = document.head.querySelectorAll('#_goober')
930+
expect(styleTags).toHaveLength(1)
931+
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
932+
})
933+
934+
it('should append the style tag to the provided "ShadowRoot" target', () => {
935+
const host = document.createElement('div')
936+
const shadow = host.attachShadow({ mode: 'open' })
937+
938+
setupStyleSheet('test-nonce', shadow)
939+
940+
expect(shadow.querySelector('#_goober')).not.toBeNull()
941+
expect(document.head.querySelector('#_goober')).toBeNull()
942+
})
943+
944+
it('should not insert a duplicate style tag when the target already has one', () => {
945+
const host = document.createElement('div')
946+
const shadow = host.attachShadow({ mode: 'open' })
947+
948+
setupStyleSheet('first-nonce', shadow)
949+
setupStyleSheet('second-nonce', shadow)
950+
951+
const styleTags = shadow.querySelectorAll('#_goober')
952+
expect(styleTags).toHaveLength(1)
953+
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
954+
})
955+
956+
it('should not insert into the target when "document.head" already has a style tag', () => {
957+
const host = document.createElement('div')
958+
const shadow = host.attachShadow({ mode: 'open' })
959+
960+
setupStyleSheet('first-nonce')
961+
setupStyleSheet('second-nonce', shadow)
962+
963+
expect(shadow.querySelector('#_goober')).toBeNull()
964+
})
965+
})
890966
})

0 commit comments

Comments
 (0)