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

Unit Test Case for useDeepCompareMemoize #2373

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

uidoyen
Copy link
Contributor

@uidoyen uidoyen commented Jan 22, 2024

Closes: RHOAIENG-2091

Description

Unit Test Case for useDeepCompareMemoize

How Has This Been Tested?

npm run test

Test Impact

Screenshot 2024-01-23 at 2 05 56 AM

Request review criteria:

Self checklist (all need to be checked):

  • The developer has manually tested the changes and verified that the changes work
  • Commits have been squashed into descriptive, self-contained units of work (e.g. 'WIP' and 'Implements feedback' style messages have been removed)
  • Testing instructions have been added in the PR body (for PRs involving changes that are not immediately obvious).
  • The developer has added tests or explained why testing cannot be added (unit tests & storybook for related changes)

If you have UI changes:

  • Included any necessary screenshots or gifs if it was a UI change.
  • Included tags to the UX team if it was a UI/UX change (find relevant UX in the SMEs section).

After the PR is posted & before it merges:

  • The developer has tested their solution on a cluster by using the image produced by the PR to main

@uidoyen uidoyen changed the title Rhoaieng 2091 Unit Test Case for useDeepCompareMemoize Jan 22, 2024
Comment on lines 32 to 40
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);
Copy link
Contributor

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.

Comment on lines 28 to 30
const { result, rerender } = renderHook(({ data }) => useDeepCompareMemoize(data), {
initialProps: { data: initialData },
});
Copy link
Contributor

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.

@uidoyen uidoyen force-pushed the RHOAIENG-2091 branch 2 times, most recently from c7e17e6 to 8ea514c Compare January 23, 2024 09:21
@uidoyen
Copy link
Contributor Author

uidoyen commented Jan 23, 2024

@christianvogt Updated accordingly.

@christianvogt
Copy link
Contributor

/retest

Comment on lines 31 to 32
// Re-render with new data, expect result to change
expect(testHook(useDeepCompareMemoize)(newData).result.current).toBe(newData);
Copy link
Contributor

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);
Copy link
Contributor

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.

Suggested change
expect(result.current).toBe(updatedValue);
expect(result).hookToBe(updatedValue);
expect(result).hookToBeStable();

@uidoyen
Copy link
Contributor Author

uidoyen commented Jan 23, 2024

@christianvogt thanks for pointing this out. Updated it accordingly.

Copy link
Contributor

@christianvogt christianvogt left a 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.

Comment on lines 14 to 23
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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);

Comment on lines 34 to 44
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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);

Comment on lines 54 to 65
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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);

@christianvogt
Copy link
Contributor

/lgtm
/approve

Copy link
Contributor

openshift-ci bot commented Jan 23, 2024

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit d8fbf39 into opendatahub-io:main Jan 23, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants