1
1
import { act } from "react-test-renderer" ;
2
- import { Repeater } from "@repeaterjs/repeater" ;
2
+ import { Push , Repeater , Stop } from "@repeaterjs/repeater" ;
3
3
import { renderHook } from "@testing-library/react-hooks" ;
4
4
import { useAsyncIter , useRepeater , useResult , useValue } from "../react-hooks" ;
5
5
@@ -98,8 +98,8 @@ describe("useAsyncIter", () => {
98
98
99
99
describe ( "useResult" , ( ) => {
100
100
test ( "basic" , async ( ) => {
101
- let push : ( value : number ) => Promise < void > ;
102
- let stop : ( ( ) => void ) & Promise < void > ;
101
+ let push : Push < number > ;
102
+ let stop : Stop ;
103
103
const repeater = new Repeater ( async ( push1 , stop1 ) => {
104
104
push = push1 ;
105
105
stop = stop1 ;
@@ -115,22 +115,21 @@ describe("useResult", () => {
115
115
} ) ;
116
116
117
117
expect ( result . current ) . toBeUndefined ( ) ;
118
- await act ( ( ) => push ( 1 ) ) ;
118
+ await act ( async ( ) => void ( await push ( 1 ) ) ) ;
119
119
expect ( result . current ) . toEqual ( { value : 1 , done : false } ) ;
120
- await act ( ( ) => push ( 2 ) ) ;
120
+ await act ( async ( ) => void ( await push ( 2 ) ) ) ;
121
121
expect ( result . current ) . toEqual ( { value : 2 , done : false } ) ;
122
- await act ( ( ) => push ( 3 ) ) ;
122
+ await act ( async ( ) => void ( await push ( 3 ) ) ) ;
123
123
expect ( result . current ) . toEqual ( { value : 3 , done : false } ) ;
124
- await act ( ( ) => push ( 4 ) ) ;
124
+ await act ( async ( ) => void ( await push ( 4 ) ) ) ;
125
125
expect ( result . current ) . toEqual ( { value : 4 , done : false } ) ;
126
- // we have to return stop here to stop act from yelling at us
127
126
await act ( async ( ) => ( stop ( ) , stop ) ) ;
128
127
expect ( result . current ) . toEqual ( { value : - 1 , done : true } ) ;
129
128
expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
130
129
} ) ;
131
130
132
131
test ( "unmount" , async ( ) => {
133
- let push : ( value : number ) => Promise < void > ;
132
+ let push : Push < number > ;
134
133
const repeater = new Repeater ( async ( push1 ) => {
135
134
push = push1 ;
136
135
return - 1 ;
@@ -145,7 +144,7 @@ describe("useResult", () => {
145
144
} ) ;
146
145
147
146
expect ( result . current ) . toBeUndefined ( ) ;
148
- await act ( ( ) => push ( 1 ) ) ;
147
+ await act ( async ( ) => void ( await push ( 1 ) ) ) ;
149
148
expect ( result . current ) . toEqual ( { value : 1 , done : false } ) ;
150
149
unmount ( ) ;
151
150
expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -154,11 +153,7 @@ describe("useResult", () => {
154
153
155
154
test ( "deps" , async ( ) => {
156
155
const callback = jest . fn ( ( deps ) => {
157
- return new Repeater < number > ( async ( push1 ) => {
158
- const push = async ( num : number ) => {
159
- return act ( ( ) => push1 ( num ) ) ;
160
- } ;
161
-
156
+ return new Repeater < number > ( async ( push ) => {
162
157
for await ( const [ num ] of deps ) {
163
158
await push ( num ) ;
164
159
}
@@ -191,8 +186,8 @@ describe("useResult", () => {
191
186
192
187
describe ( "useValue" , ( ) => {
193
188
test ( "basic" , async ( ) => {
194
- let push : ( value : number ) => Promise < void > ;
195
- let stop : ( ( ) => void ) & Promise < void > ;
189
+ let push : Push < number > ;
190
+ let stop : Stop ;
196
191
const repeater = new Repeater ( async ( push1 , stop1 ) => {
197
192
push = push1 ;
198
193
stop = stop1 ;
@@ -208,15 +203,14 @@ describe("useValue", () => {
208
203
} ) ;
209
204
210
205
expect ( result . current ) . toBeUndefined ( ) ;
211
- await act ( ( ) => push ( 1 ) ) ;
206
+ await act ( async ( ) => void ( await push ( 1 ) ) ) ;
212
207
expect ( result . current ) . toBe ( 1 ) ;
213
- await act ( ( ) => push ( 2 ) ) ;
208
+ await act ( async ( ) => void ( await push ( 2 ) ) ) ;
214
209
expect ( result . current ) . toBe ( 2 ) ;
215
- await act ( ( ) => push ( 3 ) ) ;
210
+ await act ( async ( ) => void ( await push ( 3 ) ) ) ;
216
211
expect ( result . current ) . toBe ( 3 ) ;
217
- await act ( ( ) => push ( 4 ) ) ;
212
+ await act ( async ( ) => void ( await push ( 4 ) ) ) ;
218
213
expect ( result . current ) . toBe ( 4 ) ;
219
- // we have to return stop here to stop act from yelling at us
220
214
await act ( async ( ) => ( stop ( ) , stop ) ) ;
221
215
expect ( result . current ) . toBe ( - 1 ) ;
222
216
rerender ( ) ;
@@ -225,7 +219,7 @@ describe("useValue", () => {
225
219
} ) ;
226
220
227
221
test ( "unmount" , async ( ) => {
228
- let push : ( value : number ) => Promise < void > ;
222
+ let push : Push < number > ;
229
223
const repeater = new Repeater ( async ( push1 ) => {
230
224
push = push1 ;
231
225
return - 1 ;
@@ -241,7 +235,7 @@ describe("useValue", () => {
241
235
} ) ;
242
236
243
237
expect ( result . current ) . toBeUndefined ( ) ;
244
- await act ( ( ) => push ( 1 ) ) ;
238
+ await act ( async ( ) => void ( await push ( 1 ) ) ) ;
245
239
expect ( result . current ) . toBe ( 1 ) ;
246
240
unmount ( ) ;
247
241
expect ( callback ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -250,11 +244,7 @@ describe("useValue", () => {
250
244
251
245
test ( "deps" , async ( ) => {
252
246
const callback = jest . fn ( ( deps ) => {
253
- return new Repeater < number > ( async ( push1 ) => {
254
- const push = async ( num : number ) => {
255
- return act ( ( ) => push1 ( num ) ) ;
256
- } ;
257
-
247
+ return new Repeater < number > ( async ( push ) => {
258
248
for await ( const [ num ] of deps ) {
259
249
await push ( num ) ;
260
250
}
0 commit comments