1- import { it , describe , expect , afterEach } from 'vitest' ;
1+ import { it , describe , expect , afterEach , vi } from 'vitest' ;
22import { gray } from 'colorette' ;
33import { range } from 'lodash-es' ;
44import { renderHook , cleanup as cleanupMountedReactTrees , act } from '@testing-library/react' ;
@@ -14,43 +14,6 @@ afterEach(() => {
1414} ) ;
1515
1616describe ( '`useAsyncIterState` hook' , ( ) => {
17- it ( gray ( 'Updating states iteratively with the returned setter works correctly' ) , async ( ) => {
18- const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
19-
20- const rounds = 3 ;
21-
22- const yieldsPromise = pipe ( values , asyncIterTake ( rounds ) , asyncIterToArray ) ;
23- const currentValues = [ values . value . current ] ;
24-
25- for ( let i = 0 ; i < rounds ; ++ i ) {
26- await act ( ( ) => {
27- setValue ( i ) ;
28- currentValues . push ( values . value . current ) ;
29- } ) ;
30- }
31-
32- expect ( currentValues ) . toStrictEqual ( [ undefined , 0 , 1 , 2 ] ) ;
33- expect ( await yieldsPromise ) . toStrictEqual ( [ 0 , 1 , 2 ] ) ;
34- } ) ;
35-
36- it (
37- gray ( 'Updating states as rapidly as possible with the returned setter works correctly' ) ,
38- async ( ) => {
39- const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
40-
41- const yieldPromise = pipe ( values , asyncIterTakeFirst ( ) ) ;
42- const currentValues = [ values . value . current ] ;
43-
44- for ( let i = 0 ; i < 3 ; ++ i ) {
45- setValue ( i ) ;
46- currentValues . push ( values . value . current ) ;
47- }
48-
49- expect ( currentValues ) . toStrictEqual ( [ undefined , 0 , 1 , 2 ] ) ;
50- expect ( await yieldPromise ) . toStrictEqual ( 2 ) ;
51- }
52- ) ;
53-
5417 it ( gray ( 'The returned iterable can be async-iterated upon successfully' ) , async ( ) => {
5518 const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < string > ( ) ) . result . current ;
5619
@@ -108,6 +71,93 @@ describe('`useAsyncIterState` hook', () => {
10871 }
10972 ) ;
11073
74+ it ( gray ( 'Updating states iteratively with the returned setter works correctly' ) , async ( ) => {
75+ const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
76+
77+ const rounds = 3 ;
78+
79+ const yieldsPromise = pipe ( values , asyncIterTake ( rounds ) , asyncIterToArray ) ;
80+ const currentValues = [ values . value . current ] ;
81+
82+ for ( let i = 0 ; i < rounds ; ++ i ) {
83+ await act ( ( ) => {
84+ setValue ( i ) ;
85+ currentValues . push ( values . value . current ) ;
86+ } ) ;
87+ }
88+
89+ expect ( currentValues ) . toStrictEqual ( [ undefined , 0 , 1 , 2 ] ) ;
90+ expect ( await yieldsPromise ) . toStrictEqual ( [ 0 , 1 , 2 ] ) ;
91+ } ) ;
92+
93+ it (
94+ gray ( 'Updating states as rapidly as possible with the returned setter works correctly' ) ,
95+ async ( ) => {
96+ const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
97+
98+ const yieldPromise = pipe ( values , asyncIterTakeFirst ( ) ) ;
99+ const currentValues = [ values . value . current ] ;
100+
101+ for ( let i = 0 ; i < 3 ; ++ i ) {
102+ setValue ( i ) ;
103+ currentValues . push ( values . value . current ) ;
104+ }
105+
106+ expect ( currentValues ) . toStrictEqual ( [ undefined , 0 , 1 , 2 ] ) ;
107+ expect ( await yieldPromise ) . toStrictEqual ( 2 ) ;
108+ }
109+ ) ;
110+
111+ it (
112+ gray (
113+ 'Updating states iteratively with the returned setter *in the functional form* works correctly'
114+ ) ,
115+ async ( ) => {
116+ const renderFn = vi . fn < ( prevState : number | undefined ) => number > ( ) ;
117+ const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
118+
119+ const rounds = 3 ;
120+
121+ const yieldsPromise = pipe ( values , asyncIterTake ( rounds ) , asyncIterToArray ) ;
122+ const currentValues = [ values . value . current ] ;
123+
124+ for ( let i = 0 ; i < rounds ; ++ i ) {
125+ await act ( ( ) => {
126+ setValue ( renderFn . mockImplementation ( _prev => i ) ) ;
127+ currentValues . push ( values . value . current ) ;
128+ } ) ;
129+ }
130+
131+ expect ( renderFn . mock . calls ) . toStrictEqual ( [ [ undefined ] , [ 0 ] , [ 1 ] ] ) ;
132+ expect ( currentValues ) . toStrictEqual ( [ undefined , 0 , 1 , 2 ] ) ;
133+ expect ( await yieldsPromise ) . toStrictEqual ( [ 0 , 1 , 2 ] ) ;
134+ }
135+ ) ;
136+
137+ it (
138+ gray (
139+ 'Updating states as rapidly as possible with the returned setter *in the functional form* works correctly'
140+ ) ,
141+ async ( ) => {
142+ const renderFn = vi . fn < ( prevState : number | undefined ) => number > ( ) ;
143+
144+ const [ values , setValue ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
145+
146+ const yieldPromise = pipe ( values , asyncIterTakeFirst ( ) ) ;
147+
148+ const currentValues = [ values . value . current ] ;
149+
150+ for ( let i = 0 ; i < 3 ; ++ i ) {
151+ setValue ( renderFn . mockImplementation ( _prev => i ) ) ;
152+ currentValues . push ( values . value . current ) ;
153+ }
154+
155+ expect ( renderFn . mock . calls ) . toStrictEqual ( [ [ undefined ] , [ 0 ] , [ 1 ] ] ) ;
156+ expect ( currentValues ) . toStrictEqual ( [ undefined , 0 , 1 , 2 ] ) ;
157+ expect ( await yieldPromise ) . toStrictEqual ( 2 ) ;
158+ }
159+ ) ;
160+
111161 it (
112162 gray (
113163 'When hook is unmounted, all outstanding yieldings of the returned iterable resolve to "done"'
@@ -198,7 +248,7 @@ describe('`useAsyncIterState` hook', () => {
198248 const [ values ] = renderHook ( ( ) => useAsyncIterState < number > ( ) ) . result . current ;
199249
200250 expect ( ( ) => {
201- ( values . value as any ) . current = "can't do this ..." ;
251+ ( values . value as any ) . current = `CAN'T DO THIS ...` ;
202252 } ) . toThrow ( TypeError ) ;
203253 } ) ;
204254} ) ;
0 commit comments