import { ROW_FOCUS_ID } from '../../constants' import reducerConfig, { selectSubFocus, selectCanLoopHorizontally, selectRawCurrentFocus, } from './focus' const { reducer: focusReducer } = reducerConfig const getStateMock = () => reducerConfig.initialState describe('focusReducer', () => { describe('unknown action', () => { it('should not modify the state', () => { const state = getStateMock() const newState = focusReducer(state, { type: 'nope' } as any) expect(state).toBe(newState) }) }) describe('updateFocus', () => { it('should replace the focus config', () => { const state = focusReducer(getStateMock(), { type: 'updateFocus', payload: { area: 'body', columnId: '5', rowId: '2', subFocus: 'first', }, }) expect(state).toEqual({ initialized: false, loopHorizontally: false, focus: { area: 'body', columnId: '5', rowId: '2', subFocus: 'first', }, }) }) it('should not change the focus config if the values are the same', () => { const state = getStateMock() const newState = focusReducer(state, { type: 'updateFocus', payload: { area: 'header', columnId: '', rowId: '0', subFocus: 'first', }, }) expect(state).toBe(newState) }) }) describe('applyProps', () => { it('should initialize focus with default values when not hiding headers', () => { const state = focusReducer(getStateMock(), { type: 'applyProps', payload: { hideHeaders: false, initialFocusMode: 'cell', loopHorizontally: false, }, }) expect(state.initialized).toBe(true) expect(selectCanLoopHorizontally(state)).toBe(false) const focus = selectRawCurrentFocus(state) expect(focus.area).toBe('header') expect(focus.columnId).toBe('') expect(focus.rowId).toBe('0') expect(selectSubFocus(state)).toBe('first') }) it('should initialize focus with body area when hiding headers', () => { const state = focusReducer(getStateMock(), { type: 'applyProps', payload: { hideHeaders: true, initialFocusMode: 'cell', loopHorizontally: true, }, }) expect(state.initialized).toBe(true) expect(selectCanLoopHorizontally(state)).toBe(true) const focus = selectRawCurrentFocus(state) expect(focus.area).toBe('body') expect(focus.columnId).toBe('') expect(focus.rowId).toBe('0') expect(selectSubFocus(state)).toBe('first') }) it('should initialize focus with row focus mode', () => { const state = focusReducer(getStateMock(), { type: 'applyProps', payload: { hideHeaders: false, initialFocusMode: 'row', }, }) const focus = selectRawCurrentFocus(state) expect(focus.columnId).toBe(ROW_FOCUS_ID) expect(selectSubFocus(state)).toBe('last') }) it('should not reinitialize if already initialized', () => { const initializedState = { ...getStateMock(), initialized: true, focus: { area: 'body' as const, columnId: 'test-column', rowId: '5', subFocus: 'last' as const, }, } const state = focusReducer(initializedState, { type: 'applyProps', payload: { hideHeaders: true, initialFocusMode: 'cell', loopHorizontally: true, }, }) const focus = selectRawCurrentFocus(state) expect(focus.area).toBe('body') expect(focus.columnId).toBe('test-column') expect(selectCanLoopHorizontally(state)).toBe(true) }) }) describe('selectSubFocus', () => { it('should return sub focus', () => { expect(selectSubFocus(getStateMock())).toBe('first') }) }) describe('selectCanLoopHorizontally', () => { it('should return false by default', () => { expect(selectCanLoopHorizontally(getStateMock())).toBe(false) }) it('should return true when loopHorizontally is enabled', () => { const state = { ...getStateMock(), loopHorizontally: true, } expect(selectCanLoopHorizontally(state)).toBe(true) }) }) })