-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathimport-prod.js
94 lines (84 loc) · 2.21 KB
/
import-prod.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 * as ReactDOM from 'react-dom'
import { css, Global } from '@emotion/react'
import styled from '@emotion/styled'
import prettify from '@emotion/css-prettifier'
// using styled instead of the css prop because there was a really weird flow error
// when using `jsx` from @emotion/react and Global
let Comp = styled.div({ color: 'hotpink' })
expect.addSnapshotSerializer({
test: x => x instanceof StyleSheet,
print: ({ cssRules }) => {
let styles = ''
for (let rule of cssRules) {
styles += rule.cssText
}
return prettify(styles)
}
})
// can't use RTL as production React 18 throws when it's trying to use `act`
const render = children =>
new Promise(resolve => {
const el = document.createElement('div')
document.body.appendChild(el)
if (ReactDOM.createRoot) {
const root = ReactDOM.createRoot(el)
root.render(<div ref={resolve}>{children}</div>)
} else {
ReactDOM.render(children, el, resolve)
}
})
gate({ development: false }, ({ test }) => {
test('it works', async () => {
await render(
<div>
<Comp>something</Comp>
<Global
styles={{
html: {
backgroundColor: 'yellow'
}
}}
/>
<Global
styles={css`
@import url('something.com/file.css');
body {
padding: 0;
}
`}
/>
</div>
)
// order should be
// 1. html { background-color: yellow; }
// 1. @import
// 2. body { padding: 0; }
// 3. styled comp
// querying for style instead of [data-emotion] to appease flow
let elements = Array.from(document.querySelectorAll('style')).filter(x =>
x.getAttribute('data-emotion')
)
expect(elements.map(x => x.getAttribute('data-emotion'))).toEqual([
'css-global',
'css-global',
'css'
])
expect(elements[0].sheet).toMatchInlineSnapshot(`
html {
background-color: yellow;
}
`)
expect(elements[1].sheet).toMatchInlineSnapshot(`
@import url(something.com/file.css);
body {
padding: 0;
}
`)
expect(elements[2].sheet).toMatchInlineSnapshot(`
.css-1lrxbo5 {
color: hotpink;
}
`)
})
})