|
6 | 6 | getMutationStatusColor, |
7 | 7 | getQueryStatusColorByLabel, |
8 | 8 | getSidedProp, |
| 9 | + setupStyleSheet, |
9 | 10 | updateNestedDataByPath, |
10 | 11 | } from '../utils' |
11 | 12 | import type { MutationStatus } from '@tanstack/query-core' |
@@ -887,4 +888,63 @@ describe('Utils tests', () => { |
887 | 888 | expect(convertRemToPixels(1)).toBe(15.5) |
888 | 889 | }) |
889 | 890 | }) |
| 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 | + }) |
890 | 950 | }) |
0 commit comments