File tree 3 files changed +50
-9
lines changed
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:
63
63
64
64
```js
65
65
{
66
+ all: Array < any>
66
67
current: any,
67
68
error: Error
68
69
}
69
70
` ` `
70
71
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.
73
77
74
78
### ` rerender `
75
79
Original file line number Diff line number Diff line change @@ -21,25 +21,28 @@ function Fallback() {
21
21
}
22
22
23
23
function resultContainer ( ) {
24
- let value = null
25
- let error = null
24
+ const results = [ ]
26
25
const resolvers = [ ]
27
26
28
27
const result = {
28
+ get all ( ) {
29
+ return results . map ( ( { value, error } ) => error || value )
30
+ } ,
29
31
get current ( ) {
32
+ const { value, error } = results [ results . length - 1 ]
30
33
if ( error ) {
31
34
throw error
32
35
}
33
36
return value
34
37
} ,
35
38
get error ( ) {
39
+ const { error } = results [ results . length - 1 ]
36
40
return error
37
41
}
38
42
}
39
43
40
- const updateResult = ( val , err ) => {
41
- value = val
42
- error = err
44
+ const updateResult = ( value , error ) => {
45
+ results . push ( { value, error } )
43
46
resolvers . splice ( 0 , resolvers . length ) . forEach ( ( resolve ) => resolve ( ) )
44
47
}
45
48
@@ -48,8 +51,8 @@ function resultContainer() {
48
51
addResolver : ( resolver ) => {
49
52
resolvers . push ( resolver )
50
53
} ,
51
- setValue : ( val ) => updateResult ( val ) ,
52
- setError : ( err ) => updateResult ( undefined , err )
54
+ setValue : ( value ) => updateResult ( value ) ,
55
+ setError : ( error ) => updateResult ( undefined , error )
53
56
}
54
57
}
55
58
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