Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Mar 28, 2023
1 parent 89f674e commit 9b394cd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions packages/components/src/navigator/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
NavigatorToParentButton,
useNavigator,
} from '..';
import type { NavigateOptions } from '../types';

const INVALID_HTML_ATTRIBUTE = {
raw: ' "\'><=invalid_path',
Expand Down Expand Up @@ -57,13 +58,15 @@ const BUTTON_TEXT = {
'Navigate to screen with an invalid HTML value as a path.',
back: 'Go back',
backUsingGoTo: 'Go back using goTo',
goToWithSkipFocus: 'Go to with skipFocus',
};

type CustomTestOnClickHandler = (
args:
| {
type: 'goTo';
path: string;
options?: NavigateOptions;
}
| { type: 'goBack' }
| { type: 'goToParent' }
Expand Down Expand Up @@ -108,6 +111,26 @@ function CustomNavigatorGoToBackButton( {
);
}

function CustomNavigatorGoToSkipFocusButton( {
path,
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
const { goTo } = useNavigator();
return (
<Button
onClick={ () => {
goTo( path, { skipFocus: true } );
// Used to spy on the values passed to `navigator.goTo`.
onClick?.( { type: 'goTo', path } );
} }
{ ...props }
/>
);
}

function CustomNavigatorBackButton( {
onClick,
...props
Expand Down Expand Up @@ -342,6 +365,12 @@ const MyHierarchicalNavigation = ( {
{ BUTTON_TEXT.backUsingGoTo }
</CustomNavigatorGoToBackButton>
</NavigatorScreen>
<CustomNavigatorGoToSkipFocusButton
path={ PATHS.NESTED }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.goToWithSkipFocus }
</CustomNavigatorGoToSkipFocusButton>
</NavigatorProvider>
</>
);
Expand Down Expand Up @@ -716,6 +745,29 @@ describe( 'Navigator', () => {
await user.click( getNavigationButton( 'back' ) );
expect( getNavigationButton( 'toChildScreen' ) ).toHaveFocus();
} );

it( 'should skip focus based on location `skipFocus` option', async () => {
const user = userEvent.setup();
render( <MyHierarchicalNavigation /> );

// Navigate to child screen with skipFocus.
await user.click( getNavigationButton( 'goToWithSkipFocus' ) );
expect( queryScreen( 'home' ) ).not.toBeInTheDocument();
expect( getScreen( 'nested' ) ).toBeInTheDocument();

// The clicked button should remain focused.
expect( getNavigationButton( 'goToWithSkipFocus' ) ).toHaveFocus();

// Navigate back to parent screen.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
// The first tabbable element receives focus.
expect(
screen.getByRole( 'button', {
name: 'First tabbable child screen button',
} )
).toHaveFocus();
} );
} );

describe( 'animation', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/navigator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ButtonAsButtonProps } from '../button/types';

export type MatchParams = Record< string, string | string[] >;

type NavigateOptions = {
export type NavigateOptions = {
focusTargetSelector?: string;
isBack?: boolean;
skipFocus?: boolean;
Expand Down

0 comments on commit 9b394cd

Please sign in to comment.