Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/react-core/src/components/Nav/NavExpandable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class NavExpandable extends Component<NavExpandableProps, NavExpandableState> {
</button>
)}
</PageSidebarContext.Consumer>
<section className={css(styles.navSubnav)} aria-labelledby={this.id} hidden={expandedState ? null : true}>
<section
className={css(styles.navSubnav)}
aria-labelledby={this.id}
hidden={expandedState ? null : true}
{...(!expandedState && { inert: '' })}
>
{srText && (
<h2 className="pf-v6-screen-reader" id={this.id}>
{srText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ exports[`NavExpandable should match snapshot (auto-generated) 1`] = `
aria-labelledby="''"
class="pf-v6-c-nav__subnav"
hidden=""
inert=""
>
<h2
class="pf-v6-screen-reader"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this isn't being used let's remove this import

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was testing some things out with the remaining two tests, missed removing it before pushing changes.

import '@testing-library/jest-dom';
import { NavExpandable } from '../NavExpandable';
import { NavItem } from '../NavItem';
import { Nav, NavContext } from '../Nav';
import { NavList } from '../NavList';
Copy link
Contributor

Choose a reason for hiding this comment

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

So for the purposes of this set of unit tests, we don't need any of these other Nav components. We really only want to test that NavExpandable behaves/works the way we expect. Sometimes that does involve importing other components/creating mocks, but for the tests relevant to this PR we can get away wthout them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh great, reduced the code by a lot. I am new to writing tests so learning a lot.


const props = {
items: [
{ to: '#link1', label: 'Link 1' },
{ to: '#link2', label: 'Link 2' },
{ to: '#link3', label: 'Link 3' }
]
};
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this object


describe('NavExpandable', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Currently I think we rarely use the describe block in our revamped tests; I personally try to use it more to separate out lengthy amounts of tests that can be grouped together. For now I think we can remove the describe block and just have each test on its own.

beforeEach(() => {
jest.resetAllMocks();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need this here


test('check that inert is on the section element by default', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

For the 2 tests here, with the above comments taken into account, we can slim down the code written:

  • Let's update the test names similar to how other revamped tests we have, along the lines of "Renders with the inert attribute by default" and "Does not render with the intert attribute when isExpanded is true".
  • For the render method, let's just pass a NavExpandable with a title prop passed in
  • For the expect, we can use the getByLabelText matcher using the string you pass to the title prop and then assert that the inert attribute is/isn't there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, will make the necessary changes.

render(
<Nav onSelect={jest.fn()} onToggle={jest.fn()}>
<NavList>
<NavExpandable id="grp-1" title="Expandable group" data-testid="test-id">
{props.items.map((item) => (
<NavItem to={item.to} key={item.to}>
{item.label}
</NavItem>
))}
</NavExpandable>
</NavList>
</Nav>
);
const wrapper = screen.getByTestId('test-id');
const section = wrapper.querySelector('.pf-v6-c-nav__subnav');
expect(section).toHaveAttribute('inert', '');
});

test('check that inert is NOT on the section when isExpanded is true', () => {
render(
<Nav onSelect={jest.fn()} onToggle={jest.fn()}>
<NavList>
<NavExpandable id="grp-1" title="Expandable group" isExpanded={true} data-testid="test-id">
{props.items.map((item) => (
<NavItem to={item.to} key={item.to}>
{item.label}
</NavItem>
))}
</NavExpandable>
</NavList>
</Nav>
);

const wrapper = screen.getByTestId('test-id');
const section = wrapper.querySelector('.pf-v6-c-nav__subnav');
expect(section).not.toHaveAttribute('inert', '');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ exports[`Nav Expandable Nav List - Trigger toggle 1`] = `
aria-labelledby="grp-1"
class="pf-v6-c-nav__subnav"
hidden=""
inert=""
>
<ul
class="pf-v6-c-nav__list"
Expand Down Expand Up @@ -355,6 +356,7 @@ exports[`Nav Expandable Nav List 1`] = `
aria-labelledby="grp-1"
class="pf-v6-c-nav__subnav"
hidden=""
inert=""
>
<ul
class="pf-v6-c-nav__list"
Expand Down Expand Up @@ -486,6 +488,7 @@ exports[`Nav Expandable Nav List with aria label 1`] = `
aria-labelledby="grp-1"
class="pf-v6-c-nav__subnav"
hidden=""
inert=""
>
<h2
class="pf-v6-screen-reader"
Expand Down
Loading