-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
warnings.js
311 lines (278 loc) Β· 8.27 KB
/
warnings.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
// @flow
/** @jsx jsx */
import 'test-utils/next-env'
import { jsx, css, Global, keyframes, ClassNames } from '@emotion/react'
import renderer from 'react-test-renderer'
import { render } from '@testing-library/react'
// $FlowFixMe
console.error = jest.fn()
const validValues = [
'normal',
'none',
'counter',
'open-quote',
'close-quote',
'no-open-quote',
'no-close-quote',
'initial',
'inherit',
'"some thing"',
"'another thing'",
'url("http://www.example.com/test.png")',
'counter(chapter_counter)',
'counters(section_counter, ".")',
'attr(value string)'
]
beforeEach(() => {
jest.resetAllMocks()
})
it('does not warn when valid values are passed for the content property', () => {
const style = css(validValues.map(value => ({ content: value })))
expect(console.error).not.toBeCalled()
expect(renderer.create(<div css={style} />).toJSON()).toMatchSnapshot()
})
const invalidValues = ['this is not valid', '']
it('does warn when invalid values are passed for the content property', () => {
// $FlowFixMe
invalidValues.forEach(value => {
expect(() =>
renderer.create(<div css={{ content: value }} />)
).toThrowError(
`You seem to be using a value for 'content' without quotes, try replacing it with \`content: '"${value}"'\``
)
})
})
describe('unsafe pseudo classes', () => {
const ignoreSsrFlag =
'/* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */'
describe(`warns when using without flag: ${ignoreSsrFlag}`, () => {
const unsafePseudoClasses = [
':first-child',
':not(:first-child)',
':nth-child(3)',
':not(:nth-child(3))',
':nth-last-child(7)'
]
unsafePseudoClasses.forEach(pseudoClass => {
it(`"${pseudoClass}"`, () => {
const style = css`
${pseudoClass} {
color: hotpink;
}
`
const match = (pseudoClass.match(/(:first|:nth|:nth-last)-child/): any)
expect(match).not.toBeNull()
expect(renderer.create(<div css={style} />).toJSON()).toMatchSnapshot()
expect(console.error).toBeCalledWith(
`The pseudo class "${
match[0]
}" is potentially unsafe when doing server-side rendering. Try changing it to "${
match[1]
}-of-type".`
)
})
})
})
describe(`does not warn when using with flag: ${ignoreSsrFlag}`, () => {
const ignoredUnsafePseudoClasses = [
`:first-child ${ignoreSsrFlag}`,
`:not(:first-child) ${ignoreSsrFlag}`,
`:nth-child(3) ${ignoreSsrFlag}`,
`:not(:nth-child(3)) ${ignoreSsrFlag}`,
`:nth-last-child(7) ${ignoreSsrFlag}`,
`:first-child span ${ignoreSsrFlag}`,
`:first-child, span ${ignoreSsrFlag}`,
`:first-child :nth-child(3) ${ignoreSsrFlag}`,
`:first-child, :nth-child(3) ${ignoreSsrFlag}`,
`:first-child:nth-child(3) ${ignoreSsrFlag}`
]
ignoredUnsafePseudoClasses.forEach(pseudoClass => {
const styles = {
string: css`
${pseudoClass} {
color: rebeccapurple;
}
`,
object: {
[pseudoClass]: {
color: 'rebeccapurple'
}
}
}
Object.keys(styles).forEach(type => {
it(`"${pseudoClass.replace(
/\/\* \S+ \*\//g,
'/* [flag] */'
)}" in a style ${type}`, () => {
const match = (pseudoClass.match(
/(:first|:nth|:nth-last)-child/
): any)
expect(match).not.toBeNull()
expect(
renderer.create(<div css={styles[type]} />).toJSON()
).toMatchSnapshot()
expect(console.error).not.toBeCalled()
})
})
})
})
})
test('global with css prop', () => {
let tree = renderer
.create(
// $FlowFixMe
<Global
css={{
html: {
backgroundColor: 'hotpink'
},
'@font-face': {
fontFamily: 'some-name'
}
}}
/>
)
.toJSON()
expect(tree).toMatchSnapshot()
expect(console.error).toBeCalledWith(
"It looks like you're using the css prop on Global, did you mean to use the styles prop instead?"
)
})
test('kebab-case', () => {
css({ 'background-color': 'green' })
css({ 'background-color': 'hotpink' })
css({ '-ms-filter': 'inherit' })
css({ '@media (min-width 800px)': undefined })
css({ '--primary-color': 'hotpink' })
css({ ':last-of-type': null })
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"Using kebab-case for css properties in objects is not supported. Did you mean backgroundColor?",
],
Array [
"Using kebab-case for css properties in objects is not supported. Did you mean msFilter?",
],
]
`)
})
test('keyframes interpolated into plain string', () => {
const animateColor = keyframes({
'from,to': { color: 'green' },
'50%': { color: 'hotpink' }
})
const rotate360 = keyframes({
from: {
transform: 'rotate(0deg)'
},
to: {
transform: 'rotate(360deg)'
}
})
renderer.create(
<div css={[`animation: ${animateColor} 10s ${rotate360} 5s;`]} />
)
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"\`keyframes\` output got interpolated into plain string, please wrap it with \`css\`.
Instead of doing this:
const animation0 = keyframes\`{from,to{color:green;}50%{color:hotpink;}}\`
const animation1 = keyframes\`{from{transform:rotate(0deg);}to{transform:rotate(360deg);}}\`
\`animation: \${animation0} 10s \${animation1} 5s;\`
You should wrap it with \`css\` like this:
css\`animation: \${animation0} 10s \${animation1} 5s;\`",
],
]
`)
})
test('`css` opaque object passed in as `className` prop', () => {
const { container } = render(
<div
className={css`
color: hotpink;
`}
/>
)
expect(container).toMatchInlineSnapshot(`
<div>
<div
class="You have tried to stringify object returned from \`css\` function. It isn't supposed to be used directly (e.g. as value of the \`className\` prop), but rather handed to emotion so it can handle it (e.g. as value of \`css\` prop)."
/>
</div>
`)
})
test('`css` opaque object passed to `cx` from <ClassNames/>', () => {
render(
<ClassNames>
{({ cx }) => (
<div
className={cx(
// $FlowFixMe
css`
color: hotpink;
`,
'other-cls'
)}
/>
)}
</ClassNames>
)
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"You have passed styles created with \`css\` from \`@emotion/react\` package to the \`cx\`.
\`cx\` is meant to compose class names (strings) so you should convert those styles to a class name by passing them to the \`css\` received from <ClassNames/> component.",
],
]
`)
})
test('@import nested in scoped `css`', () => {
renderer.create(
<div
css={css`
@import url('https://some-url');
h1 {
color: hotpink;
}
`}
/>
)
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"\`@import\` rules can't be nested inside other rules. Please move it to the top and put it before regular rules. Keep in mind that they can only be used within global styles.",
],
]
`)
})
test('@import prepended with other rules', () => {
renderer.create(
<Global
styles={css`
h1 {
color: hotpink;
}
@import url('https://some-url');
`}
/>
)
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"\`@import\` rules can't be preceded by regular rules. Please put it before them.",
],
]
`)
})
test('@import prepended by other @import', () => {
renderer.create(
<Global
styles={css`
@import url('https://some-url');
@import url('https://some-url2');
`}
/>
)
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`Array []`)
})