/
toHaveStyleRule.spec.js
198 lines (170 loc) Β· 4.35 KB
/
toHaveStyleRule.spec.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
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
import 'react-native'
import React from 'react'
import renderer from 'react-test-renderer'
import styled, { ThemeProvider } from 'styled-components/native'
import '../../native'
test('basic', () => {
const StyledView = styled.View`
background-color: papayawhip;
`
const tree = renderer.create(<StyledView />).toJSON()
expect(tree).toHaveStyleRule('background-color', 'papayawhip')
})
test('message when property not found', () => {
const Button = styled.Text`
background-color: papayawhip;
`
expect(() =>
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'color',
'black'
)
).toThrowErrorMatchingSnapshot()
})
test('message when value does not match', () => {
const StyledView = styled.View`
background-color: orange;
`
expect(() => {
expect(renderer.create(<StyledView />).toJSON()).toHaveStyleRule(
'background-color',
'red'
)
}).toThrowErrorMatchingSnapshot()
})
test('basic', () => {
const StyledView = styled.View`
padding: 4px;
background-color: papayawhip;
`
expect(renderer.create(<StyledView />).toJSON()).toHaveStyleRule(
'background-color',
'papayawhip'
)
})
test('regex', () => {
const StyledView = styled.View`
padding: 4px;
background-color: papayawhip;
`
expect(renderer.create(<StyledView />).toJSON()).toHaveStyleRule(
'background-color',
/^papaya/
)
})
test('undefined', () => {
const Button = styled.Text`
${({ transparent }) => !transparent && 'background-color: papayawhip;'};
`
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'background-color',
'papayawhip'
)
expect(renderer.create(<Button transparent />).toJSON()).toHaveStyleRule(
'background-color',
undefined
)
})
test('negated ".not" modifier with no value', () => {
const Button = styled.Text`
${({ transparent }) => !transparent && 'background-color: papayawhip;'};
`
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'background-color',
'papayawhip'
)
expect(renderer.create(<Button transparent />).toJSON()).not.toHaveStyleRule(
'background-color'
)
})
test('jest asymmetric matchers', () => {
const Button = styled.Text`
background-color: ${({ transparent }) =>
transparent ? 'transparent' : 'papayawhip'};
`
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'background-color',
expect.any(String)
)
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'background-color',
expect.stringMatching('papayawhip')
)
expect(renderer.create(<Button />).toJSON()).toHaveStyleRule(
'background-color',
expect.stringMatching(/^papaya/)
)
expect(renderer.create(<Button transparent />).toJSON()).toHaveStyleRule(
'background-color',
expect.stringContaining('transparent')
)
expect(renderer.create(<Button transparent />).toJSON()).not.toHaveStyleRule(
'color',
expect.any(String)
)
expect(renderer.create(<Button transparent />).toJSON()).not.toHaveStyleRule(
'color',
expect.anything()
)
})
test('styled child', () => {
const Parent = styled.Text`
color: red;
`
const StyledChild = styled(Parent)`
padding: 0;
`
expect(renderer.create(<StyledChild />).toJSON()).toHaveStyleRule(
'color',
'red'
)
})
test('extending styles', () => {
const Button = styled.Text`
color: palevioletred;
font-size: 1px;
margin: 1px;
padding: 0.25px 1px;
border: 2px solid palevioletred;
border-radius: 3px;
`
const TomatoButton = Button.extend`
color: tomato;
border-color: tomato;
`
expect(renderer.create(<TomatoButton />).toJSON()).toHaveStyleRule(
'color',
'tomato'
)
})
test('theming', () => {
const Button = styled.Text`
font-size: 1px;
margin: 1px;
padding: 0.25px 1px;
border-radius: 3px;
color: ${props => props.theme.main};
border: 2px solid ${props => props.theme.main};
`
Button.defaultProps = {
theme: {
main: 'palevioletred',
},
}
const theme = {
main: 'mediumseagreen',
}
expect(renderer.create(<Button>Normal</Button>).toJSON()).toHaveStyleRule(
'color',
'palevioletred'
)
const component = (
<ThemeProvider theme={theme}>
<Button>Themed</Button>
</ThemeProvider>
)
expect(renderer.create(component).toJSON()).toHaveStyleRule(
'color',
'mediumseagreen'
)
})