File tree Expand file tree Collapse file tree 3 files changed +50
-9
lines changed Expand file tree Collapse file tree 3 files changed +50
-9
lines changed Original file line number Diff line number Diff line change @@ -63,13 +63,17 @@ The `renderHook` function returns an object that has the following properties:
6363
6464```js
6565{
66+ all: Array < any>
6667 current: any,
6768 error: Error
6869}
6970` ` `
7071
71- The ` current ` value or the ` result ` will reflect whatever is returned from the ` callback ` passed to
72- ` renderHook ` . Any thrown values will be reflected in the ` error ` value of the ` result ` .
72+ The ` current ` value or the ` result ` will reflect the latest of whatever is returned from the
73+ ` callback ` passed to ` renderHook ` . Any thrown values from the latest call will be reflected in the
74+ ` error ` value of the ` result ` . The ` all ` value is an array containing all the returns (including the
75+ most recent) from the callback. These could be ` result ` or an ` error ` depending on what the callback
76+ returned at the time.
7377
7478### ` rerender `
7579
Original file line number Diff line number Diff line change @@ -21,25 +21,28 @@ function Fallback() {
2121}
2222
2323function resultContainer ( ) {
24- let value = null
25- let error = null
24+ const results = [ ]
2625 const resolvers = [ ]
2726
2827 const result = {
28+ get all ( ) {
29+ return results . map ( ( { value, error } ) => error || value )
30+ } ,
2931 get current ( ) {
32+ const { value, error } = results [ results . length - 1 ]
3033 if ( error ) {
3134 throw error
3235 }
3336 return value
3437 } ,
3538 get error ( ) {
39+ const { error } = results [ results . length - 1 ]
3640 return error
3741 }
3842 }
3943
40- const updateResult = ( val , err ) => {
41- value = val
42- error = err
44+ const updateResult = ( value , error ) => {
45+ results . push ( { value, error } )
4346 resolvers . splice ( 0 , resolvers . length ) . forEach ( ( resolve ) => resolve ( ) )
4447 }
4548
@@ -48,8 +51,8 @@ function resultContainer() {
4851 addResolver : ( resolver ) => {
4952 resolvers . push ( resolver )
5053 } ,
51- setValue : ( val ) => updateResult ( val ) ,
52- setError : ( err ) => updateResult ( undefined , err )
54+ setValue : ( value ) => updateResult ( value ) ,
55+ setError : ( error ) => updateResult ( undefined , error )
5356 }
5457}
5558
Original file line number Diff line number Diff line change 1+ import { renderHook } from '../src'
2+
3+ describe ( 'result history tests' , ( ) => {
4+ let count = 0
5+ function useCounter ( ) {
6+ const result = count ++
7+ if ( result === 2 ) {
8+ throw Error ( 'expected' )
9+ }
10+ return result
11+ }
12+
13+ test ( 'should capture all renders states of hook' , ( ) => {
14+ const { result, rerender } = renderHook ( ( ) => useCounter ( ) )
15+
16+ expect ( result . current ) . toEqual ( 0 )
17+ expect ( result . all ) . toEqual ( [ 0 ] )
18+
19+ rerender ( )
20+
21+ expect ( result . current ) . toBe ( 1 )
22+ expect ( result . all ) . toEqual ( [ 0 , 1 ] )
23+
24+ rerender ( )
25+
26+ expect ( result . error ) . toEqual ( Error ( 'expected' ) )
27+ expect ( result . all ) . toEqual ( [ 0 , 1 , Error ( 'expected' ) ] )
28+
29+ rerender ( )
30+
31+ expect ( result . current ) . toBe ( 3 )
32+ expect ( result . all ) . toEqual ( [ 0 , 1 , Error ( 'expected' ) , 3 ] )
33+ } )
34+ } )
You can’t perform that action at this time.
0 commit comments