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

Add some tests for TreeGrid. Update README to reflect latest functionality. #39302

Merged
merged 7 commits into from
Mar 9, 2022
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
2 changes: 1 addition & 1 deletion packages/components/src/tree-grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Aside from the documented callback functions, any props specified will be passed

###### onFocusRow( event: Event, startRow: HTMLElement, destinationRow: HTMLElement )

Callback that fires when focus is shifted from one row to another via the UP and DOWN keys.
Callback that fires when focus is shifted from one row to another via the Up and Down keys. Callback is also fired on Home and End keys which move focus from the beginning row to the end row.
The callback is passed the event, the start row element that the focus was on originally, and
the destination row element after the focus has moved.

Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/tree-grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ function TreeGrid(
);
focusablesInNextRow[ nextIndex ].focus();

// Let consumers know the row that was originally focused,
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 guess this was missed in the original PR review. My bad.

// and the row that is now in focus.
onFocusRow( event, activeRow, rows[ nextRowIndex ] );

// Prevent key use for anything else. This ensures Voiceover
// doesn't try to handle key navigation.
event.preventDefault();
Expand Down
78 changes: 61 additions & 17 deletions packages/components/src/tree-grid/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
/**
* WordPress dependencies
*/
import { LEFT, RIGHT, UP, DOWN } from '@wordpress/keycodes';
import { LEFT, RIGHT, UP, DOWN, HOME, END } from '@wordpress/keycodes';
import { forwardRef } from '@wordpress/element';

/**
Expand Down Expand Up @@ -68,10 +68,10 @@ describe( 'TreeGrid', () => {
level={ 1 }
positionInSet={ 1 }
setSize={ 3 }
isExpanded={ true }
isExpanded={ false }
>
<TreeGridCell withoutGridItem>
<TestButton>Row 1</TestButton>
<TestButton aria-expanded="false">Row 1</TestButton>
</TreeGridCell>
</TreeGridRow>
<TreeGridRow
Expand All @@ -88,7 +88,7 @@ describe( 'TreeGrid', () => {
level={ 1 }
positionInSet={ 3 }
setSize={ 3 }
isExpanded={ true }
isExpanded={ false }
>
<TreeGridCell withoutGridItem>
<TestButton>Row 3</TestButton>
Expand All @@ -97,16 +97,16 @@ describe( 'TreeGrid', () => {
</TreeGrid>
);

screen.getByText( 'Row 2' ).focus();
const row2Element = screen.getByText( 'Row 2' ).closest( 'tr' );
screen.getByText( 'Row 1' ).focus();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was necessary as row 1 will do the collapsing/expanding now.

const row1Element = screen.getByText( 'Row 1' ).closest( 'tr' );

fireEvent.keyDown( screen.getByText( 'Row 2' ), {
fireEvent.keyDown( screen.getByText( 'Row 1' ), {
key: 'ArrowRight',
keyCode: RIGHT,
currentTarget: row2Element,
currentTarget: row1Element,
} );

expect( onExpandRow ).toHaveBeenCalledWith( row2Element );
expect( onExpandRow ).toHaveBeenCalledWith( row1Element );
} );
} );

Expand All @@ -120,17 +120,17 @@ describe( 'TreeGrid', () => {
level={ 1 }
positionInSet={ 1 }
setSize={ 3 }
isExpanded={ false }
isExpanded={ true }
>
<TreeGridCell withoutGridItem>
<TestButton>Row 1</TestButton>
<TestButton aria-expanded="true">Row 1</TestButton>
</TreeGridCell>
</TreeGridRow>
<TreeGridRow
level={ 1 }
positionInSet={ 2 }
setSize={ 3 }
isExpanded={ true }
isExpanded={ false }
>
<TreeGridCell withoutGridItem>
<TestButton>Row 2</TestButton>
Expand All @@ -149,16 +149,16 @@ describe( 'TreeGrid', () => {
</TreeGrid>
);

screen.getByText( 'Row 2' ).focus();
const row2Element = screen.getByText( 'Row 2' ).closest( 'tr' );
screen.getByText( 'Row 1' ).focus();
const row1Element = screen.getByText( 'Row 1' ).closest( 'tr' );

fireEvent.keyDown( screen.getByText( 'Row 2' ), {
fireEvent.keyDown( screen.getByText( 'Row 1' ), {
key: 'ArrowLeft',
keyCode: LEFT,
currentTarget: row2Element,
currentTarget: row1Element,
} );

expect( onCollapseRow ).toHaveBeenCalledWith( row2Element );
expect( onCollapseRow ).toHaveBeenCalledWith( row1Element );
} );
} );

Expand Down Expand Up @@ -205,6 +205,28 @@ describe( 'TreeGrid', () => {
);
} );

it( 'should call onFocusRow with event, start and end nodes when pressing End', () => {
const onFocusRow = jest.fn();
render( <TestTree onFocusRow={ onFocusRow } /> );

screen.getByText( 'Row 1' ).focus();

const row1Element = screen.getByText( 'Row 1' ).closest( 'tr' );
const row3Element = screen.getByText( 'Row 3' ).closest( 'tr' );

fireEvent.keyDown( screen.getByText( 'Row 1' ), {
key: 'End',
keyCode: END,
currentTarget: row1Element,
} );

expect( onFocusRow ).toHaveBeenCalledWith(
expect.objectContaining( { key: 'End', keyCode: END } ),
row1Element,
row3Element
);
} );

it( 'should call onFocusRow with event, start and end nodes when pressing Up Arrow', () => {
const onFocusRow = jest.fn();
render( <TestTree onFocusRow={ onFocusRow } /> );
Expand All @@ -227,6 +249,28 @@ describe( 'TreeGrid', () => {
);
} );

it( 'should call onFocusRow with event, start and end nodes when pressing Home', () => {
const onFocusRow = jest.fn();
render( <TestTree onFocusRow={ onFocusRow } /> );

screen.getByText( 'Row 3' ).focus();

const row3Element = screen.getByText( 'Row 3' ).closest( 'tr' );
const row1Element = screen.getByText( 'Row 1' ).closest( 'tr' );

fireEvent.keyDown( screen.getByText( 'Row 3' ), {
key: 'Home',
keyCode: HOME,
currentTarget: row3Element,
} );

expect( onFocusRow ).toHaveBeenCalledWith(
expect.objectContaining( { key: 'Home', keyCode: HOME } ),
row3Element,
row1Element
);
} );

it( 'should call onFocusRow when shift key is held', () => {
const onFocusRow = jest.fn();
render( <TestTree onFocusRow={ onFocusRow } /> );
Expand Down