1
1
import React from 'react' ;
2
- import { View , Text , StyleSheet , Pressable } from 'react-native' ;
2
+ import { StyleSheet , View , Pressable } from 'react-native' ;
3
3
import { render } from '../..' ;
4
4
import '../extend-expect' ;
5
5
6
- test ( 'handles positive test cases' , ( ) => {
7
- const styles = StyleSheet . create ( {
8
- container : { borderBottomColor : 'white' } ,
9
- } ) ;
10
- const { getByTestId } = render (
6
+ const styles = StyleSheet . create ( {
7
+ container : { borderBottomColor : 'white' } ,
8
+ } ) ;
9
+
10
+ test ( 'toHaveStyle() handles basic cases' , ( ) => {
11
+ const screen = render (
11
12
< View
12
- testID = "container "
13
+ testID = "view "
13
14
style = { [
14
15
{
15
16
backgroundColor : 'blue' ,
@@ -20,92 +21,155 @@ test('handles positive test cases', () => {
20
21
[ [ { width : '50%' } ] ] ,
21
22
styles . container ,
22
23
] }
23
- >
24
- < Text > Hello World</ Text >
25
- </ View >
24
+ />
26
25
) ;
27
26
28
- const container = getByTestId ( 'container' ) ;
27
+ const view = screen . getByTestId ( 'view' ) ;
28
+ expect ( view ) . toHaveStyle ( { backgroundColor : 'blue' } ) ;
29
+ expect ( view ) . toHaveStyle ( { height : '100%' } ) ;
30
+ expect ( view ) . toHaveStyle ( { backgroundColor : 'blue' , height : '100%' } ) ;
31
+ expect ( view ) . toHaveStyle ( [ { backgroundColor : 'blue' } , { height : '100%' } ] ) ;
32
+
33
+ expect ( view ) . toHaveStyle ( { borderBottomColor : 'white' } ) ;
34
+ expect ( view ) . toHaveStyle ( { width : '50%' } ) ;
35
+ expect ( view ) . toHaveStyle ( [ [ { width : '50%' } ] ] ) ;
36
+ expect ( view ) . toHaveStyle ( {
37
+ transform : [ { scale : 2 } , { rotate : '45deg' } ] ,
38
+ } ) ;
29
39
30
- expect ( container ) . toHaveStyle ( { backgroundColor : 'blue' , height : '100%' } ) ;
31
- expect ( container ) . toHaveStyle ( [
40
+ expect ( view ) . not . toHaveStyle ( { backgroundColor : 'red' } ) ;
41
+ expect ( view ) . not . toHaveStyle ( { height : '50%' } ) ;
42
+ expect ( view ) . not . toHaveStyle ( { backgroundColor : 'blue' , height : '50%' } ) ;
43
+ expect ( view ) . not . toHaveStyle ( [
32
44
{ backgroundColor : 'blue' } ,
33
- { height : '100 %' } ,
45
+ { height : '50 %' } ,
34
46
] ) ;
35
- expect ( container ) . toHaveStyle ( { backgroundColor : 'blue' } ) ;
36
- expect ( container ) . toHaveStyle ( { height : '100%' } ) ;
37
- expect ( container ) . toHaveStyle ( { borderBottomColor : 'white' } ) ;
38
- expect ( container ) . toHaveStyle ( { width : '50%' } ) ;
39
- expect ( container ) . toHaveStyle ( [ [ { width : '50%' } ] ] ) ;
40
- expect ( container ) . toHaveStyle ( {
41
- transform : [ { scale : 2 } , { rotate : '45deg' } ] ,
47
+ expect ( view ) . not . toHaveStyle ( {
48
+ transform : [ { scale : 2 } ] ,
49
+ } ) ;
50
+ expect ( view ) . not . toHaveStyle ( {
51
+ transform : [ { rotate : '45deg' } , { scale : 2 } ] ,
42
52
} ) ;
43
53
} ) ;
44
54
45
- test ( 'handles negative test cases ' , ( ) => {
46
- const { getByTestId } = render (
55
+ test ( 'toHaveStyle error messages ' , ( ) => {
56
+ const screen = render (
47
57
< View
48
- testID = "container "
58
+ testID = "view "
49
59
style = { {
50
60
backgroundColor : 'blue' ,
51
61
borderBottomColor : 'black' ,
52
62
height : '100%' ,
53
63
transform : [ { scale : 2 } , { rotate : '45deg' } ] ,
54
64
} }
55
- >
56
- < Text > Hello World</ Text >
57
- </ View >
65
+ />
58
66
) ;
59
67
60
- const container = getByTestId ( 'container' ) ;
68
+ const view = screen . getByTestId ( 'view' ) ;
69
+ expect ( ( ) => expect ( view ) . toHaveStyle ( { backgroundColor : 'red' } ) )
70
+ . toThrowErrorMatchingInlineSnapshot ( `
71
+ "expect(element).toHaveStyle()
72
+
73
+ - Expected
74
+ + Received
75
+
76
+ - backgroundColor: red;
77
+ + backgroundColor: blue;"
78
+ ` ) ;
79
+
61
80
expect ( ( ) =>
62
- expect ( container ) . toHaveStyle ( {
81
+ expect ( view ) . toHaveStyle ( {
63
82
backgroundColor : 'blue' ,
64
83
transform : [ { scale : 1 } ] ,
65
84
} )
66
- ) . toThrowErrorMatchingSnapshot ( ) ;
67
- expect ( container ) . not . toHaveStyle ( { fontWeight : 'bold' } ) ;
68
- expect ( container ) . not . toHaveStyle ( { color : 'black' } ) ;
69
- expect ( container ) . not . toHaveStyle ( {
70
- transform : [ { rotate : '45deg' } , { scale : 2 } ] ,
71
- } ) ;
72
- expect ( container ) . not . toHaveStyle ( { transform : [ { rotate : '45deg' } ] } ) ;
73
- } ) ;
85
+ ) . toThrowErrorMatchingInlineSnapshot ( `
86
+ "expect(element).toHaveStyle()
74
87
75
- test ( 'handles when the style prop is undefined' , ( ) => {
76
- const { getByTestId } = render (
77
- < View testID = "container" >
78
- < Text > Hello World</ Text >
79
- </ View >
80
- ) ;
88
+ - Expected
89
+ + Received
90
+
91
+ backgroundColor: blue;
92
+ transform: [
93
+ {
94
+ - "scale": 1
95
+ + "scale": 2
96
+ + },
97
+ + {
98
+ + "rotate": "45deg"
99
+ }
100
+ ];"
101
+ ` ) ;
102
+
103
+ expect ( ( ) => expect ( view ) . not . toHaveStyle ( { backgroundColor : 'blue' } ) )
104
+ . toThrowErrorMatchingInlineSnapshot ( `
105
+ "expect(element).not.toHaveStyle()
106
+
107
+ Expected element not to have style:
108
+ backgroundColor: blue;
109
+ Received:
110
+ backgroundColor: blue;"
111
+ ` ) ;
112
+
113
+ expect ( ( ) => expect ( view ) . toHaveStyle ( { fontWeight : 'bold' } ) )
114
+ . toThrowErrorMatchingInlineSnapshot ( `
115
+ "expect(element).toHaveStyle()
116
+
117
+ - Expected
118
+ + Received
119
+
120
+ - fontWeight: bold;"
121
+ ` ) ;
81
122
82
- const container = getByTestId ( 'container' ) ;
123
+ expect ( ( ) => expect ( view ) . not . toHaveStyle ( { backgroundColor : 'blue' } ) )
124
+ . toThrowErrorMatchingInlineSnapshot ( `
125
+ "expect(element).not.toHaveStyle()
83
126
84
- expect ( container ) . not . toHaveStyle ( { fontWeight : 'bold' } ) ;
127
+ Expected element not to have style:
128
+ backgroundColor: blue;
129
+ Received:
130
+ backgroundColor: blue;"
131
+ ` ) ;
85
132
} ) ;
86
133
87
- test ( 'handles transform when transform undefined' , ( ) => {
88
- const { getByTestId } = render (
134
+ test ( 'toHaveStyle() supports missing "style" prop' , ( ) => {
135
+ const screen = render ( < View testID = "view" /> ) ;
136
+
137
+ const view = screen . getByTestId ( 'view' ) ;
138
+ expect ( view ) . not . toHaveStyle ( { fontWeight : 'bold' } ) ;
139
+ } ) ;
140
+
141
+ test ( 'toHaveStyle() supports undefined "transform" style' , ( ) => {
142
+ const screen = render (
89
143
< View
90
- testID = "container "
144
+ testID = "view "
91
145
style = { {
92
146
backgroundColor : 'blue' ,
93
147
transform : undefined ,
94
148
} }
95
- >
96
- < Text > Hello World</ Text >
97
- </ View >
149
+ />
98
150
) ;
99
151
100
- const container = getByTestId ( 'container' ) ;
101
- expect ( ( ) =>
102
- expect ( container ) . toHaveStyle ( { transform : [ { scale : 1 } ] } )
103
- ) . toThrowErrorMatchingSnapshot ( ) ;
152
+ const view = screen . getByTestId ( 'view' ) ;
153
+ expect ( ( ) => expect ( view ) . toHaveStyle ( { transform : [ { scale : 1 } ] } ) )
154
+ . toThrowErrorMatchingInlineSnapshot ( `
155
+ "expect(element).toHaveStyle()
156
+
157
+ - Expected
158
+ + Received
159
+
160
+ - transform: [
161
+ - {
162
+ - "scale": 1
163
+ - }
164
+ - ];
165
+ + transform: undefined;"
166
+ ` ) ;
104
167
} ) ;
105
168
106
- test ( 'handles Pressable with function style prop' , ( ) => {
107
- const { getByTestId } = render (
108
- < Pressable testID = "test " style = { ( ) => ( { backgroundColor : 'blue' } ) } />
169
+ test ( 'toHaveStyle() supports Pressable with function " style" prop' , ( ) => {
170
+ const screen = render (
171
+ < Pressable testID = "view " style = { ( ) => ( { backgroundColor : 'blue' } ) } />
109
172
) ;
110
- expect ( getByTestId ( 'test' ) ) . toHaveStyle ( { backgroundColor : 'blue' } ) ;
173
+
174
+ expect ( screen . getByTestId ( 'view' ) ) . toHaveStyle ( { backgroundColor : 'blue' } ) ;
111
175
} ) ;
0 commit comments