/
babel.js
157 lines (135 loc) Β· 4.28 KB
/
babel.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
/* eslint-disable jsx-quotes,no-useless-escape,no-template-curly-in-string */
/* eslint-env jest */
import React from 'react'
import renderer from 'react-test-renderer'
import plugin from '../babel'
import css, {fragment} from 'glam'
const babel = require('babel-core')
describe('emotion/glam', () => {
describe('babel css prop', () => {
test('basic', () => {
const basic = '(<div className="a" css={`color: brown;`}></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('no css attr', () => {
const basic = '(<div></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('css empty', () => {
const basic = '(<div css=""></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('wrong value type', () => {
const basic = '(<div css={5}></div>)'
expect(() => babel.transform(basic, {plugins: [plugin, 'glam/babel']})).toThrow()
})
test('StringLiteral css prop value', () => {
const basic = `<div css="color: brown;"></div>`
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('noClassName', () => {
const basic = '(<div css={`color: brown;`}></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('emptyClassName', () => {
const basic = '(<div className="" css={`color: brown;`}></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('className as expression', () => {
const basic = '(<div className={variable} css={`color: brown;`}></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
test('className as expression string', () => {
const basic =
'(<div className={`test__class\`} css={`color: brown;`} this={`hello\`}></div>)'
const {code} = babel.transform(basic, {plugins: [plugin, 'glam/babel']})
expect(code).toMatchSnapshot()
})
})
describe('real', () => {
test('basic', () => {
const tree = renderer
.create(
<p css={`color: red;`}>
hello world
</p>
)
.toJSON()
expect(tree).toMatchSnapshot()
})
test('string expression', () => {
const tree = renderer
.create(
<p css="color:red;background:blue;font-size:48px;">
hello world
</p>
)
.toJSON()
expect(tree).toMatchSnapshot()
})
test('kitchen sink', () => {
const props = {online: false, error: false, radius: 5}
const huge = 100
const tiny = 6
const bold = fragment`
display: flex;
font-weight: bold;`
const big = fragment`
@apply ${bold};
font-size: ${huge}`
const small = fragment`
font-size: ${tiny}`
const flexCenter = fragment`
display: flex;
justify-content: center;
align-items: center`
const tree = renderer
.create(
<div
className="css__legacy-stuff"
css={`
@apply ${bold}
@apply ${flexCenter};
`}
>
<h1
css={`
@apply ${props.error ? big : small};
color: red
`}
>
BOOM
</h1>
<p className="test_class1" css={`color: blue;`}>Hello</p>
<p
className="test_class1 test___class45"
css={`display: inline-flex`}
>
World
</p>
<p
css={`
color: red;
border-radius: ${props.radius};
&:hover {
font-weight: bold;
color: ${props.online ? 'green' : 'gray'};
}
`}
>
hello world
</p>
</div>
)
.toJSON()
expect(tree).toMatchSnapshot()
})
})
})