import { enableMapSet } from 'immer' import type { GridSelectionState } from './selection' import reducerConfig, { selectSelectionMode, selectSelectionAnchor, } from './selection' const { reducer: selectionReducer } = reducerConfig enableMapSet() const getStateMock = ( state: Partial = {} ): GridSelectionState => ({ ...reducerConfig.initialState, ...state, }) describe('selectionReducer', () => { describe('unknown action', () => { it('should not modify the state', () => { const state = getStateMock() const newState = selectionReducer(state, { type: 'nope' } as any) expect(state).toBe(newState) }) }) describe('applyProps', () => { it('should be marked as controlled if selection is provided', () => { const state = selectionReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], selection: new Set(['123']), }, }) expect(state.controlled).toBe(true) }) it('should be marked as not controlled if selection is omitted', () => { const state = selectionReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], }, }) expect(state.controlled).toBe(false) }) it('should store selection if provided', () => { const state = selectionReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], selection: new Set(['123']), }, }) expect(state.selection).toEqual(new Set(['123'])) }) it('should store selectionMode if provided', () => { const state = selectionReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], selectionMode: 'multi', }, }) expect(state.mode).toBe('multi') }) it('should not change selection if controlled and mode changes', () => { const state = selectionReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], selection: new Set(['1', '2', '3']), selectionMode: 'single', }, }) expect(state.mode).toBe('single') expect(state.selection).toEqual(new Set(['1', '2', '3'])) }) it('should empty selection if uncontrolled and mode changes to none', () => { const state = selectionReducer( getStateMock({ selection: new Set(['1', '2', '3']), }), { type: 'applyProps', payload: { columns: [], rows: [], selectionMode: 'none', }, } ) expect(state.mode).toBe('none') expect(state.selection).toEqual(new Set()) }) it('should limit selection to one if uncontrolled and mode changes to single', () => { const state = selectionReducer( getStateMock({ selection: new Set(['1', '2', '3']), }), { type: 'applyProps', payload: { columns: [], rows: [], selectionMode: 'single', }, } ) expect(state.mode).toBe('single') expect(state.selection).toEqual(new Set(['3'])) }) }) describe('updateSelection', () => { it('should update the selection', () => { const state = selectionReducer(getStateMock(), { type: 'updateSelection', payload: new Set(['123', '234']), }) expect(state.selection).toEqual(new Set(['123', '234'])) }) }) describe('selectSelectionMode', () => { it('should return the mode', () => { expect(selectSelectionMode(getStateMock())).toBe('single') }) }) describe('setSelectionAnchor', () => { let initialState: GridSelectionState beforeEach(() => { initialState = getStateMock({ anchor: '3', }) }) it('should set anchor', () => { const state = selectionReducer(initialState, { type: 'setSelectionAnchor', payload: '2', }) expect(state.anchor).toBe('2') }) it('should clear anchor', () => { const state = selectionReducer(initialState, { type: 'setSelectionAnchor', payload: null, }) expect(state.anchor).toBe(null) }) it('selectSelectionAnchor should return anchor', () => { const state = selectionReducer(initialState, { type: 'setSelectionAnchor', payload: null, }) expect(selectSelectionAnchor(state)).toBe(null) const nextState = selectionReducer(state, { type: 'setSelectionAnchor', payload: '2', }) expect(selectSelectionAnchor(nextState)).toBe('2') }) }) })