-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathglobal.js
94 lines (85 loc) · 2.22 KB
/
global.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
import * as React from 'react'
import { render } from '@testing-library/react'
import {
Global,
keyframes,
css,
CacheProvider,
ThemeProvider
} from '@emotion/react'
import createCache from '@emotion/cache'
console.error = jest.fn()
beforeEach(() => {
document.head.innerHTML = ''
jest.resetAllMocks()
})
test('basic', () => {
const { unmount } = render(
<CacheProvider value={createCache({ key: 'css' })}>
<Global
styles={[
css`
@import url('something.com/file.css');
`,
{
html: {
backgroundColor: 'hotpink'
},
h1: {
animation: `${keyframes({
'from,to': {
color: 'green'
},
'50%': {
color: 'hotpink'
}
})} 1s`
},
'@font-face': {
fontFamily: 'some-name'
}
}
]}
/>
</CacheProvider>
)
expect(document.head).toMatchSnapshot()
expect(document.body).toMatchSnapshot()
unmount()
expect(document.head).toMatchSnapshot()
expect(document.body).toMatchSnapshot()
})
test('updating more than 1 global rule', () => {
const cache = createCache({ key: 'global-multiple-rules' })
const Comp = ({ background, color }) => (
<CacheProvider value={cache}>
<Global styles={{ body: { background }, div: { color } }} />
</CacheProvider>
)
const { rerender } = render(<Comp background="white" color="black" />)
expect(document.head).toMatchSnapshot()
rerender(<Comp background="gray" color="white" />)
expect(document.head).toMatchSnapshot()
})
test('no React hook order violations', () => {
const theme = { color: 'blue' }
const cache = createCache({ key: 'context' })
const Comp = ({ flag }) => (
<ThemeProvider theme={theme}>
<CacheProvider value={cache}>
<Global
styles={
flag &&
(t => css`
color: ${t.color};
`)
}
/>
</CacheProvider>
</ThemeProvider>
)
render(<Comp />)
expect(console.error).not.toHaveBeenCalled()
render(<Comp flag />)
expect(console.error).not.toHaveBeenCalled()
})