-
Notifications
You must be signed in to change notification settings - Fork 137
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
Unit Test Case for useDeepCompareMemoize #2373
Unit Test Case for useDeepCompareMemoize #2373
Conversation
expect(result.current).toEqual(initialData); | ||
|
||
// Re-render with new data, expect result to change | ||
rerender({ data: newData }); | ||
expect(result.current).toEqual(newData); | ||
|
||
// Re-render with identical data, expect result not to change | ||
rerender({ data: identicalData }); | ||
expect(result.current).toEqual(initialData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the tests to use toBe
since identity equality is the most important aspect of this hook.
const { result, rerender } = renderHook(({ data }) => useDeepCompareMemoize(data), { | ||
initialProps: { data: initialData }, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you can still use testHook
to simplify the tests somewhat since you aren't needing the extra functionality that comes with renderHook
.
c7e17e6
to
8ea514c
Compare
@christianvogt Updated accordingly. |
/retest |
// Re-render with new data, expect result to change | ||
expect(testHook(useDeepCompareMemoize)(newData).result.current).toBe(newData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you've done is not a re-render but a brand new render.
testHook
returns a rerender
function that should be used here.
Please see hooks.spec.ts
for examples on rerender
.
|
||
// Re-render with an updated value, expect the result to change | ||
rerender(updatedValue); | ||
expect(result.current).toBe(updatedValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make use of the hook
functions (see hooks.spec.ts
for examples). While what you've done is correct, it's good to see at least the hookToBeStable()
function being used to assert stability when we expect a stable value after a rerender.
expect(result.current).toBe(updatedValue); | |
expect(result).hookToBe(updatedValue); | |
expect(result).hookToBeStable(); |
8ea514c
to
8d1241e
Compare
@christianvogt thanks for pointing this out. Updated it accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure you test stability of all hook results when testing hook functions.
// Re-render with new data, expect result to change | ||
renderResult.rerender(newData); | ||
expect(renderResult).hookToBe(newData); | ||
expect(renderResult).hookToHaveUpdateCount(2); | ||
|
||
// Re-render with identical data, expect result not to change | ||
renderResult.rerender(identicalData); | ||
expect(renderResult).hookToStrictEqual(initialData); | ||
expect(renderResult).hookToHaveUpdateCount(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Re-render with new data, expect result to change | |
renderResult.rerender(newData); | |
expect(renderResult).hookToBe(newData); | |
expect(renderResult).hookToHaveUpdateCount(2); | |
// Re-render with identical data, expect result not to change | |
renderResult.rerender(identicalData); | |
expect(renderResult).hookToStrictEqual(initialData); | |
expect(renderResult).hookToHaveUpdateCount(3); | |
// Re-render with identical data, expect result not to change | |
renderResult.rerender(identicalData); | |
expect(renderResult).hookToBe(initialData); | |
expect(renderResult).hookToBeStable(); | |
expect(renderResult).hookToHaveUpdateCount(2); | |
// Re-render with new data, expect result to change | |
renderResult.rerender(newData); | |
expect(renderResult).hookToBe(newData); | |
expect(renderResult).hookToHaveUpdateCount(3); |
// Re-render with new data, expect result to change | ||
renderResult.rerender(newNumber); | ||
expect(renderResult).hookToBe(newNumber); | ||
expect(renderResult).hookToHaveUpdateCount(2); | ||
|
||
// Re-render with identical data, expect result not to change | ||
renderResult.rerender(identicalNumber); | ||
expect(renderResult).hookToStrictEqual(initialNumber); | ||
expect(renderResult).hookToHaveUpdateCount(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Re-render with new data, expect result to change | |
renderResult.rerender(newNumber); | |
expect(renderResult).hookToBe(newNumber); | |
expect(renderResult).hookToHaveUpdateCount(2); | |
// Re-render with identical data, expect result not to change | |
renderResult.rerender(identicalNumber); | |
expect(renderResult).hookToStrictEqual(initialNumber); | |
expect(renderResult).hookToHaveUpdateCount(3); | |
// Re-render with identical data, expect result not to change | |
renderResult.rerender(identicalNumber); | |
expect(renderResult).hookToBe(identicalNumber); | |
expect(renderResult).hookToBeStable(); | |
expect(renderResult).hookToHaveUpdateCount(2); | |
// Re-render with new data, expect result to change | |
renderResult.rerender(newNumber); | |
expect(renderResult).hookToBe(newNumber); | |
expect(renderResult).hookToHaveUpdateCount(3); |
// Re-render with new data, expect result to change | ||
renderResult.rerender(newArray); | ||
expect(renderResult).hookToBe(newArray); | ||
expect(renderResult).hookToHaveUpdateCount(2); | ||
|
||
// Re-render with identical data, expect result not to change | ||
renderResult.rerender(identicalArray); | ||
expect(renderResult).hookToStrictEqual(initialArray); | ||
expect(renderResult).hookToHaveUpdateCount(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Re-render with new data, expect result to change | |
renderResult.rerender(newArray); | |
expect(renderResult).hookToBe(newArray); | |
expect(renderResult).hookToHaveUpdateCount(2); | |
// Re-render with identical data, expect result not to change | |
renderResult.rerender(identicalArray); | |
expect(renderResult).hookToStrictEqual(initialArray); | |
expect(renderResult).hookToHaveUpdateCount(3); | |
// Re-render with identical data, expect result not to change | |
renderResult.rerender(identicalArray); | |
expect(renderResult).hookToBe(initialArray); | |
expect(renderResult).hookToBeStable(); | |
expect(renderResult).hookToHaveUpdateCount(2); | |
// Re-render with new data, expect result to change | |
renderResult.rerender(newArray); | |
expect(renderResult).hookToBe(newArray); | |
expect(renderResult).hookToHaveUpdateCount(3); |
6830aee
to
252da3b
Compare
252da3b
to
3307a58
Compare
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: christianvogt The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Closes: RHOAIENG-2091
Description
Unit Test Case for useDeepCompareMemoize
How Has This Been Tested?
npm run test
Test Impact
Request review criteria:
Self checklist (all need to be checked):
If you have UI changes:
After the PR is posted & before it merges:
main