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

17492: Increase SideNavMenu unit test coverage #17842

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mikdhadc
Copy link

@mikdhadc mikdhadc commented Oct 23, 2024

Added testcases for SideNavMenu Component that increased the test coverage

BREAKING CHANGE: The SideNavMenu now has a test coverage greater than 80%

Closes #17492

Changelog

Changed

  • Added tests case to properly test and increase the test coverage for SideNavMenu

Testing / Reviewing

Screenshot 2024-10-24 at 7 10 14 PM

Added testcases for SideNavMenu Component that increased the test coverage

BREAKING CHANGE: The SideNavMenu now has a test coverage greater than 80%
@mikdhadc mikdhadc requested a review from a team as a code owner October 23, 2024 09:18
Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for v11-carbon-web-components ready!

Name Link
🔨 Latest commit ce55b39
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-web-components/deploys/671a2fa40442f90008f4fe34
😎 Deploy Preview https://deploy-preview-17842--v11-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mikdhadc mikdhadc changed the title test: added testcases for SideNavMenu 17492: Increase SideNavMenu unit test coverage Oct 23, 2024
Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 65366b1
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/6718bf4dbe959100089d7375
😎 Deploy Preview https://deploy-preview-17842--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 65366b1
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/6718bf4dcfda6f000882d749
😎 Deploy Preview https://deploy-preview-17842--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit ce55b39
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/671a2fa4b390640008b737cd
😎 Deploy Preview https://deploy-preview-17842--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ce55b39
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/671a2fa42b38f00008bea161
😎 Deploy Preview https://deploy-preview-17842--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@tay1orjones tay1orjones left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on! I have a couple points of feedback below, let me know what you think.

Comment on lines 106 to 118
it('should return true if the child is a valid React element, and instance of Array and has isActive and aria-current props', () => {
const child = [
<SideNavMenuItem isActive={true} aria-current="page">
<SideNavMenuItem isActive={false} aria-current="page">
a
</SideNavMenuItem>
</SideNavMenuItem>,
<SideNavMenuItem isActive={true} aria-current="page">
b
</SideNavMenuItem>,
];
expect(hasActiveDescendant(child)).toBe(true);
});
Copy link
Member

Choose a reason for hiding this comment

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

Rather than test the internal implementation like this - how could we reframe the tests from the perspective of someone using the component?

hasActiveDescendant is only called in one place under certain circumstances

[`${prefix}--side-nav__item--active`]:
isActive || (hasActiveDescendant(children) && !isExpanded),

I think this would mean that to test this from a user-interaction viewpoint, each test could render the component with a different type of children which would get hasActiveDescendant to be called, and then the assertion would be toHaveClass('${prefix}--side-nav__item--active').

Comment on lines 120 to 123
it('should return true if the child is a invalid React element and has isActive props set to true', () => {
const child = <div isActive={true} />;
expect(hasActiveDescendant(child)).toBe(true);
});
Copy link
Member

Choose a reason for hiding this comment

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

Same kind of idea here. I don't think we need to directly assert that the function call returns true. Instead it's the end result of the function call we want to ensure happens correctly.

Again I think in nearly all cases this would be an assertion of toHaveClass or .not.toHaveClass. The classes are admittedly still an implementation detail, but they're the closest thing to what the user perceives without having to actually assert that certain styles are applied (background, etc. or whatever is in selectors with ${prefix}--side-nav__item--active

Comment on lines 142 to 152
it('should return false if child is an invalid React element', () => {
const child = ['abc', 'xyz'];
render(
<SideNavMenu title="test-title">
<SideNavMenuItem>a</SideNavMenuItem>
<SideNavMenuItem>b</SideNavMenuItem>
<SideNavMenuItem>c</SideNavMenuItem>
</SideNavMenu>
);
expect(hasActiveDescendant(child)).toBe(false);
});
Copy link
Member

Choose a reason for hiding this comment

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

This one doesn't use anything that's being rendered.

@@ -200,7 +200,9 @@ SideNavMenu.propTypes = {
Defining the children parameter with the type ReactNode | ReactNode[]. This allows for various possibilities:
a single element, an array of elements, or null or undefined.
**/
function hasActiveDescendant(children: ReactNode | ReactNode[]): boolean {
export function hasActiveDescendant(
Copy link
Member

Choose a reason for hiding this comment

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

If the tests are refactored to be user centered this won't need to be exported.

@mikdhadc
Copy link
Author

Thanks for your feedback. Will work on them.

Added testcases for SideNavMenu Component that increased the test coverage

BREAKING CHANGE: The SideNavMenu now has a test coverage greater than 90% - fixes based on feedback
Removed unused import fireEvent from the test file

BREAKING CHANGE: Removed unused imports
@mikdhadc
Copy link
Author

@tay1orjones , I have updated the PR based on your feedback. Let me know if any other modifications are needed. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase SideNavMenu unit test coverage
2 participants