Skip to content

Commit

Permalink
Add deprecated APIs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Aug 16, 2024
1 parent da79e35 commit f1f6888
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions packages/components/src/navigator/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import {
NavigatorScreen,
NavigatorButton,
NavigatorBackButton,
NavigatorToParentButton,
useNavigator,
} from '..';
import type { NavigateOptions } from '../types';
import React from 'react';

const INVALID_HTML_ATTRIBUTE = {
raw: '/ "\'><=invalid_path',
Expand Down Expand Up @@ -147,6 +149,44 @@ function CustomNavigatorBackButton( {
);
}

function CustomNavigatorToParentButton( {
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorBackButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
return (
<NavigatorToParentButton
onClick={ () => {
// Used to spy on the values passed to `navigator.goBack`.
onClick?.( { type: 'goToParent' } );
} }
{ ...props }
/>
);
}

function CustomNavigatorToParentButtonAlternative( {
onClick,
children,
}: {
children: React.ReactNode;
onClick?: CustomTestOnClickHandler;
} ) {
const { goToParent } = useNavigator();
return (
<button
onClick={ () => {
goToParent();
// Used to spy on the values passed to `navigator.goBack`.
onClick?.( { type: 'goToParent' } );
} }
>
{ children }
</button>
);
}

const ProductScreen = ( {
onBackButtonClick,
}: {
Expand Down Expand Up @@ -358,6 +398,66 @@ const MyHierarchicalNavigation = ( {
);
};

const MyDeprecatedNavigation = ( {
initialPath = PATHS.HOME,
onNavigatorButtonClick,
}: {
initialPath?: string;
onNavigatorButtonClick?: CustomTestOnClickHandler;
} ) => {
return (
<>
<NavigatorProvider initialPath={ initialPath }>
<NavigatorScreen path={ PATHS.HOME }>
<p>{ SCREEN_TEXT.home }</p>
{ /*
* A button useful to test focus restoration. This button is the first
* tabbable item in the screen, but should not receive focus when
* navigating to screen as a result of a backwards navigation.
*/ }
<button>First tabbable home screen button</button>
<CustomNavigatorButton
path={ PATHS.CHILD }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toChildScreen }
</CustomNavigatorButton>
</NavigatorScreen>

<NavigatorScreen path={ PATHS.CHILD }>
<p>{ SCREEN_TEXT.child }</p>
{ /*
* A button useful to test focus restoration. This button is the first
* tabbable item in the screen, but should not receive focus when
* navigating to screen as a result of a backwards navigation.
*/ }
<button>First tabbable child screen button</button>
<CustomNavigatorButton
path={ PATHS.NESTED }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toNestedScreen }
</CustomNavigatorButton>
<CustomNavigatorToParentButton
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorToParentButton>
</NavigatorScreen>

<NavigatorScreen path={ PATHS.NESTED }>
<p>{ SCREEN_TEXT.nested }</p>
<CustomNavigatorToParentButtonAlternative
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorToParentButtonAlternative>
</NavigatorScreen>
</NavigatorProvider>
</>
);
};

const getScreen = ( screenKey: keyof typeof SCREEN_TEXT ) =>
screen.getByText( SCREEN_TEXT[ screenKey ] );
const queryScreen = ( screenKey: keyof typeof SCREEN_TEXT ) =>
Expand Down Expand Up @@ -751,4 +851,53 @@ describe( 'Navigator', () => {
).toHaveFocus();
} );
} );

describe( 'deprecated APIs', () => {
it( 'should log a deprecation notice when using the NavigatorToParentButton component', async () => {
const user = userEvent.setup();

render( <MyDeprecatedNavigation initialPath={ PATHS.CHILD } /> );

expect( getScreen( 'child' ) ).toBeInTheDocument();

// Navigate back to home screen.
// The first tabbable element receives focus, since focus restoration
// it not possible (there was no forward navigation).
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect(
screen.getByRole( 'button', {
name: 'First tabbable home screen button',
} )
).toHaveFocus();

// Rendering `NavigatorToParentButton` logs a deprecation notice
expect( console ).toHaveWarnedWith(
'wp.components.NavigatorToParentButton is deprecated since version 6.7. Please use wp.components.NavigatorBackButton instead.'
);
} );

it( 'should log a deprecation notice when using the useNavigator().goToParent() function', async () => {
const user = userEvent.setup();

render( <MyDeprecatedNavigation initialPath={ PATHS.NESTED } /> );

expect( getScreen( 'nested' ) ).toBeInTheDocument();

// Navigate back to child screen using the back button.
// The first tabbable element receives focus, since focus restoration
// it not possible (there was no forward navigation).
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect(
screen.getByRole( 'button', {
name: 'First tabbable child screen button',
} )
).toHaveFocus();

expect( console ).toHaveWarnedWith(
'wp.components.useNavigator().goToParent is deprecated since version 6.7. Please use wp.components.useNavigator().goBack instead.'
);
} );
} );
} );

0 comments on commit f1f6888

Please sign in to comment.