-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathCardRoot.test.tsx
More file actions
265 lines (230 loc) · 9.46 KB
/
Copy pathCardRoot.test.tsx
File metadata and controls
265 lines (230 loc) · 9.46 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { ClerkInstanceContext } from '@clerk/shared/react';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { beforeEach, describe, expect, it } from 'vitest';
import { EnvironmentProvider } from '@/contexts';
import { AppearanceProvider, useAppearance } from '@/customizables';
import type { ParsedAppearance } from '@/customizables/parseAppearance';
import { FlowMetadataProvider } from '@/elements/contexts';
import { ModalContext } from '@/elements/Modal';
import { InternalThemeProvider } from '@/styledSystem';
import { CardFooter } from '../CardFooter';
import { CardRoot } from '../CardRoot';
let captured: ParsedAppearance;
const AppearanceCapture = () => {
captured = useAppearance();
return null;
};
const hasFlushShape = (elements: ParsedAppearance['parsedElements']) =>
elements.some(el => el.cardBox?.boxShadow === 'none' && el.card?.backgroundColor === 'transparent');
type RenderCardOptions = {
elevation?: 'raised' | 'flush';
globalAppearance?: Record<string, unknown>;
appearance?: Record<string, unknown>;
withModal?: boolean;
};
const renderCard = ({ elevation, globalAppearance, appearance, withModal }: RenderCardOptions = {}) => {
const card = (
<CardRoot elevation={elevation}>
<AppearanceCapture />
</CardRoot>
);
return render(
<AppearanceProvider
appearanceKey='signIn'
globalAppearance={globalAppearance as any}
appearance={appearance as any}
>
<InternalThemeProvider>
<FlowMetadataProvider flow='signIn'>
{withModal ? <ModalContext.Provider value={{ value: {} }}>{card}</ModalContext.Provider> : card}
</FlowMetadataProvider>
</InternalThemeProvider>
</AppearanceProvider>,
);
};
const devEnvironment = {
displayConfig: {
branded: false,
showDevModeWarning: true,
},
isDevelopmentOrStaging: () => true,
};
const renderCardWithFooter = ({ appearance }: Pick<RenderCardOptions, 'appearance'> = {}) => {
return render(
<ClerkInstanceContext.Provider value={{ value: { client: {}, user: {} } as any }}>
<EnvironmentProvider value={devEnvironment as any}>
<AppearanceProvider
appearanceKey='signIn'
appearance={appearance as any}
>
<InternalThemeProvider>
<FlowMetadataProvider flow='signIn'>
<CardRoot>
<CardFooter />
</CardRoot>
</FlowMetadataProvider>
</InternalThemeProvider>
</AppearanceProvider>
</EnvironmentProvider>
</ClerkInstanceContext.Provider>,
);
};
describe('CardRoot augmentedAppearance — raised (default)', () => {
beforeEach(() => {
captured = undefined as any;
});
it('children see original parsedElements when elevation is raised', () => {
renderCard();
expect(hasFlushShape(captured.parsedElements)).toBe(false);
});
it('explicit raised prop overrides flush option', () => {
renderCard({
elevation: 'raised',
appearance: { options: { elevation: 'flush' } },
});
expect(hasFlushShape(captured.parsedElements)).toBe(false);
});
});
describe('CardRoot augmentedAppearance — flush', () => {
beforeEach(() => {
captured = undefined as any;
});
it('flush overrides appear at index 1 after base theme', () => {
renderCard({ elevation: 'flush' });
expect(captured.parsedElements[1].cardBox?.boxShadow).toBe('none');
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
expect(captured.parsedElements[1].footer?.paddingTop).toBe(0);
});
it('preserves parsedInternalTheme, parsedOptions, and parsedCaptcha through augmentation', () => {
renderCard({ elevation: 'raised' });
const { parsedInternalTheme: raisedTheme, parsedOptions: raisedOptions, parsedCaptcha: raisedCaptcha } = captured;
renderCard({ elevation: 'flush' });
expect(captured.parsedInternalTheme).toEqual(raisedTheme);
expect(captured.parsedOptions).toEqual(raisedOptions);
expect(captured.parsedCaptcha).toEqual(raisedCaptcha);
});
it('parsedElements length increases by 1 when flush is active', () => {
renderCard({ elevation: 'raised' });
const raisedLength = captured.parsedElements.length;
renderCard({ elevation: 'flush' });
expect(captured.parsedElements.length).toBe(raisedLength + 1);
});
it('user appearance.elements.card appears after flush overrides', () => {
renderCard({
elevation: 'flush',
appearance: { elements: { card: { backgroundColor: 'hotpink' } } },
});
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
expect(captured.parsedElements[2].card?.backgroundColor).toBe('hotpink');
});
it('globalAppearance.elements appear after flush overrides', () => {
renderCard({
elevation: 'flush',
globalAppearance: { elements: { card: { backgroundColor: 'hotpink' } } },
});
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
expect(captured.parsedElements[2].card?.backgroundColor).toBe('hotpink');
});
it('both global and component element layers are preserved in order after flush', () => {
renderCard({
elevation: 'flush',
globalAppearance: { elements: { card: { color: 'blue' } } },
appearance: { elements: { card: { color: 'red' } } },
});
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
expect(captured.parsedElements[2].card?.color).toBe('blue');
expect(captured.parsedElements[3].card?.color).toBe('red');
});
it('elevation via appearance.options activates flush', () => {
renderCard({
appearance: { options: { elevation: 'flush' } },
});
expect(captured.parsedElements[1].cardBox?.boxShadow).toBe('none');
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
});
it('user string classname elements are preserved after flush overrides', () => {
renderCard({
elevation: 'flush',
appearance: { elements: { card: 'my-custom-class' } },
});
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
expect(captured.parsedElements[2].card).toBe('my-custom-class');
});
it('user footer overrides with nested selectors appear after flush footer', () => {
renderCard({
elevation: 'flush',
appearance: {
elements: {
footer: {
background: 'blue',
'>:first-of-type': { padding: '16px' },
},
},
},
});
expect(captured.parsedElements[1].footer?.background).toBe('transparent');
expect(captured.parsedElements[1].footer?.['>:first-of-type']?.padding).toBe(0);
expect(captured.parsedElements[2].footer?.background).toBe('blue');
expect(captured.parsedElements[2].footer?.['>:first-of-type']?.padding).toBe('16px');
});
it('adds flush-only dev mode indicator overrides', () => {
renderCard({ elevation: 'flush' });
expect(captured.parsedElements[1].footer?.['& [data-clerk-dev-mode-overlay]']?.display).toBe('none');
expect(captured.parsedElements[1].footer?.['& [data-clerk-dev-mode-notice]']?.textAlign).toBe('center');
expect(captured.parsedElements[1].footer?.['& [data-clerk-dev-mode-notice]']?.padding).toBe(0);
});
});
describe('CardRoot dev mode indicator rendering', () => {
it('raised cards render both the patterned overlay and development mode text markers', () => {
const { container } = renderCardWithFooter();
const notice = screen.getByText('Development mode');
expect(notice.hasAttribute('data-clerk-dev-mode-notice')).toBe(true);
expect(container.querySelector('[data-clerk-dev-mode-overlay]')).not.toBeNull();
});
it('flush cards keep the development mode text and hide the patterned overlay through flush styling', () => {
const { container } = renderCardWithFooter({ appearance: { options: { elevation: 'flush' } } });
const notice = screen.getByText('Development mode');
const overlay = container.querySelector('[data-clerk-dev-mode-overlay]');
expect(notice.hasAttribute('data-clerk-dev-mode-notice')).toBe(true);
expect(overlay).not.toBeNull();
expect(getComputedStyle(overlay as HTMLElement).display).toBe('none');
});
});
describe('CardRoot elevation resolution priority', () => {
beforeEach(() => {
captured = undefined as any;
});
it('modal context forces raised even when option is flush', () => {
renderCard({
withModal: true,
appearance: { options: { elevation: 'flush' } },
});
expect(hasFlushShape(captured.parsedElements)).toBe(false);
});
it('explicit flush prop overrides modal context', () => {
renderCard({
elevation: 'flush',
withModal: true,
});
expect(captured.parsedElements[1].cardBox?.boxShadow).toBe('none');
expect(captured.parsedElements[1].card?.backgroundColor).toBe('transparent');
});
});
describe('CardRoot data-elevation attribute', () => {
it('data-elevation is absent when raised', () => {
const { container } = renderCard();
expect(container.querySelector('[data-elevation]')).toBeNull();
});
it('data-elevation is flush when elevation is flush', () => {
const { container } = renderCard({ elevation: 'flush' });
expect(container.querySelector('[data-elevation="flush"]')).not.toBeNull();
});
it('data-elevation is absent when modal forces raised', () => {
const { container } = renderCard({
withModal: true,
appearance: { options: { elevation: 'flush' } },
});
expect(container.querySelector('[data-elevation]')).toBeNull();
});
});