-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
/
makeStyles.test.js
568 lines (501 loc) · 17.2 KB
/
makeStyles.test.js
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import { assert } from 'chai';
import React from 'react';
import PropTypes from 'prop-types';
import { SheetsRegistry } from 'jss';
import { act } from 'react-dom/test-utils';
import { createMount } from '@material-ui/core/test-utils';
import { createMuiTheme } from '@material-ui/core/styles';
import createGenerateClassName from '../createGenerateClassName';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import makeStyles from './makeStyles';
import useTheme from '../useTheme';
import StylesProvider from '../StylesProvider';
import ThemeProvider from '../ThemeProvider';
describe('makeStyles', () => {
let mount;
/**
* returns a function that given the props for the styles object will return
* the css classes
* @param {object} styles argument for `makeStyles`
*/
function createGetClasses(styles) {
const useStyles = makeStyles(styles);
const output = {};
function TestComponent(props) {
output.classes = useStyles(props);
return <div />;
}
return function mountWithProps(props) {
const wrapper = mount(<TestComponent {...props} />);
output.wrapper = wrapper;
return output;
};
}
let generateClassName;
before(() => {
mount = createMount({ strict: undefined });
});
beforeEach(() => {
generateClassName = createGenerateClassName();
});
after(() => {
mount.cleanUp();
});
it('should accept a classes prop', () => {
const styles = { root: {} };
const mountWithProps = createGetClasses(styles);
const output = mountWithProps();
const baseClasses = output.classes;
output.wrapper.setProps({
classes: { root: 'h1' },
});
const extendedClasses = output.classes;
assert.strictEqual(extendedClasses.root, `${baseClasses.root} h1`);
});
it('should ignore undefined prop', () => {
const styles = { root: {} };
const mountWithProps = createGetClasses(styles);
const output = mountWithProps();
const baseClasses = output.classes;
output.wrapper.setProps({
classes: { root: undefined },
});
const extendedClasses = output.classes;
assert.strictEqual(extendedClasses.root, baseClasses.root);
});
describe('warnings', () => {
const mountWithProps = createGetClasses({ root: {} });
beforeEach(() => {
consoleErrorMock.spy();
});
afterEach(() => {
consoleErrorMock.reset();
});
it('should warn if providing a unknown key', () => {
const output = mountWithProps();
const baseClasses = output.classes;
output.wrapper.setProps({ classes: { bar: 'foo' } });
const extendedClasses = output.classes;
assert.deepEqual(extendedClasses, { root: baseClasses.root, bar: 'undefined foo' });
assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.args()[0][0],
'Material-UI: the key `bar` provided to the classes prop is not implemented',
);
});
it('should warn if providing a string', () => {
const output = mountWithProps();
output.wrapper.setProps({ classes: 'foo' });
assert.strictEqual(consoleErrorMock.callCount() >= 1, true);
const args = consoleErrorMock.args();
assert.include(
consoleErrorMock.args()[args.length - 1][0],
'You might want to use the className prop instead',
);
});
it('should warn if providing a non string', () => {
const output = mountWithProps();
const baseClasses = output.classes;
output.wrapper.setProps({ classes: { root: {} } });
const extendedClasses = output.classes;
assert.deepEqual(extendedClasses, { root: `${baseClasses.root} [object Object]` });
assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.args()[0][0],
'Material-UI: the key `root` provided to the classes prop is not valid',
);
});
it('should warn if missing theme', () => {
const styles = theme => ({ root: { padding: theme.spacing(2) } });
const mountWithProps2 = createGetClasses(styles);
assert.throw(() => {
mountWithProps2({});
});
assert.strictEqual(consoleErrorMock.callCount(), 4);
assert.include(consoleErrorMock.args()[1][0], 'the `styles` argument provided is invalid');
});
});
describe('classes memoization', () => {
let mountWithProps;
before(() => {
const styles = { root: {} };
mountWithProps = createGetClasses(styles);
});
it('should recycle with no classes prop', () => {
const output = mountWithProps();
const classes1 = output.classes;
output.wrapper.update();
const classes2 = output.classes;
assert.strictEqual(classes1, classes2);
});
it('should recycle even when a classes prop is provided', () => {
const inputClasses = { root: 'foo' };
const output = mountWithProps({ classes: inputClasses });
const classes1 = output.classes;
output.wrapper.setProps({
classes: inputClasses,
});
const classes2 = output.classes;
assert.strictEqual(classes1, classes2);
});
it('should invalidate the cache', () => {
const output = mountWithProps();
const classes = output.classes;
output.wrapper.setProps({ classes: { root: 'foo' } });
const classes1 = output.classes;
assert.deepEqual(classes1, {
root: `${classes.root} foo`,
});
output.wrapper.setProps({
classes: { root: 'bar' },
});
const classes2 = output.classes;
assert.notStrictEqual(classes1, classes2);
assert.deepEqual(classes2, {
root: `${classes.root} bar`,
});
});
});
describe('integration', () => {
let sheetsRegistry;
beforeEach(() => {
sheetsRegistry = new SheetsRegistry();
});
it('should run lifecycles with no theme', () => {
const useStyles = makeStyles({ root: { display: 'flex' } });
const StyledComponent = () => {
useStyles();
return <div />;
};
const wrapper = mount(
<ThemeProvider theme={createMuiTheme()}>
<StylesProvider
sheetsRegistry={sheetsRegistry}
sheetsCache={new Map()}
generateClassName={generateClassName}
>
<StyledComponent />
</StylesProvider>
</ThemeProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'makeStyles-root-1' });
wrapper.update();
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'makeStyles-root-1' });
wrapper.setProps({ theme: createMuiTheme() });
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'makeStyles-root-2' });
wrapper.unmount();
assert.strictEqual(sheetsRegistry.registry.length, 0);
});
it('should work when depending on a theme', () => {
const useStyles = makeStyles(theme => ({ root: { padding: theme.spacing(1) } }), {
name: 'MuiTextField',
});
const StyledComponent = () => {
useStyles();
return <div />;
};
const wrapper = mount(
<ThemeProvider theme={createMuiTheme()}>
<StylesProvider
sheetsRegistry={sheetsRegistry}
sheetsCache={new Map()}
generateClassName={generateClassName}
>
<StyledComponent />
</StylesProvider>
</ThemeProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'MuiTextField-root' });
wrapper.setProps({ theme: createMuiTheme({ foo: 'bar' }) });
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'MuiTextField-root' });
});
describe('overrides', () => {
it('should support the overrides key', () => {
const useStyles = makeStyles(
{
root: {
padding: 8,
margin: [1, 3],
},
},
{
name: 'MuiTextField',
},
);
const StyledComponent = () => {
useStyles();
return <div />;
};
mount(
<ThemeProvider
theme={createMuiTheme({
overrides: {
MuiTextField: {
root: {
padding: 9,
margin: [2, 2, 3],
},
},
},
})}
>
<StylesProvider sheetsRegistry={sheetsRegistry} sheetsCache={new Map()}>
<StyledComponent />
</StylesProvider>
</ThemeProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].rules.raw, {
root: { padding: 9, margin: [2, 2, 3] },
});
});
it('can be used to remove styles', () => {
const theme = {
overrides: {
Test: {
root: {
margin: null,
},
},
},
};
const useStyles = makeStyles({ root: { margin: 5, padding: 3 } }, { name: 'Test' });
function Test() {
const classes = useStyles();
return <div className={classes.root} />;
}
const wrapper = mount(
<ThemeProvider theme={theme}>
<StylesProvider sheetsRegistry={sheetsRegistry} sheetsCache={new Map()}>
<Test />
</StylesProvider>
</ThemeProvider>,
);
const div = wrapper.find('div').instance();
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].rules.raw, {
root: { margin: null, padding: 3 },
});
});
});
it('should handle dynamic props', () => {
const useStyles = makeStyles({
root: props => ({ margin: 8, padding: props.padding || 8 }),
});
const StyledComponent = props => {
const classes = useStyles(props);
return <div className={classes.root} />;
};
const Test = props => (
<StylesProvider
sheetsRegistry={sheetsRegistry}
sheetsCache={new Map()}
generateClassName={generateClassName}
>
<StyledComponent {...props} />
</StylesProvider>
);
const wrapper = mount(<Test />);
assert.strictEqual(sheetsRegistry.registry.length, 2);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'makeStyles-root-1' });
assert.deepEqual(sheetsRegistry.registry[1].classes, { root: 'makeStyles-root-2' });
assert.deepEqual(sheetsRegistry.registry[1].rules.map.root.style, {
margin: '8px',
padding: '8px',
});
wrapper.setProps({ padding: 4 });
assert.strictEqual(sheetsRegistry.registry.length, 2);
assert.deepEqual(sheetsRegistry.registry[0].classes, { root: 'makeStyles-root-1' });
assert.deepEqual(sheetsRegistry.registry[1].classes, { root: 'makeStyles-root-2' });
assert.deepEqual(sheetsRegistry.registry[1].rules.map.root.style, {
margin: '8px',
padding: '4px',
});
});
});
describe('options: disableGeneration', () => {
it('should not generate the styles', () => {
const sheetsRegistry = new SheetsRegistry();
const Empty = () => <div />;
const useStyles = makeStyles({ root: { padding: 8 } });
const StyledComponent = () => {
const classes = useStyles();
return <Empty classes={classes} />;
};
const wrapper = mount(
<StylesProvider sheetsRegistry={sheetsRegistry} disableGeneration sheetsCache={new Map()}>
<StyledComponent />
</StylesProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 0);
assert.deepEqual(wrapper.find(Empty).props().classes, {});
wrapper.unmount();
assert.strictEqual(sheetsRegistry.registry.length, 0);
});
});
describe('react-hot-loader', () => {
it('should take the new stylesCreator into account', () => {
const useStyles1 = makeStyles({ root: { padding: 8 } });
const useStyles2 = makeStyles({ root: { padding: 4 } });
let hmr = false;
const StyledComponent = () => {
// Simulate react-hot-loader behavior
/* eslint-disable react-hooks/rules-of-hooks */
if (hmr) {
useStyles2();
} else {
useStyles1();
}
/* eslint-enable react-hooks/rules-of-hooks */
return <div />;
};
const sheetsRegistry = new SheetsRegistry();
const wrapper = mount(
<StylesProvider sheetsRegistry={sheetsRegistry} sheetsCache={new Map()}>
<StyledComponent />
</StylesProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].rules.raw, {
root: { padding: 8 },
});
hmr = true;
wrapper.setProps({});
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.deepEqual(sheetsRegistry.registry[0].rules.raw, {
root: { padding: 4 },
});
});
});
describe('classname quality', () => {
let sheetsRegistry;
beforeEach(() => {
sheetsRegistry = new SheetsRegistry();
});
it('should use the displayName', () => {
const useStyles1 = makeStyles({ root: { padding: 8 } });
const StyledComponent1 = () => {
useStyles1();
return <div />;
};
const useStyles2 = makeStyles({ root: { padding: 8 } }, { name: 'Fooo' });
const StyledComponent2 = () => {
useStyles2();
return <div />;
};
mount(
<StylesProvider sheetsRegistry={sheetsRegistry} sheetsCache={new Map()}>
<StyledComponent1 />
<StyledComponent2 />
</StylesProvider>,
);
assert.strictEqual(sheetsRegistry.registry[0].options.classNamePrefix, 'makeStyles');
assert.strictEqual(sheetsRegistry.registry[0].options.name, undefined);
assert.strictEqual(sheetsRegistry.registry[1].options.classNamePrefix, 'Fooo');
assert.strictEqual(sheetsRegistry.registry[1].options.name, 'Fooo');
});
});
describe('stress test', () => {
let StressTest;
before(() => {
const useStyles = makeStyles(theme => ({
root: props => ({
backgroundColor: props.backgroundColor,
color: theme.color,
}),
}));
const Component = React.memo(props => {
const classes = useStyles(props);
const theme = useTheme();
const rendered = React.useRef(1);
React.useEffect(() => {
rendered.current += 1;
});
return (
<div className={classes.root}>
rendered {rendered.current} times
<br />
backgroundColor: {props.backgroundColor}
<br />
color: {theme.color}
</div>
);
});
Component.propTypes = {
backgroundColor: PropTypes.string.isRequired,
};
StressTest = () => {
const [backgroundColor, setBackgroundColor] = React.useState('black');
const handleBackgroundColorChange = event => {
setBackgroundColor(event.target.value);
};
const [color, setColor] = React.useState('white');
const handleColorChange = event => {
setColor(event.target.value);
};
const theme = React.useMemo(() => ({ color }), [color]);
return (
<ThemeProvider theme={theme}>
<fieldset>
<div>Color in theme, background-color in props</div>
<label htmlFor="background-color">background-color</label>
<input
id="background-color"
onChange={handleBackgroundColorChange}
value={backgroundColor}
/>
<label htmlFor="color">color</label>
<input id="color" onChange={handleColorChange} value={color} />
</fieldset>
<Component backgroundColor={backgroundColor} />
</ThemeProvider>
);
};
});
it('should update like expected', () => {
const sheetsRegistry = new SheetsRegistry();
const wrapper = mount(
<StylesProvider
sheetsRegistry={sheetsRegistry}
sheetsCache={new Map()}
generateClassName={generateClassName}
>
<StressTest />
</StylesProvider>,
);
assert.strictEqual(sheetsRegistry.registry.length, 2);
assert.strictEqual(
sheetsRegistry.toString(),
`.makeStyles-root-2 {
color: white;
background-color: black;
}`,
);
act(() => {
wrapper.find('#color').simulate('change', { target: { value: 'blue' } });
});
assert.strictEqual(
sheetsRegistry.toString(),
`.makeStyles-root-4 {
color: blue;
background-color: black;
}`,
);
act(() => {
wrapper.find('#background-color').simulate('change', { target: { value: 'green' } });
});
assert.strictEqual(
sheetsRegistry.toString(),
`.makeStyles-root-4 {
color: blue;
background-color: green;
}`,
);
});
});
});