-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Return value of hook run with testHook? #295
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
Comments
See #274 (comment) for why this doesn't work - it's due to closures |
Ok, in that case, we can do what I already mentioned somewhere else - return a getter. test('accepts a default initial value for `count`', () => {
const { getResult } = testHook(() => useCounter({}))
const { count } = getResult()
expect(count).toBe(0)
}) Alternatively, the |
Ok, I have prepared a proof of concept that both approaches work. https://codesandbox.io/s/209nj999jy?module=%2Fsrc%2FtestHook.test.js&previewwindow=tests test("using a getter", () => {
const { getResult } = testHook(() => useCounter({ step: 2 }));
let { count, increment } = getResult();
expect(count).toBe(0);
act(() => {
increment();
});
const { count: count2 } = getResult();
expect(count2).toBe(2);
}); test("using a result.current", () => {
const { result } = testHook(() => useCounter({ step: 2 }));
expect(result.current.count).toBe(0);
act(() => {
result.current.increment();
});
expect(result.current.count).toBe(2);
}); Looking at it now I might move even more inclined to |
I'm really struggling to understand how this is better than the current solution/example... |
|
Ah, I see, that makes sense. Couldn't you do: test('accepts a default initial value for `count`', () => {
const ref = {current: null}
testHook(() => (ref.current = useCounter({})))
expect(count).toBe(0)
}) I guess I can see how that could be annoying. I'd be fine with returning a ref so long as the existing solution still works. |
🎉 This issue has been resolved in version 5.8.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Honestly, it got me confused big time. Considering that most of the tests might actually want to validate the output of the custom hook I find it strange we need to do this.
So I would like to propose getting the output directly.
It can be compared to a
render
where we are gettingcontainer
(and bunch of getters) to write our expectations. ThetestHook
is kinda inconsistent in this.The text was updated successfully, but these errors were encountered: