1
- import React from 'react' ;
2
- import { Button , Picker , Switch , Text , View , TextInput } from 'react-native' ;
1
+ import React , { useEffect , useRef } from 'react' ;
2
+ import {
3
+ Button ,
4
+ Picker ,
5
+ Pressable ,
6
+ ScrollView ,
7
+ Switch ,
8
+ Text ,
9
+ TextInput ,
10
+ TouchableHighlight ,
11
+ TouchableNativeFeedback ,
12
+ TouchableOpacity ,
13
+ TouchableWithoutFeedback ,
14
+ View ,
15
+ } from 'react-native' ;
3
16
import { toMatchDiffSnapshot } from 'snapshot-diff' ;
4
17
5
18
import { cleanup , fireEvent , render } from '../' ;
6
19
7
20
afterEach ( cleanup ) ;
8
21
22
+ test ( '<Pressable /> works' , ( ) => {
23
+ const fireZeMissiles = jest . fn ( ) ;
24
+
25
+ function Wrapper ( ) {
26
+ return (
27
+ < Pressable onPress = { fireZeMissiles } >
28
+ < Text > missiles</ Text >
29
+ </ Pressable >
30
+ ) ;
31
+ }
32
+ const { getByText } = render ( < Wrapper /> ) ;
33
+
34
+ fireEvent . press ( getByText ( 'missiles' ) ) ;
35
+ expect ( fireZeMissiles ) . toBeCalledTimes ( 1 ) ;
36
+ } ) ;
37
+
9
38
test ( '<Picker /> works' , ( ) => {
10
39
function Wrapper ( ) {
11
40
const [ value , setValue ] = React . useState ( 'js' ) ;
@@ -23,6 +52,95 @@ test('<Picker /> works', () => {
23
52
expect ( ( ) => findByDisplayValue ( 'js' ) ) . not . toThrow ( ) ;
24
53
} ) ;
25
54
55
+ test ( '<ScrollView /> instance methods are mocked' , ( ) => {
56
+ function Wrapper ( ) {
57
+ const ref = useRef ( ) ;
58
+
59
+ useEffect ( ( ) => {
60
+ ref . current . scrollTo ( 0 ) ;
61
+ } , [ ] ) ;
62
+
63
+ return (
64
+ < ScrollView ref = { ref } >
65
+ < Text > Some content</ Text >
66
+ </ ScrollView >
67
+ ) ;
68
+ }
69
+ const { getByText, debug } = render ( < Wrapper /> ) ;
70
+
71
+ expect ( ( ) => getByText ( 'Some content' ) ) . not . toThrow ( ) ;
72
+ } ) ;
73
+
74
+ test ( '<TextInput /> instance methods are mocked' , ( ) => {
75
+ function Wrapper ( ) {
76
+ const ref = useRef ( ) ;
77
+
78
+ useEffect ( ( ) => {
79
+ ref . current . clear ( ) ;
80
+ } , [ ] ) ;
81
+
82
+ return < TextInput ref = { ref } value = { 'yo' } /> ;
83
+ }
84
+ const { getByDisplayValue } = render ( < Wrapper /> ) ;
85
+
86
+ expect ( ( ) => getByDisplayValue ( 'yo' ) ) . not . toThrow ( ) ;
87
+ } ) ;
88
+
89
+ test ( 'calling a handler if a Touchable is disabled does not work' , ( ) => {
90
+ const handleEvent = jest . fn ( ) ;
91
+ const { getByText } = render (
92
+ < Pressable disabled onPress = { handleEvent } >
93
+ < Text > touchable</ Text >
94
+ </ Pressable > ,
95
+ ) ;
96
+ expect ( ( ) => fireEvent . press ( getByText ( 'touchable' ) ) ) . not . toThrow ( ) ;
97
+ expect ( handleEvent ) . toBeCalledTimes ( 0 ) ;
98
+ } ) ;
99
+
100
+ test ( 'calling a TouchableHighlight handler works' , ( ) => {
101
+ const handleEvent = jest . fn ( ) ;
102
+ const { getByText } = render (
103
+ < TouchableHighlight onPress = { handleEvent } >
104
+ < Text > touchable</ Text >
105
+ </ TouchableHighlight > ,
106
+ ) ;
107
+ expect ( ( ) => fireEvent . press ( getByText ( 'touchable' ) ) ) . not . toThrow ( ) ;
108
+ expect ( handleEvent ) . toBeCalledTimes ( 1 ) ;
109
+ } ) ;
110
+
111
+ test ( 'calling a TouchableNativeFeedback handler works' , ( ) => {
112
+ const handleEvent = jest . fn ( ) ;
113
+ const { getByText } = render (
114
+ < TouchableNativeFeedback onPress = { handleEvent } >
115
+ < Text > touchable</ Text >
116
+ </ TouchableNativeFeedback > ,
117
+ ) ;
118
+ expect ( ( ) => fireEvent . press ( getByText ( 'touchable' ) ) ) . not . toThrow ( ) ;
119
+ expect ( handleEvent ) . toBeCalledTimes ( 1 ) ;
120
+ } ) ;
121
+
122
+ test ( 'calling a TouchableOpacity handler works' , ( ) => {
123
+ const handleEvent = jest . fn ( ) ;
124
+ const { getByText } = render (
125
+ < TouchableOpacity onPress = { handleEvent } >
126
+ < Text > touchable</ Text >
127
+ </ TouchableOpacity > ,
128
+ ) ;
129
+ expect ( ( ) => fireEvent . press ( getByText ( 'touchable' ) ) ) . not . toThrow ( ) ;
130
+ expect ( handleEvent ) . toBeCalledTimes ( 1 ) ;
131
+ } ) ;
132
+
133
+ test ( 'calling a TouchableWithoutFeedback handler works ' , ( ) => {
134
+ const handleEvent = jest . fn ( ) ;
135
+ const { getByText } = render (
136
+ < TouchableWithoutFeedback onPress = { handleEvent } >
137
+ < Text > touchable</ Text >
138
+ </ TouchableWithoutFeedback > ,
139
+ ) ;
140
+ expect ( ( ) => fireEvent . press ( getByText ( 'touchable' ) ) ) . not . toThrow ( ) ;
141
+ expect ( handleEvent ) . toBeCalledTimes ( 1 ) ;
142
+ } ) ;
143
+
26
144
test ( 'fragments can show diffs' , ( ) => {
27
145
function TestComponent ( ) {
28
146
const [ count , setCount ] = React . useState ( 0 ) ;
0 commit comments