@@ -101,4 +101,45 @@ describe('async hook tests', () => {
101101 )
102102 ) . rejects . toThrow ( Error ( 'Timed out in wait after 75ms.' ) )
103103 } )
104+
105+ test ( 'should wait for value to change' , async ( ) => {
106+ const { result, waitForValueToChange } = renderHook ( ( ) =>
107+ useSequence ( 'first' , 'second' , 'third' )
108+ )
109+
110+ expect ( result . current ) . toBe ( 'first' )
111+
112+ await waitForValueToChange ( ( ) => result . current === 'third' )
113+
114+ expect ( result . current ) . toBe ( 'third' )
115+ } )
116+
117+ test ( 'should reject if timeout exceeded when waiting for value to change' , async ( ) => {
118+ const { result, waitForValueToChange } = renderHook ( ( ) =>
119+ useSequence ( 'first' , 'second' , 'third' )
120+ )
121+
122+ expect ( result . current ) . toBe ( 'first' )
123+
124+ await expect (
125+ waitForValueToChange ( ( ) => result . current === 'third' , {
126+ timeout : 75
127+ } )
128+ ) . rejects . toThrow ( Error ( 'Timed out in waitForValueToChange after 75ms.' ) )
129+ } )
130+
131+ test ( 'should reject if selector throws error' , async ( ) => {
132+ const { result, waitForValueToChange } = renderHook ( ( ) => useSequence ( 'first' , 'second' ) )
133+
134+ expect ( result . current ) . toBe ( 'first' )
135+
136+ await expect (
137+ waitForValueToChange ( ( ) => {
138+ if ( result . current === 'second' ) {
139+ throw new Error ( 'Something Unexpected' )
140+ }
141+ return result . current
142+ } )
143+ ) . rejects . toThrow ( Error ( 'Something Unexpected' ) )
144+ } )
104145} )
0 commit comments