1
- import type { Action , Dispatch } from "@reduxjs/toolkit"
2
- import { configureStore } from "@reduxjs/toolkit"
3
- import { screen , waitFor } from "@testing-library/react-native"
4
- import { Component , PureComponent , type PropsWithChildren } from "react"
5
- import type { TextStyle } from "react-native"
6
- import { Button , Text , View } from "react-native"
7
- import { connect , Provider } from "react-redux"
8
- import { App } from "./App"
9
- import { renderWithProviders } from "./src/utils/test-utils"
10
-
11
- test ( "App should have correct initial render" , ( ) => {
1
+ import type { Action } from '@reduxjs/toolkit'
2
+ import { configureStore } from '@reduxjs/toolkit'
3
+ import { act , screen } from '@testing-library/react-native'
4
+ import type { Dispatch , PropsWithChildren } from 'react'
5
+ import { Component , PureComponent } from 'react'
6
+ import type { TextStyle } from 'react-native'
7
+ import { Button , Text , View } from 'react-native'
8
+ import { connect , Provider } from 'react-redux'
9
+ import { App } from './App'
10
+ import { renderWithProviders } from './src/utils/test-utils'
11
+
12
+ test ( 'App should have correct initial render' , ( ) => {
12
13
renderWithProviders ( < App /> )
13
14
15
+ const countLabel = screen . getByLabelText ( 'Count' )
16
+
17
+ const incrementValueInput = screen . getByLabelText ( 'Set increment amount' )
18
+
14
19
// The app should be rendered correctly
15
20
expect ( screen . getByText ( / l e a r n m o r e r e d u x / i) ) . toBeOnTheScreen ( )
16
21
17
22
// Initial state: count should be 0, incrementValue should be 2
18
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "0" )
19
- expect ( screen . getByLabelText ( "Set increment amount" ) ) . toHaveDisplayValue ( "2" )
23
+ expect ( countLabel ) . toHaveTextContent ( '0' )
24
+ expect ( incrementValueInput ) . toHaveDisplayValue ( '2' )
20
25
} )
21
26
22
- test ( " Increment value and Decrement value should work as expected" , async ( ) => {
27
+ test ( ' Increment value and Decrement value should work as expected' , async ( ) => {
23
28
const { user } = renderWithProviders ( < App /> )
24
29
30
+ const countLabel = screen . getByLabelText ( 'Count' )
31
+
32
+ const incrementValueButton = screen . getByLabelText ( 'Increment value' )
33
+
34
+ const decrementValueButton = screen . getByLabelText ( 'Decrement value' )
35
+
25
36
// Click on "+" => Count should be 1
26
- await user . press ( screen . getByLabelText ( "Increment value" ) )
27
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "1" )
37
+ await user . press ( incrementValueButton )
38
+ expect ( countLabel ) . toHaveTextContent ( '1' )
28
39
29
40
// Click on "-" => Count should be 0
30
- await user . press ( screen . getByLabelText ( "Decrement value" ) )
31
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "0" )
41
+ await user . press ( decrementValueButton )
42
+ expect ( countLabel ) . toHaveTextContent ( '0' )
32
43
} )
33
44
34
- test ( " Add Amount should work as expected" , async ( ) => {
45
+ test ( ' Add Amount should work as expected' , async ( ) => {
35
46
const { user } = renderWithProviders ( < App /> )
36
47
48
+ const countLabel = screen . getByLabelText ( 'Count' )
49
+
50
+ const incrementValueInput = screen . getByLabelText ( 'Set increment amount' )
51
+
52
+ const addAmountButton = screen . getByText ( 'Add Amount' )
53
+
37
54
// "Add Amount" button is clicked => Count should be 2
38
- await user . press ( screen . getByText ( "Add Amount" ) )
39
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "2" )
55
+ await user . press ( addAmountButton )
56
+ expect ( countLabel ) . toHaveTextContent ( '2' )
40
57
41
- const incrementValueInput = screen . getByLabelText ( "Set increment amount" )
42
58
// incrementValue is 2, click on "Add Amount" => Count should be 4
43
59
await user . clear ( incrementValueInput )
44
- await user . type ( incrementValueInput , "2" )
45
- await user . press ( screen . getByText ( "Add Amount" ) )
46
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "4" )
60
+ await user . type ( incrementValueInput , '2' )
61
+ await user . press ( addAmountButton )
62
+ expect ( countLabel ) . toHaveTextContent ( '4' )
47
63
48
64
// [Negative number] incrementValue is -1, click on "Add Amount" => Count should be 3
49
65
await user . clear ( incrementValueInput )
50
- await user . type ( incrementValueInput , "-1" )
51
- await user . press ( screen . getByText ( "Add Amount" ) )
52
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "3" )
66
+ await user . type ( incrementValueInput , '-1' )
67
+ await user . press ( addAmountButton )
68
+ expect ( countLabel ) . toHaveTextContent ( '3' )
53
69
} )
54
70
55
- it ( " Add Async should work as expected" , async ( ) => {
71
+ it ( ' Add Async should work as expected' , async ( ) => {
56
72
const { user } = renderWithProviders ( < App /> )
57
73
74
+ const addAsyncButton = screen . getByText ( 'Add Async' )
75
+
76
+ const countLabel = screen . getByLabelText ( 'Count' )
77
+
78
+ const incrementValueInput = screen . getByLabelText ( 'Set increment amount' )
79
+
58
80
// "Add Async" button is clicked => Count should be 2
59
- await user . press ( screen . getByText ( "Add Async" ) )
81
+ await user . press ( addAsyncButton )
60
82
61
- await waitFor ( ( ) => {
62
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "2" )
83
+ await act ( async ( ) => {
84
+ await jest . advanceTimersByTimeAsync ( 500 )
63
85
} )
64
86
65
- const incrementValueInput = screen . getByLabelText ( "Set increment amount" )
87
+ expect ( countLabel ) . toHaveTextContent ( '2' )
88
+
66
89
// incrementValue is 2, click on "Add Async" => Count should be 4
67
90
await user . clear ( incrementValueInput )
68
- await user . type ( incrementValueInput , "2" )
91
+ await user . type ( incrementValueInput , '2' )
92
+
93
+ await user . press ( addAsyncButton )
69
94
70
- await user . press ( screen . getByText ( "Add Async" ) )
71
- await waitFor ( ( ) => {
72
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "4" )
95
+ await act ( async ( ) => {
96
+ await jest . advanceTimersByTimeAsync ( 500 )
73
97
} )
74
98
99
+ expect ( countLabel ) . toHaveTextContent ( '4' )
100
+
75
101
// [Negative number] incrementValue is -1, click on "Add Async" => Count should be 3
76
102
await user . clear ( incrementValueInput )
77
- await user . type ( incrementValueInput , "-1" )
78
- await user . press ( screen . getByText ( "Add Async" ) )
79
- await waitFor ( ( ) => {
80
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "3" )
103
+ await user . type ( incrementValueInput , '-1' )
104
+ await user . press ( addAsyncButton )
105
+
106
+ await act ( async ( ) => {
107
+ await jest . advanceTimersByTimeAsync ( 500 )
81
108
} )
109
+
110
+ expect ( countLabel ) . toHaveTextContent ( '3' )
82
111
} )
83
112
84
- test ( " Add If Odd should work as expected" , async ( ) => {
113
+ test ( ' Add If Odd should work as expected' , async ( ) => {
85
114
const { user } = renderWithProviders ( < App /> )
86
115
116
+ const countLabel = screen . getByLabelText ( 'Count' )
117
+
118
+ const addIfOddButton = screen . getByText ( 'Add If Odd' )
119
+
120
+ const incrementValueInput = screen . getByLabelText ( 'Set increment amount' )
121
+
122
+ const incrementValueButton = screen . getByLabelText ( 'Increment value' )
123
+
87
124
// "Add If Odd" button is clicked => Count should stay 0
88
- await user . press ( screen . getByText ( "Add If Odd" ) )
89
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "0" )
125
+ await user . press ( addIfOddButton )
126
+ expect ( countLabel ) . toHaveTextContent ( '0' )
90
127
91
128
// Click on "+" => Count should be updated to 1
92
- await user . press ( screen . getByLabelText ( "Increment value" ) )
93
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "1" )
129
+ await user . press ( incrementValueButton )
130
+ expect ( countLabel ) . toHaveTextContent ( '1' )
94
131
95
132
// "Add If Odd" button is clicked => Count should be updated to 3
96
- await user . press ( screen . getByText ( "Add If Odd" ) )
97
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "3" )
133
+ await user . press ( addIfOddButton )
134
+ expect ( countLabel ) . toHaveTextContent ( '3' )
98
135
99
- const incrementValueInput = screen . getByLabelText ( "Set increment amount" )
100
136
// incrementValue is 1, click on "Add If Odd" => Count should be updated to 4
101
137
await user . clear ( incrementValueInput )
102
- await user . type ( incrementValueInput , "1" )
103
- await user . press ( screen . getByText ( "Add If Odd" ) )
104
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "4" )
138
+ await user . type ( incrementValueInput , '1' )
139
+ await user . press ( addIfOddButton )
140
+ expect ( countLabel ) . toHaveTextContent ( '4' )
105
141
106
142
// click on "Add If Odd" => Count should stay 4
107
143
await user . clear ( incrementValueInput )
108
- await user . type ( incrementValueInput , "-1" )
109
- await user . press ( screen . getByText ( "Add If Odd" ) )
110
- expect ( screen . getByLabelText ( "Count" ) ) . toHaveTextContent ( "4" )
144
+ await user . type ( incrementValueInput , '-1' )
145
+ await user . press ( addIfOddButton )
146
+ expect ( countLabel ) . toHaveTextContent ( '4' )
111
147
} )
112
148
113
- test ( " React-Redux issue #2150: Nested component updates should be properly batched when using connect" , async ( ) => {
149
+ test ( ' React-Redux issue #2150: Nested component updates should be properly batched when using connect' , async ( ) => {
114
150
// Original Issue: https://github.com/reduxjs/react-redux/issues/2150
115
151
// Solution: https://github.com/reduxjs/react-redux/pull/2156
116
152
117
153
// Actions
118
- const ADD = " ADD"
119
- const DATE = " DATE"
154
+ const ADD = ' ADD'
155
+ const DATE = ' DATE'
120
156
121
157
// Action types
122
- interface AddAction extends Action < string > { }
123
- interface DateAction extends Action < string > {
158
+ type AddAction = { } & Action
159
+ type DateAction = {
124
160
payload ?: { date : number }
125
- }
161
+ } & Action
126
162
127
163
// Reducer states
128
- interface DateState {
164
+ type DateState = {
129
165
date : number | null
130
166
}
131
167
132
- interface CounterState {
168
+ type CounterState = {
133
169
count : number
134
170
}
135
171
@@ -173,7 +209,7 @@ test("React-Redux issue #2150: Nested component updates should be properly batch
173
209
} )
174
210
175
211
// ======== COMPONENTS =========
176
- interface CounterProps {
212
+ type CounterProps = {
177
213
count ?: number
178
214
date ?: number | null
179
215
dispatch : Dispatch < AddAction | DateAction >
@@ -192,7 +228,7 @@ test("React-Redux issue #2150: Nested component updates should be properly batch
192
228
render ( ) {
193
229
return (
194
230
< View style = { { paddingVertical : 20 } } >
195
- < Text testID = { `${ this . props . testID } -child` } >
231
+ < Text testID = { `${ this . props . testID ?? '' } -child` } >
196
232
Counter Value: { this . props . count }
197
233
</ Text >
198
234
< Text > date Value: { this . props . date } </ Text >
@@ -241,7 +277,7 @@ test("React-Redux issue #2150: Nested component updates should be properly batch
241
277
}
242
278
}
243
279
244
- const mapStateToPropsBreaking = ( _state : any ) => ( { } )
280
+ const mapStateToPropsBreaking = ( _state : unknown ) => ( { } )
245
281
246
282
const ContainerBad = connect ( mapStateToPropsBreaking , null ) ( Container )
247
283
@@ -254,7 +290,7 @@ test("React-Redux issue #2150: Nested component updates should be properly batch
254
290
null ,
255
291
) ( Container )
256
292
257
- const mapStateToPropsNonBlocking2 = ( state : any ) => ( { state } )
293
+ const mapStateToPropsNonBlocking2 = ( state : unknown ) => ( { state } )
258
294
259
295
const ContainerNonBlocking2 = connect (
260
296
mapStateToPropsNonBlocking2 ,
@@ -303,13 +339,13 @@ test("React-Redux issue #2150: Nested component updates should be properly batch
303
339
304
340
const { user, getByTestId, getByText } = renderWithProviders ( < MainApp /> )
305
341
306
- expect ( getByTestId ( " undesired-child" ) ) . toHaveTextContent ( " Counter Value: 0" )
342
+ expect ( getByTestId ( ' undesired-child' ) ) . toHaveTextContent ( ' Counter Value: 0' )
307
343
308
- await user . press ( getByText ( " Increment Counter" ) )
344
+ await user . press ( getByText ( ' Increment Counter' ) )
309
345
310
- expect ( getByTestId ( " inconsistent-child" ) ) . toHaveTextContent (
311
- " Counter Value: 1" ,
346
+ expect ( getByTestId ( ' inconsistent-child' ) ) . toHaveTextContent (
347
+ ' Counter Value: 1' ,
312
348
)
313
349
314
- expect ( getByTestId ( " undesired-child" ) ) . toHaveTextContent ( " Counter Value: 1" )
350
+ expect ( getByTestId ( ' undesired-child' ) ) . toHaveTextContent ( ' Counter Value: 1' )
315
351
} )
0 commit comments