-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcustom-cache.js
84 lines (75 loc) · 1.81 KB
/
custom-cache.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
/** @jsx jsx */
import createCache from '@emotion/cache'
import { CacheProvider, Global, jsx } from '@emotion/react'
import { StyleSheet } from '@emotion/sheet'
import { render } from '@testing-library/react'
import { safeQuerySelector } from 'test-utils'
import 'test-utils/setup-env'
function stylisPlugin(element) {
if (element.type === 'decl' && element.value.startsWith('color:')) {
element.value = `color:hotpink;`
}
}
beforeEach(() => {
safeQuerySelector('head').innerHTML = ''
safeQuerySelector('body').innerHTML = ''
})
test('with custom plugins', () => {
let cache = createCache({
key: 'custom-plugins',
stylisPlugins: [stylisPlugin]
})
expect(
render(
<CacheProvider value={cache}>
<div css={{ display: 'flex', color: 'blue' }} />
</CacheProvider>
).container.firstChild
).toMatchInlineSnapshot(`
.emotion-0 {
display: flex;
color: hotpink;
}
<div
class="emotion-0"
/>
`)
})
test('Global should "inherit" sheet class from the cache', () => {
// https://github.com/emotion-js/emotion/issues/2675
let cache = createCache({
key: 'test',
speedy: false
})
class MySheet extends StyleSheet {
insert(rule) {
super.insert(`/** ${this.key} */${rule}`)
}
}
cache.sheet = new MySheet({
key: 'test',
container: safeQuerySelector('head')
})
render(
<CacheProvider value={cache}>
<div css={{ color: 'hotpink' }} />
<Global styles={{ body: { width: '0' } }} />
</CacheProvider>
)
expect(safeQuerySelector('head')).toMatchInlineSnapshot(`
<head>
<style
data-href="1lrxbo5"
data-precedence="emotion-test"
>
.test-1lrxbo5{color:hotpink;}
</style>
<style
data-emotion="test-global"
data-s=""
>
/** test-global */body{width:0;}
</style>
</head>
`)
})