Skip to content

Commit

Permalink
Capture all results a rendered hook produces. (#496)
Browse files Browse the repository at this point in the history
feat: Capture all results a rendered hook produces as `result.all`

Fixes #461

Co-authored-by: Josh Ellis <joshua.ellis18@gmail.com>
  • Loading branch information
mpeyper and joshuaellis committed Dec 7, 2020
1 parent 33b8e1e commit c3e09ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
8 changes: 6 additions & 2 deletions docs/api-reference.md
Expand Up @@ -63,13 +63,17 @@ The `renderHook` function returns an object that has the following properties:

```js
{
all: Array<any>
current: any,
error: Error
}
```
The `current` value or the `result` will reflect whatever is returned from the `callback` passed to
`renderHook`. Any thrown values will be reflected in the `error` value of the `result`.
The `current` value or the `result` will reflect the latest of whatever is returned from the
`callback` passed to `renderHook`. Any thrown values from the latest call will be reflected in the
`error` value of the `result`. The `all` value is an array containing all the returns (including the
most recent) from the callback. These could be `result` or an `error` depending on what the callback
returned at the time.
### `rerender`
Expand Down
17 changes: 10 additions & 7 deletions src/pure.js
Expand Up @@ -21,25 +21,28 @@ function Fallback() {
}

function resultContainer() {
let value = null
let error = null
const results = []
const resolvers = []

const result = {
get all() {
return results.map(({ value, error }) => error || value)
},
get current() {
const { value, error } = results[results.length - 1]
if (error) {
throw error
}
return value
},
get error() {
const { error } = results[results.length - 1]
return error
}
}

const updateResult = (val, err) => {
value = val
error = err
const updateResult = (value, error) => {
results.push({ value, error })
resolvers.splice(0, resolvers.length).forEach((resolve) => resolve())
}

Expand All @@ -48,8 +51,8 @@ function resultContainer() {
addResolver: (resolver) => {
resolvers.push(resolver)
},
setValue: (val) => updateResult(val),
setError: (err) => updateResult(undefined, err)
setValue: (value) => updateResult(value),
setError: (error) => updateResult(undefined, error)
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/resultHistory.test.js
@@ -0,0 +1,34 @@
import { renderHook } from '../src'

describe('result history tests', () => {
let count = 0
function useCounter() {
const result = count++
if (result === 2) {
throw Error('expected')
}
return result
}

test('should capture all renders states of hook', () => {
const { result, rerender } = renderHook(() => useCounter())

expect(result.current).toEqual(0)
expect(result.all).toEqual([0])

rerender()

expect(result.current).toBe(1)
expect(result.all).toEqual([0, 1])

rerender()

expect(result.error).toEqual(Error('expected'))
expect(result.all).toEqual([0, 1, Error('expected')])

rerender()

expect(result.current).toBe(3)
expect(result.all).toEqual([0, 1, Error('expected'), 3])
})
})

0 comments on commit c3e09ed

Please sign in to comment.