Skip to content

Commit 2c23dbf

Browse files
committed
add tests
1 parent 1e594b7 commit 2c23dbf

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

packages/clerk-js/src/ui/styledSystem/StyleCacheProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import React, { useEffect, useMemo, useState } from 'react';
1111
* @param layerName - The name of the CSS layer to wrap the styles in
1212
* @returns A Stylis plugin function
1313
*/
14-
const wrapInLayer: (layerName: string) => StylisPlugin = layerName => node => {
14+
export const wrapInLayer: (layerName: string) => StylisPlugin = layerName => node => {
1515
// if we're not at the root of a <style> tag, leave the tree intact
1616
if (node.parent) return;
1717

@@ -33,7 +33,7 @@ const wrapInLayer: (layerName: string) => StylisPlugin = layerName => node => {
3333
* Finds the appropriate insertion point for Emotion styles in the DOM
3434
* @returns The HTMLElement to use as insertion point, or undefined if none found
3535
*/
36-
const getInsertionPoint = (): HTMLElement | undefined => {
36+
export const getInsertionPoint = (): HTMLElement | undefined => {
3737
try {
3838
const metaTag = document.querySelector('meta[name="emotion-insertion-point"]');
3939
if (metaTag) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)