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

fix(HeaderPanel): Initialize onHeaderPanelFocus with default value #14351

Merged
merged 7 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/react/src/components/UIShell/HeaderPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import { keys, match } from '../../internal/keyboard';
import { useWindowEvent } from '../../internal/useEvent';
import { useMergedRefs } from '../../internal/useMergedRefs';

const noopFn = () => {};
const HeaderPanel = React.forwardRef(function HeaderPanel(
{
children,
className: customClassName,
expanded,
addFocusListeners = true,
onHeaderPanelFocus,
onHeaderPanelFocus = noopFn,
href,
...other
},
Expand Down Expand Up @@ -123,7 +124,6 @@ HeaderPanel.propTypes = {
/**
* An optional listener that is called a callback to collapse the HeaderPanel
*/

onHeaderPanelFocus: PropTypes.func,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React from 'react';
import HeaderPanel from '../HeaderPanel';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

describe('HeaderPanel', () => {
describe('renders as expected - Component API', () => {
Expand Down Expand Up @@ -63,5 +64,32 @@ describe('HeaderPanel', () => {
const childrenArray = screen.getAllByText('Test');
expect(childrenArray.length).toEqual(2);
});

it('should call `onHeaderPanelFocus` callback, when defined', async () => {
const onHeaderPanelFocus = jest.fn();
render(
<HeaderPanel onHeaderPanelFocus={onHeaderPanelFocus}>
<button type="button">Test</button>
</HeaderPanel>
);

screen.getByRole('button', { name: 'Test' }).focus();
await userEvent.keyboard('{Escape}');

expect(onHeaderPanelFocus).toHaveBeenCalled();
});

it('should not error when `onHeaderPanelFocus` is not defined', async () => {
render(
<HeaderPanel>
<button type="button">Test</button>
</HeaderPanel>
);

await expect(async () => {
screen.getByRole('button', { name: 'Test' }).focus();
await userEvent.keyboard('{Escape}');
}).not.toThrow();
});
});
});