|
| 1 | +// eslint-disable-next-line no-restricted-imports |
| 2 | +import type { StylisElement } from '@emotion/cache'; |
| 3 | + |
| 4 | +import { getInsertionPoint, wrapInLayer } from '../StyleCacheProvider'; |
| 5 | + |
| 6 | +// Mock the StylisPlugin type |
| 7 | +type MockStylisElement = Partial<StylisElement> & { |
| 8 | + type: string; |
| 9 | + props: string[]; |
| 10 | + children: MockStylisElement[]; |
| 11 | + parent: MockStylisElement | null; |
| 12 | + root: MockStylisElement | null; |
| 13 | + length: number; |
| 14 | + value: string; |
| 15 | + return: string; |
| 16 | + line: number; |
| 17 | + column: number; |
| 18 | +}; |
| 19 | + |
| 20 | +describe('wrapInLayer', () => { |
| 21 | + it('wraps CSS rules in a CSS layer', () => { |
| 22 | + const node: MockStylisElement = { |
| 23 | + type: 'rule', |
| 24 | + props: ['body'], |
| 25 | + children: [], |
| 26 | + parent: null, |
| 27 | + root: null, |
| 28 | + length: 0, |
| 29 | + value: '', |
| 30 | + return: '', |
| 31 | + line: 1, |
| 32 | + column: 1, |
| 33 | + }; |
| 34 | + |
| 35 | + const plugin = wrapInLayer('test-layer'); |
| 36 | + // @ts-expect-error - We're mocking the StylisPlugin type |
| 37 | + plugin(node); |
| 38 | + |
| 39 | + expect(node.type).toBe('@layer'); |
| 40 | + expect(node.props).toEqual(['test-layer']); |
| 41 | + expect(node.children).toHaveLength(1); |
| 42 | + const child = node.children[0]; |
| 43 | + expect(child.props).toEqual(['body']); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not wrap if node has a parent', () => { |
| 47 | + const node: MockStylisElement = { |
| 48 | + type: 'rule', |
| 49 | + props: ['body'], |
| 50 | + children: [], |
| 51 | + parent: { type: 'parent' } as MockStylisElement, |
| 52 | + root: null, |
| 53 | + length: 0, |
| 54 | + value: '', |
| 55 | + return: '', |
| 56 | + line: 1, |
| 57 | + column: 1, |
| 58 | + }; |
| 59 | + |
| 60 | + const originalNode = { ...node }; |
| 61 | + const plugin = wrapInLayer('test-layer'); |
| 62 | + // @ts-expect-error - We're mocking the StylisPlugin type |
| 63 | + plugin(node); |
| 64 | + |
| 65 | + expect(node).toEqual(originalNode); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('getInsertionPoint', () => { |
| 70 | + beforeEach(() => { |
| 71 | + document.head.innerHTML = ''; |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns meta tag if found', () => { |
| 75 | + const meta = document.createElement('meta'); |
| 76 | + meta.setAttribute('name', 'emotion-insertion-point'); |
| 77 | + document.head.appendChild(meta); |
| 78 | + |
| 79 | + const result = getInsertionPoint(); |
| 80 | + expect(result).toBe(meta); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns style tag if found', () => { |
| 84 | + const style = document.createElement('style'); |
| 85 | + style.id = 'cl-style-insertion-point'; |
| 86 | + document.head.appendChild(style); |
| 87 | + |
| 88 | + const result = getInsertionPoint(); |
| 89 | + expect(result).toBe(style); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns null if no insertion point found', () => { |
| 93 | + const result = getInsertionPoint(); |
| 94 | + expect(result).toBeNull(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments