@@ -115,8 +115,27 @@ describe(`Given the '${useAsyncTask.name}' hook`, () => {
115115 } ) ;
116116 } ) ;
117117
118+ describe ( 'When the task throws an error synchronously' , ( ) => {
119+ it ( 'Then it should revert to the previous state and rethrow the error' , async ( ) => {
120+ const { result } = renderHook ( ( ) =>
121+ useAsyncTask ( ( _ : string ) => {
122+ throw new Error ( 'test error' ) ;
123+ } , [ ] ) ,
124+ ) ;
125+ const state = result . current [ 1 ] ;
126+
127+ await act ( async ( ) => {
128+ await expect ( async ( ) => {
129+ await result . current [ 0 ] ( 'test' ) ;
130+ } ) . rejects . toThrow ( Error ) ;
131+ } ) ;
132+
133+ expect ( result . current [ 1 ] ) . toMatchObject ( state ) ;
134+ } ) ;
135+ } ) ;
136+
118137 describe ( 'When the task fails' , ( ) => {
119- it ( 'Then it should return the state in line with type of `AsyncTaskFailed `' , async ( ) => {
138+ it ( 'Then it should return the state in line with type of `AsyncTaskError `' , async ( ) => {
120139 const { result } = renderHook ( ( ) =>
121140 useAsyncTask ( ( _ : string ) => errAsync ( new Error ( 'test error' ) ) , [ ] ) ,
122141 ) ;
@@ -150,7 +169,7 @@ describe(`Given the '${useAsyncTask.name}' hook`, () => {
150169 } ) ;
151170 } ) ;
152171
153- describe ( 'And the hook is executed once with an error ' , ( ) => {
172+ describe ( 'And a previous execution failed ' , ( ) => {
154173 describe ( 'When the hook is executed again' , ( ) => {
155174 it ( 'Then it should return the state in line with type of `AsyncTaskLoading`' , async ( ) => {
156175 const { result } = renderHook ( ( ) =>
@@ -235,7 +254,7 @@ describe(`Given the '${useAsyncTask.name}' hook`, () => {
235254 } ) ;
236255
237256 describe ( 'When the task fails' , ( ) => {
238- it ( 'Then it should return the state in line with type of `AsyncTaskFailed `' , async ( ) => {
257+ it ( 'Then it should return the state in line with type of `AsyncTaskError `' , async ( ) => {
239258 const { result } = renderHook ( ( ) =>
240259 useAsyncTask ( ( input : string ) => {
241260 if ( input === 'one' ) return okAsync ( input ) ;
0 commit comments