Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 committed Jan 31, 2024
1 parent 68ed014 commit a46c253
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 75 deletions.
100 changes: 26 additions & 74 deletions packages/components/src/tabs/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { useEffect, useState } from '@wordpress/element';
*/
import Tabs from '..';
import type { TabsProps } from '../types';
import { act } from 'react-dom/test-utils';

type Tab = {
tabId: string;
Expand Down Expand Up @@ -1174,79 +1173,32 @@ describe( 'Tabs', () => {
} );
} );
describe( 'When `selectedId` is changed by the controlling component', () => {
describe.each( [ true, false ] )(
'When `selectOnMove` is `%s`',
( selectOnMove ) => {
it( 'should move focus to the newly selected tab if the previously selected tab was focused', async () => {
const { rerender } = render(
<ControlledTabs
tabs={ TABS }
selectedTabId="beta"
selectOnMove={ selectOnMove }
/>
);

// Tab key should focus the currently selected tab, which is Beta.
await press.Tab();
expect( await getSelectedTab() ).toHaveTextContent(
'Beta'
);
expect( await getSelectedTab() ).toHaveFocus();

rerender(
<ControlledTabs
tabs={ TABS }
selectedTabId="gamma"
selectOnMove={ selectOnMove }
/>
);

// When the selected tab is changed, it should automatically receive focus.
expect( await getSelectedTab() ).toHaveTextContent(
'Gamma'
);
expect( await getSelectedTab() ).toHaveFocus();
} );
it( 'should not move focus to the newly selected tab if the previously selected tab was not focused', async () => {
const { rerender } = render(
<ControlledTabs
tabs={ TABS }
selectedTabId="beta"
selectOnMove={ selectOnMove }
/>
);

expect( await getSelectedTab() ).toHaveTextContent(
'Beta'
);

// Tab key should focus the currently selected tab, which is Beta.
await press.Tab();
expect( await getSelectedTab() ).toHaveFocus();
// Focus Alpha, which is not the currently selected tab
// (not the most elegant way, but it does the job).
act( () =>
screen.getByRole( 'tab', { name: 'Alpha' } ).focus()
);

rerender(
<ControlledTabs
tabs={ TABS }
selectedTabId="gamma"
selectOnMove={ selectOnMove }
/>
);

// When the selected tab is changed, it should not automatically receive focus.
expect( await getSelectedTab() ).toHaveTextContent(
'Gamma'
);
expect(
screen.getByRole( 'tab', { name: 'Alpha' } )
).toHaveFocus();
} );
}
);
it( 'should continue to handle arrow key navigation properly', async () => {
const { rerender } = render(
<ControlledTabs tabs={ TABS } selectedTabId="beta" />
);

// Tab key should focus the currently selected tab, which is Beta.
await press.Tab();
expect( await getSelectedTab() ).toHaveTextContent( 'Beta' );
expect( await getSelectedTab() ).toHaveFocus();

rerender(
<ControlledTabs tabs={ TABS } selectedTabId="gamma" />
);

// When the selected tab is changed, it should not automatically receive focus.
expect( await getSelectedTab() ).toHaveTextContent( 'Gamma' );
expect(
screen.getByRole( 'tab', { name: 'Beta' } )
).toHaveFocus();

// Arrow keys should move focus to the next tab, which is Gamma
await press.ArrowRight();
expect(
screen.getByRole( 'tab', { name: 'Gamma' } )
).toHaveFocus();
} );
} );

describe( 'When `selectOnMove` is `true`', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test.describe( 'Order of block keyboard navigation', () => {
);

await page.keyboard.press( 'Tab' );
await KeyboardNavigableBlocks.expectLabelToHaveFocus( 'Post' );
await KeyboardNavigableBlocks.expectLabelToHaveFocus( 'Block' );
} );

test( 'allows tabbing in navigation mode if no block is selected (reverse)', async ( {
Expand Down

0 comments on commit a46c253

Please sign in to comment.