1
1
import React from 'react' ;
2
2
import { Button , Text , View } from 'react-native' ;
3
- import { render } from '../..' ;
3
+ import { render , screen } from '../..' ;
4
4
import '../extend-expect' ;
5
5
6
6
test ( '.toHaveProp' , ( ) => {
7
- const { queryByTestId } = render (
7
+ render (
8
8
< View style = { null } testID = "view" >
9
9
< Text allowFontScaling = { false } testID = "text" ellipsizeMode = "head" >
10
10
text
@@ -13,31 +13,67 @@ test('.toHaveProp', () => {
13
13
</ View >
14
14
) ;
15
15
16
- expect ( queryByTestId ( 'button' ) ) . toHaveProp ( 'accessibilityState' , {
16
+ const text = screen . getByTestId ( 'text' ) ;
17
+ const button = screen . getByTestId ( 'button' ) ;
18
+ const view = screen . getByTestId ( 'view' ) ;
19
+
20
+ expect ( button ) . toHaveProp ( 'accessibilityState' , {
17
21
disabled : true ,
18
22
} ) ;
19
- expect ( queryByTestId ( ' text' ) ) . toHaveProp ( 'ellipsizeMode' , 'head' ) ;
20
- expect ( queryByTestId ( ' text' ) ) . toHaveProp ( 'allowFontScaling' , false ) ;
23
+ expect ( text ) . toHaveProp ( 'ellipsizeMode' , 'head' ) ;
24
+ expect ( text ) . toHaveProp ( 'allowFontScaling' , false ) ;
21
25
22
- expect ( queryByTestId ( ' button' ) ) . not . toHaveProp ( 'accessibilityStates' ) ;
23
- expect ( queryByTestId ( ' button' ) ) . not . toHaveProp ( 'ellipsizeMode' , undefined ) ;
24
- expect ( queryByTestId ( ' button' ) ) . not . toHaveProp ( 'allowFontScaling' , false ) ;
25
- expect ( queryByTestId ( ' text' ) ) . not . toHaveProp ( 'style' ) ;
26
+ expect ( button ) . not . toHaveProp ( 'accessibilityStates' ) ;
27
+ expect ( button ) . not . toHaveProp ( 'ellipsizeMode' , undefined ) ;
28
+ expect ( button ) . not . toHaveProp ( 'allowFontScaling' , false ) ;
29
+ expect ( text ) . not . toHaveProp ( 'style' ) ;
26
30
27
31
// title is no longer findable as it is a React child
28
- expect ( ( ) =>
29
- expect ( queryByTestId ( 'button' ) ) . toHaveProp ( 'title' , 'ok' )
30
- ) . toThrow ( ) ;
31
- expect ( ( ) =>
32
- expect ( queryByTestId ( 'button' ) ) . toHaveProp ( 'disabled' )
33
- ) . toThrow ( ) ;
34
- expect ( ( ) =>
35
- expect ( queryByTestId ( 'text' ) ) . not . toHaveProp ( 'allowFontScaling' , false )
36
- ) . toThrow ( ) ;
37
- expect ( ( ) => expect ( queryByTestId ( 'text' ) ) . toHaveProp ( 'style' ) ) . toThrow ( ) ;
38
- expect ( ( ) =>
39
- expect ( queryByTestId ( 'text' ) ) . toHaveProp ( 'allowFontScaling' , 'wrongValue' )
40
- ) . toThrow ( ) ;
41
-
42
- expect ( queryByTestId ( 'view' ) ) . toHaveProp ( 'style' , null ) ;
32
+ expect ( ( ) => expect ( button ) . toHaveProp ( 'title' , 'ok' ) )
33
+ . toThrowErrorMatchingInlineSnapshot ( `
34
+ "expect(element).toHaveProp("title", "ok") // Element should have prop title with value "ok"
35
+
36
+ Expected the element to have prop:
37
+ title="ok"
38
+ Received:
39
+ null"
40
+ ` ) ;
41
+ expect ( ( ) => expect ( button ) . toHaveProp ( 'disabled' ) )
42
+ . toThrowErrorMatchingInlineSnapshot ( `
43
+ "expect(element).toHaveProp("disabled") // Element should have prop disabled
44
+
45
+ Expected the element to have prop:
46
+ disabled
47
+ Received:
48
+ null"
49
+ ` ) ;
50
+ expect ( ( ) => expect ( text ) . not . toHaveProp ( 'allowFontScaling' , false ) )
51
+ . toThrowErrorMatchingInlineSnapshot ( `
52
+ "expect(element).not.toHaveProp("allowFontScaling", false) // Element should have prop allowFontScaling with value false
53
+
54
+ Expected the element not to have prop:
55
+ allowFontScaling=false
56
+ Received:
57
+ allowFontScaling=false"
58
+ ` ) ;
59
+ expect ( ( ) => expect ( text ) . toHaveProp ( 'style' ) )
60
+ . toThrowErrorMatchingInlineSnapshot ( `
61
+ "expect(element).toHaveProp("style") // Element should have prop style
62
+
63
+ Expected the element to have prop:
64
+ style
65
+ Received:
66
+ null"
67
+ ` ) ;
68
+ expect ( ( ) => expect ( text ) . toHaveProp ( 'allowFontScaling' , 'wrongValue' ) )
69
+ . toThrowErrorMatchingInlineSnapshot ( `
70
+ "expect(element).toHaveProp("allowFontScaling", "wrongValue") // Element should have prop allowFontScaling with value "wrongValue"
71
+
72
+ Expected the element to have prop:
73
+ allowFontScaling="wrongValue"
74
+ Received:
75
+ allowFontScaling=false"
76
+ ` ) ;
77
+
78
+ expect ( view ) . toHaveProp ( 'style' , null ) ;
43
79
} ) ;
0 commit comments