Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture all results a rendered hook produces. #496

Merged
merged 6 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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])
})
})