import { ROW_FOCUS_ID } from '../constants' import type { GridFocusState } from '../state/reducer/focus' import { findNextHeaderCell, getColumnIndex } from './focus-utils' const COLUMN_IDS = [ '__selection', 'name', 'assignee', 'status', 'startDate', 'endDate', 'budget', 'tags', 'locked', ] const HEADER_HIERARCHY = { groups: [ { id: 'task details', level: 1, width: 2, childIds: ['status', 'assignee'], columnIds: ['status', 'assignee'], parentIds: [], }, { id: 'forecast', level: 2, width: 5, childIds: ['planning', 'attributes'], columnIds: ['budget', 'startDate', 'endDate', 'locked', 'tags'], parentIds: [], }, { id: 'planning', level: 1, width: 3, childIds: ['budget', 'startDate', 'endDate'], columnIds: ['budget', 'startDate', 'endDate'], parentIds: ['forecast'], }, { id: 'attributes', level: 1, width: 2, childIds: ['locked', 'tags'], columnIds: ['locked', 'tags'], parentIds: ['forecast'], }, ], levels: 2, } const HEADER_ROW_IDS = ['0', '1', '2'] describe('focus-utils', () => { describe('findNextHeaderCell', () => { it('should navigate to next top group', () => { const currentFocus: GridFocusState['focus'] = { area: 'header', columnId: 'assignee', rowId: '0', subFocus: 'first', } expect( findNextHeaderCell( 'right', currentFocus, COLUMN_IDS, HEADER_HIERARCHY, HEADER_ROW_IDS ) ).toEqual({ rowIndex: 0, columnIndex: 4 }) }) it('should navigate to previous top group', () => { const currentFocus: GridFocusState['focus'] = { area: 'header', columnId: 'locked', rowId: '0', subFocus: 'first', } expect( findNextHeaderCell( 'left', currentFocus, COLUMN_IDS, HEADER_HIERARCHY, HEADER_ROW_IDS ) ).toEqual({ rowIndex: 0, columnIndex: 3 }) }) it('should navigate to child group', () => { const currentFocus: GridFocusState['focus'] = { area: 'header', columnId: 'locked', rowId: '0', subFocus: 'first', } expect( findNextHeaderCell( 'down', currentFocus, COLUMN_IDS, HEADER_HIERARCHY, HEADER_ROW_IDS ) ).toEqual({ rowIndex: 1, columnIndex: 8 }) }) it('should navigate to parent group', () => { const currentFocus: GridFocusState['focus'] = { area: 'header', columnId: 'endDate', rowId: '1', subFocus: 'first', } expect( findNextHeaderCell( 'up', currentFocus, COLUMN_IDS, HEADER_HIERARCHY, HEADER_ROW_IDS ) ).toEqual({ rowIndex: 0, columnIndex: 5 }) }) it('should jump to column if only one parent', () => { const currentFocus: GridFocusState['focus'] = { area: 'header', columnId: 'status', rowId: '0', subFocus: 'first', } expect( findNextHeaderCell( 'down', currentFocus, COLUMN_IDS, HEADER_HIERARCHY, HEADER_ROW_IDS ) ).toEqual({ rowIndex: 2, columnIndex: 3 }) }) it('should jump to body if no parent', () => { const currentFocus: GridFocusState['focus'] = { area: 'header', columnId: 'name', rowId: '0', subFocus: 'first', } expect( findNextHeaderCell( 'down', currentFocus, COLUMN_IDS, HEADER_HIERARCHY, HEADER_ROW_IDS ) ).toEqual({ rowIndex: -1, columnIndex: 1 }) }) }) describe('getColumnIndex', () => { it('should return column index', () => { expect(getColumnIndex(COLUMN_IDS, 'endDate', 'first')).toBe(5) }) it('should return column index when row selected start', () => { expect(getColumnIndex(COLUMN_IDS, ROW_FOCUS_ID, 'last')).toBe(-1) }) it('should return column index when row selected end', () => { expect(getColumnIndex(COLUMN_IDS, ROW_FOCUS_ID, 'first')).toBe( COLUMN_IDS.length ) }) }) })