Skip to content

Commit

Permalink
basic tests for table row helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Jul 31, 2023
1 parent 3a90e99 commit 9d7b9ca
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/table/table-role/__tests__/table-role-helper.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
getTableCellRoleProps,
getTableColHeaderRoleProps,
getTableHeaderRowRoleProps,
getTableRoleProps,
getTableRowRoleProps,
getTableWrapperRoleProps,
} from '../table-role-helper';

test('non-scrollable table props', () => {
const tableRole = 'table';
const ariaLabel = 'table';
const totalItemsCount = 5;

const tableWrapper = getTableWrapperRoleProps({ tableRole, ariaLabel, isScrollable: false });
const tableProps = getTableRoleProps({ tableRole, ariaLabel, totalItemsCount });

expect(tableWrapper).toEqual({});
expect(tableProps).toEqual({ role: tableRole, 'aria-label': ariaLabel, 'aria-rowcount': 6 });
});

test('scrollable table props', () => {
const tableRole = 'table';
const ariaLabel = 'table';
const totalItemsCount = 5;

const tableWrapper = getTableWrapperRoleProps({ tableRole, ariaLabel, isScrollable: true });
const tableProps = getTableRoleProps({ tableRole, ariaLabel, totalItemsCount });

expect(tableWrapper).toEqual({ role: 'region', 'aria-label': ariaLabel, tabIndex: 0 });
expect(tableProps).toEqual({ role: tableRole, 'aria-label': ariaLabel, 'aria-rowcount': 6 });
});

test('non-scrollable grid props', () => {
const tableRole = 'grid';
const ariaLabel = 'table';
const totalItemsCount = 5;

const tableWrapper = getTableWrapperRoleProps({ tableRole, ariaLabel, isScrollable: false });
const tableProps = getTableRoleProps({ tableRole, ariaLabel, totalItemsCount });

expect(tableWrapper).toEqual({});
expect(tableProps).toEqual({ role: tableRole, 'aria-label': ariaLabel, tabIndex: -1, 'aria-rowcount': 6 });
});

test('scrollable grid props', () => {
const tableRole = 'grid';
const ariaLabel = 'table';
const totalItemsCount = 5;

const tableWrapper = getTableWrapperRoleProps({ tableRole, ariaLabel, isScrollable: true });
const tableProps = getTableRoleProps({ tableRole, ariaLabel, totalItemsCount });

expect(tableWrapper).toEqual({ role: 'region', 'aria-label': ariaLabel, tabIndex: 0 });
expect(tableProps).toEqual({ role: tableRole, 'aria-label': ariaLabel, tabIndex: -1, 'aria-rowcount': 6 });
});

test('table row and cell props', () => {
const tableRole = 'table';

const headerRow = getTableHeaderRowRoleProps({ tableRole });
const headerCell1 = getTableColHeaderRoleProps({ tableRole, colIndex: 0, sortingStatus: 'ascending' });
const headerCell2 = getTableColHeaderRoleProps({ tableRole, colIndex: 1 });
const bodyRow = getTableRowRoleProps({ tableRole, rowIndex: 0 });
const bodyCell1 = getTableCellRoleProps({ tableRole, colIndex: 0, isRowHeader: true });
const bodyCell2 = getTableCellRoleProps({ tableRole, colIndex: 1 });

expect(headerRow).toEqual({});
expect(bodyRow).toEqual({});
expect(headerCell1).toEqual({ scope: 'col', 'aria-sort': 'ascending' });
expect(headerCell2).toEqual({ scope: 'col' });
expect(bodyCell1).toEqual({ scope: 'row' });
expect(bodyCell2).toEqual({});
});

test('grid row and cell props', () => {
const tableRole = 'grid';

const headerRow = getTableHeaderRowRoleProps({ tableRole });
const headerCell1 = getTableColHeaderRoleProps({ tableRole, colIndex: 0, sortingStatus: 'ascending' });
const headerCell2 = getTableColHeaderRoleProps({ tableRole, colIndex: 1 });
const bodyRow = getTableRowRoleProps({ tableRole, rowIndex: 0 });
const bodyCell1 = getTableCellRoleProps({ tableRole, colIndex: 0, isRowHeader: true });
const bodyCell2 = getTableCellRoleProps({ tableRole, colIndex: 1 });

expect(headerRow).toEqual({ 'aria-rowindex': 1 });
expect(bodyRow).toEqual({ 'aria-rowindex': 2 });
expect(headerCell1).toEqual({ 'aria-colindex': 1, scope: 'col', 'aria-sort': 'ascending' });
expect(headerCell2).toEqual({ 'aria-colindex': 2, scope: 'col' });
expect(bodyCell1).toEqual({ 'aria-colindex': 1, scope: 'row' });
expect(bodyCell2).toEqual({ 'aria-colindex': 2 });
});

0 comments on commit 9d7b9ca

Please sign in to comment.