forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StylesProvider.test.js
171 lines (148 loc) · 4.79 KB
/
StylesProvider.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
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { assert } from 'chai';
import { create, SheetsRegistry } from 'jss';
import { createMount } from '@material-ui/core/test-utils';
import StylesProvider, { StylesContext } from './StylesProvider';
import makeStyles from '../makeStyles';
import createGenerateClassName from '../createGenerateClassName';
import consoleErrorMock from 'test/utils/consoleErrorMock';
function Test() {
const options = React.useContext(StylesContext);
return <span options={options} />;
}
function getOptions(wrapper) {
return wrapper.find('span').props().options;
}
describe('StylesProvider', () => {
let mount;
let generateClassName;
before(() => {
mount = createMount({ strict: true });
});
beforeEach(() => {
generateClassName = createGenerateClassName();
});
after(() => {
mount.cleanUp();
});
it('should provide the options', () => {
const wrapper = mount(
<StylesProvider disableGeneration>
<Test />
</StylesProvider>,
);
assert.strictEqual(getOptions(wrapper).disableGeneration, true);
});
it('should merge the themes', () => {
const wrapper = mount(
<StylesProvider>
<StylesProvider disableGeneration>
<Test />
</StylesProvider>
</StylesProvider>,
);
assert.strictEqual(getOptions(wrapper).disableGeneration, true);
});
it('should handle injectFirst', () => {
const wrapper = mount(
<StylesProvider injectFirst>
<Test />
</StylesProvider>,
);
assert.strictEqual(getOptions(wrapper).jss.options.insertionPoint.nodeType, 8);
});
describe('server-side', () => {
// Only run the test on node.
if (!/jsdom/.test(window.navigator.userAgent)) {
return;
}
const useStyles = makeStyles({ root: { display: 'flex' } });
const Button = (props) => {
const classes = useStyles();
return <button type="button" className={classes.root} {...props} />;
};
function assertRendering(markup, sheetsRegistry) {
assert.notStrictEqual(markup.match('Hello World'), null);
assert.strictEqual(sheetsRegistry.registry.length, 1);
assert.strictEqual(sheetsRegistry.toString().length > 10, true);
assert.deepEqual(sheetsRegistry.registry[0].classes, {
root: 'makeStyles-root-1',
});
}
it('should be able to extract the styles', () => {
const sheetsRegistry = new SheetsRegistry();
const markup = ReactDOMServer.renderToString(
<StylesProvider
sheetsManager={new Map()}
sheetsRegistry={sheetsRegistry}
generateClassName={generateClassName}
>
<Button>Hello World</Button>
</StylesProvider>,
);
assertRendering(markup, sheetsRegistry);
});
it('should be able to cache the sheets between two requests', () => {
const sheetsCache = new Map();
const sheetsRegistry1 = new SheetsRegistry();
const markup1 = ReactDOMServer.renderToString(
<StylesProvider
sheetsManager={new Map()}
sheetsCache={sheetsCache}
sheetsRegistry={sheetsRegistry1}
generateClassName={generateClassName}
>
<Button>Hello World</Button>
</StylesProvider>,
);
assertRendering(markup1, sheetsRegistry1);
const sheetsRegistry2 = new SheetsRegistry();
const markup2 = ReactDOMServer.renderToString(
<StylesProvider
sheetsManager={new Map()}
sheetsCache={sheetsCache}
sheetsRegistry={sheetsRegistry2}
generateClassName={generateClassName}
>
<Button>Hello World</Button>
</StylesProvider>,
);
assertRendering(markup2, sheetsRegistry2);
// The most important check:
assert.strictEqual(sheetsRegistry1.registry[0], sheetsRegistry2.registry[0]);
});
});
it('should accept a custom JSS instance', () => {
const jss = create();
const wrapper = mount(
<StylesProvider jss={jss}>
<Test />
</StylesProvider>,
);
assert.strictEqual(getOptions(wrapper).jss, jss);
});
describe('warnings', () => {
beforeEach(() => {
consoleErrorMock.spy();
});
afterEach(() => {
consoleErrorMock.reset();
});
it('should support invalid input', () => {
const jss = create();
mount(
<StylesProvider injectFirst jss={jss}>
<Test />
</StylesProvider>,
);
assert.strictEqual(consoleErrorMock.callCount(), 2);
assert.include(
consoleErrorMock.messages()[0],
'you cannot use the jss and injectFirst props at the same time',
);
// one error per render (twice rendered in StrictMode)
assert.strictEqual(consoleErrorMock.messages()[0], consoleErrorMock.messages()[1]);
});
});
});