Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
228 lines (190 loc) · 6.28 KB
/
Copy pathindex.js
File metadata and controls
228 lines (190 loc) · 6.28 KB
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
import { expect, it, vi } from 'vitest'
import transformCss, { getStylesForProperty } from '..'
it('transforms numbers', () => {
expect(transformCss([['z-index', '0']])).toEqual({ zIndex: 0 })
})
it('warns if missing units on unspecialized transform', () => {
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {
// Silence the warning from the test output
})
transformCss([['top', '1']])
expect(consoleSpy).toHaveBeenCalledWith(
'Expected style "top: 1" to contain units'
)
consoleSpy.mockRestore()
})
it('does not warn for unitless 0 length on unspecialized transform', () => {
const consoleSpy = vi.spyOn(console, 'warn')
transformCss([['top', '0']])
expect(consoleSpy).not.toHaveBeenCalled()
consoleSpy.mockRestore()
})
it('warns if adding etraneous units on unspecialized transform', () => {
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {
// Silence the warning from the test output
})
transformCss([['opacity', '1px']])
expect(consoleSpy).toHaveBeenCalledWith(
'Expected style "opacity: 1px" to be unitless'
)
consoleSpy.mockRestore()
})
it('does not warn for unitless 0 length on unitless transform', () => {
const consoleSpy = vi.spyOn(console, 'warn')
transformCss([['opacity', '0']])
expect(consoleSpy).not.toHaveBeenCalled()
consoleSpy.mockRestore()
})
it('allows pixels in unspecialized transform', () => {
expect(transformCss([['top', '0px']])).toEqual({ top: 0 })
})
it('allows boolean values', () => {
expect(
transformCss([
['boolTrue1', 'true'],
['boolTrue2', 'TRUE'],
['boolFalse1', 'false'],
['boolFalse2', 'FALSE'],
])
).toEqual({
boolTrue1: true,
boolTrue2: true,
boolFalse1: false,
boolFalse2: false,
})
})
it('allows null values', () => {
expect(transformCss([['null1', 'null'], ['null2', 'NULL']])).toEqual({
null1: null,
null2: null,
})
})
it('allows undefined values', () => {
expect(
transformCss([['undefined1', 'undefined'], ['undefined2', 'UNDEFINED']])
).toEqual({
undefined1: undefined,
undefined2: undefined,
})
})
it('allows CSS custom properties to pass through', () => {
expect(transformCss([['--my-prop', '0%']])).toEqual({ '--my-prop': '0%' })
})
it('passes filter through as a raw React Native property', () => {
expect(transformCss([['filter', 'opacity(0.5)']])).toEqual({
filter: 'opacity(0.5)',
})
})
it('passes multiple filter functions through unchanged', () => {
expect(transformCss([['filter', 'opacity(0.5) blur(5px)']])).toEqual({
filter: 'opacity(0.5) blur(5px)',
})
})
it('passes box-shadow through as a raw React Native property', () => {
expect(transformCss([['box-shadow', '10px 20px 30px red']])).toEqual({
boxShadow: '10px 20px 30px red',
})
})
it('passes multiple box-shadow values through unchanged', () => {
expect(
transformCss([['box-shadow', '5px 5px 5px red, -5px -5px 5px green']])
).toEqual({
boxShadow: '5px 5px 5px red, -5px -5px 5px green',
})
})
it('allows percent in unspecialized transform', () => {
expect(transformCss([['top', '0%']])).toEqual({ top: '0%' })
})
it('allows decimal values', () => {
expect(getStylesForProperty('margin', '0.5px').marginTop).toBe(0.5)
expect(getStylesForProperty('margin', '1.5px').marginTop).toBe(1.5)
expect(getStylesForProperty('margin', '10.5px').marginTop).toBe(10.5)
expect(getStylesForProperty('margin', '100.5px').marginTop).toBe(100.5)
expect(getStylesForProperty('margin', '-0.5px').marginTop).toBe(-0.5)
expect(getStylesForProperty('margin', '-1.5px').marginTop).toBe(-1.5)
expect(getStylesForProperty('margin', '-10.5px').marginTop).toBe(-10.5)
expect(getStylesForProperty('margin', '-100.5px').marginTop).toBe(-100.5)
expect(getStylesForProperty('margin', '.5px').marginTop).toBe(0.5)
expect(getStylesForProperty('margin', '-.5px').marginTop).toBe(-0.5)
})
it('allows decimal values in transformed values', () => {
expect(transformCss([['border-radius', '1.5px']])).toEqual({
borderTopLeftRadius: 1.5,
borderTopRightRadius: 1.5,
borderBottomRightRadius: 1.5,
borderBottomLeftRadius: 1.5,
})
})
it('allows negative values in transformed values', () => {
expect(transformCss([['border-radius', '-1.5px']])).toEqual({
borderTopLeftRadius: -1.5,
borderTopRightRadius: -1.5,
borderBottomRightRadius: -1.5,
borderBottomLeftRadius: -1.5,
})
})
it('allows uppercase units', () => {
expect(transformCss([['top', '0PX']])).toEqual({ top: 0 })
expect(transformCss([['transform', 'rotate(30DEG)']])).toEqual({
transform: [{ rotate: '30deg' }],
})
})
it('allows percent values in transformed values', () => {
expect(transformCss([['margin', '10%']])).toEqual({
marginTop: '10%',
marginRight: '10%',
marginBottom: '10%',
marginLeft: '10%',
})
})
it('allows color values in transformed border-color values', () => {
expect(transformCss([['border-color', 'red']])).toEqual({
borderTopColor: 'red',
borderRightColor: 'red',
borderBottomColor: 'red',
borderLeftColor: 'red',
})
})
it('allows omitting units for 0', () => {
expect(transformCss([['margin', '10px 0']])).toEqual({
marginTop: 10,
marginRight: 0,
marginBottom: 10,
marginLeft: 0,
})
})
it('transforms strings', () => {
expect(transformCss([['color', 'red']])).toEqual({ color: 'red' })
})
it('converts to camel-case', () => {
expect(transformCss([['background-color', 'red']])).toEqual({
backgroundColor: 'red',
})
})
it('transforms background to backgroundColor', () => {
expect(transformCss([['background', '#f00']])).toEqual({
backgroundColor: '#f00',
})
})
it('transforms background to backgroundColor with rgb', () => {
expect(transformCss([['background', 'rgb(255, 0, 0)']])).toEqual({
backgroundColor: 'rgb(255, 0, 0)',
})
})
it('transforms background to backgroundColor with named colour', () => {
expect(transformCss([['background', 'red']])).toEqual({
backgroundColor: 'red',
})
})
it('allows blacklisting shorthands', () => {
const actualStyles = transformCss(
[['border-radius', '50px']],
['borderRadius']
)
expect(actualStyles).toEqual({ borderRadius: 50 })
})
it('throws useful errors', () => {
expect(() => transformCss([['margin', '10']])).toThrow(
'Failed to parse declaration "margin: 10"'
)
})