generated from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgenStyleUtils.test.tsx
More file actions
215 lines (187 loc) · 6.77 KB
/
genStyleUtils.test.tsx
File metadata and controls
215 lines (187 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React from 'react';
import { render, renderHook } from '@testing-library/react';
import { genStyleUtils } from '../src';
import type { CSSVarRegisterProps, SubStyleComponentProps } from '@/util/genStyleUtils';
import { createCache, StyleProvider, useCSSVarRegister } from '@ant-design/cssinjs';
jest.mock('@ant-design/cssinjs', () => ({
...jest.requireActual('@ant-design/cssinjs'),
useCSSVarRegister: jest.fn(),
}));
interface TestCompTokenMap {
TestComponent: object;
}
describe('genStyleUtils', () => {
const mockConfig = {
usePrefix: jest.fn().mockReturnValue({
rootPrefixCls: 'ant',
iconPrefixCls: 'anticon',
}),
useToken: jest.fn().mockReturnValue({
theme: {},
realToken: {},
hashId: 'hash',
token: {},
cssVar: {},
}),
useCSP: jest.fn().mockReturnValue({ nonce: 'nonce' }),
getResetStyles: jest.fn().mockReturnValue([]),
layer: {
name: 'test',
dependencies: ['parent'],
},
};
const { genStyleHooks, genSubStyleComponent, genComponentStyleHook } = genStyleUtils<
TestCompTokenMap,
object,
object
>(mockConfig);
beforeEach(() => {
// Clear head style
const head = document.head;
head.innerHTML = '';
jest.clearAllMocks();
});
describe('genStyleHooks', () => {
it('should generate style hooks', () => {
const component = 'TestComponent';
const styleFn = jest.fn();
const getDefaultToken = {
mockCompToken: 'mock',
};
const hooks = genStyleHooks(component, styleFn, getDefaultToken);
expect(hooks).toBeInstanceOf(Function);
const {
result: { current },
} = renderHook(() => hooks('test-prefix'));
expect(current).toBeInstanceOf(Array);
expect(current).toHaveLength(2);
});
it('should inject CSS vars for extraCssVarPrefixCls', () => {
const component = 'TestComponent';
const styleFn = jest.fn();
const getDefaultToken = jest.fn();
const hooks = genStyleHooks(component, styleFn, getDefaultToken, {
extraCssVarPrefixCls: ['custom-a', 'custom-b'],
});
renderHook(() => hooks('test-prefix'));
// useCSSVarRegister should be called 3 times: test-prefix, custom-a, custom-b
expect(useCSSVarRegister).toHaveBeenCalledTimes(3);
expect(useCSSVarRegister).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ scope: 'test-prefix' }),
expect.any(Function),
);
expect(useCSSVarRegister).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ scope: 'custom-a' }),
expect.any(Function),
);
expect(useCSSVarRegister).toHaveBeenNthCalledWith(
3,
expect.objectContaining({ scope: 'custom-b' }),
expect.any(Function),
);
});
});
describe('genSubStyleComponent', () => {
it('should generate sub style component', () => {
const component = 'TestComponent';
const styleFn = jest.fn();
const getDefaultToken = jest.fn();
const StyledComponent = genSubStyleComponent(component, styleFn, getDefaultToken);
const { container } = render(<StyledComponent prefixCls="test-prefix" rootCls="test-root" />);
expect(container).toBeEmptyDOMElement();
});
});
describe('genComponentStyleHook', () => {
it('should generate component style hook', () => {
const component = 'TestComponent';
const styleFn = jest.fn();
const getDefaultToken = jest.fn();
const hook = genComponentStyleHook(component, styleFn, getDefaultToken);
const TestComponent: React.FC<SubStyleComponentProps> = ({ prefixCls, rootCls }) => {
hook(prefixCls, rootCls);
return <div data-testid="test-component">Test</div>;
};
const { getByTestId } = render(<TestComponent prefixCls="test-prefix" rootCls="test-root" />);
expect(getByTestId('test-component')).toHaveTextContent('Test');
});
});
describe('genComponentStyleHook should run without getResetStyles', () => {
it('should generate component style hook', () => {
const component = 'TestComponent';
const styleFn = jest.fn();
const getDefaultToken = jest.fn();
const { getResetStyles, ...restMockConfig } = mockConfig;
const { genComponentStyleHook: genComponentStyleHookWithoutReset } = genStyleUtils<
TestCompTokenMap,
object,
object
>(restMockConfig);
const hook = genComponentStyleHookWithoutReset(component, styleFn, getDefaultToken);
const TestComponent: React.FC<SubStyleComponentProps> = ({ prefixCls, rootCls }) => {
hook(prefixCls, rootCls);
return <div data-testid="test-component">Test</div>;
};
const { container } = render(<TestComponent prefixCls="test-prefix" rootCls="test-root" />);
expect(() => container).not.toThrow();
});
});
describe('CSSVarRegister', () => {
it('should render CSSVarRegister component', () => {
const CSSVarRegister: React.FC<CSSVarRegisterProps> = ({ rootCls, cssVar = {} }) => {
return <div data-testid={rootCls}>{cssVar.prefix}</div>;
};
const { getByTestId } = render(
<CSSVarRegister rootCls="test-root" cssVar={{ prefix: 'test-prefix' }} component="test" />,
);
expect(getByTestId('test-root')).toHaveTextContent('test-prefix');
});
});
it('layer', () => {
const StyledComponent = genSubStyleComponent(
'TestComponent',
() => ({}),
() => ({}),
);
render(
<StyleProvider cache={createCache()} layer>
<StyledComponent prefixCls="test" />
</StyleProvider>,
);
expect(document.head.innerHTML).toContain('@layer parent,test;');
});
describe('disabledRuntimeStyle', () => {
it('should work', () => {
const usePrefix = jest.fn().mockReturnValue({
rootPrefixCls: 'ant',
iconPrefixCls: 'anticon',
});
const config = {
...mockConfig,
useToken: jest.fn().mockReturnValue({
theme: {},
realToken: {},
hashId: 'hash',
token: {},
cssVar: {},
zeroRuntime: true,
}),
usePrefix,
};
const { genComponentStyleHook: gen } = genStyleUtils<TestCompTokenMap, object, object>(
config,
);
const styleFn = jest.fn();
const getDefaultToken = jest.fn();
const useStyle = gen('TestComponent', styleFn, getDefaultToken);
const TestComponent: React.FC<SubStyleComponentProps> = ({ prefixCls, rootCls }) => {
useStyle(prefixCls, rootCls);
return <div data-testid="test-component">Test</div>;
};
const { getByTestId } = render(<TestComponent prefixCls="test-prefix" rootCls="test-root" />);
expect(getByTestId('test-component')).toHaveTextContent('Test');
expect(usePrefix).not.toHaveBeenCalled();
});
});
});