import type { GridSortState } from './sort' import reducerConfig, { selectDefaultSort, selectSort, selectColumnSort, selectColumnSortNumber, selectIsMultiColumnSortingEnabled, selectIsMultiColumnSorting, } from './sort' const { reducer: sortReducer } = reducerConfig const getStateMock = (state: Partial = {}): GridSortState => ({ ...reducerConfig.initialState, ...state, }) describe('sortReducer', () => { describe('unknown action', () => { it('should not modify the state', () => { const state = getStateMock() const newState = sortReducer(state, { type: 'nope' } as any) expect(state).toBe(newState) }) }) describe('applyProps', () => { describe('with no sort config', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], }, }) }) it('should mark as uncontrolled', () => { expect(newState.controlled).toBe(false) }) it('should mark sort mode as internal', () => { expect(newState.mode).toBe('internal') }) }) describe('with controlled sort config', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], sort: [{ columnId: '123', direction: 'asc' }], sortMode: 'external', }, }) }) it('should store sort config', () => { expect(selectColumnSort(newState, '123')).toEqual({ columnId: '123', direction: 'asc', }) }) it('should store sort mode', () => { expect(newState.mode).toBe('external') }) }) describe('with multi column sort setting', () => { describe('when switching off with more then one column sorting (not controlled)', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer( getStateMock({ columns: [ { columnId: '123', direction: 'desc' }, { columnId: '234', direction: 'asc' }, ], }), { type: 'applyProps', payload: { columns: [], rows: [], multiColumnSort: false, }, } ) }) it('should keep only last item in sort', () => { expect(selectSort(newState)).toEqual([ { columnId: '234', direction: 'asc', }, ]) }) it('should store setting', () => { expect(selectIsMultiColumnSortingEnabled(newState)).toBe( false ) }) }) describe('when switching off with more then one column sorting (controlled)', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], multiColumnSort: false, sort: [ { columnId: '123', direction: 'desc' }, { columnId: '234', direction: 'asc' }, ], }, }) }) it('should not edit the sort items', () => { expect(selectSort(newState)).toEqual([ { columnId: '123', direction: 'desc' }, { columnId: '234', direction: 'asc', }, ]) }) it('should store setting', () => { expect(selectIsMultiColumnSortingEnabled(newState)).toBe( false ) }) }) describe('when switching off with one column sorting', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer( getStateMock({ columns: [{ columnId: '234', direction: 'asc' }], }), { type: 'applyProps', payload: { columns: [], rows: [], multiColumnSort: false, }, } ) }) it('should not edit sort', () => { expect(selectSort(newState)).toEqual([ { columnId: '234', direction: 'asc', }, ]) }) it('should store setting', () => { expect(selectIsMultiColumnSortingEnabled(newState)).toBe( false ) }) }) }) describe('with default sort config', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], defaultSort: [{ columnId: '123', direction: 'asc' }], }, }) }) it('should store sort config', () => { expect(selectColumnSort(newState, '123')).toEqual({ columnId: '123', direction: 'asc', }) }) it('should not use an updated defaultSort value', () => { const thirdState = sortReducer(newState, { type: 'applyProps', payload: { columns: [], rows: [], defaultSort: [{ columnId: '234', direction: 'desc' }], }, }) expect(selectColumnSort(thirdState, '123')).toEqual({ columnId: '123', direction: 'asc', }) }) }) }) describe('updateSort', () => { it('should update column configuration', () => { const newState = sortReducer(getStateMock(), { type: 'updateSort', payload: [{ columnId: 'title', direction: 'asc' }], }) expect(newState.columns).toEqual([ { columnId: 'title', direction: 'asc' }, ]) }) }) describe('updateSortLocale', () => { it('should update column configuration', () => { const newState = sortReducer(getStateMock(), { type: 'updateSortLocale', payload: 'sv', }) expect(newState.locale).toEqual('sv') }) }) describe('selectSort', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], sort: [{ columnId: '123', direction: 'asc' }], sortMode: 'external', }, }) }) it('should return the sort', () => { expect(selectSort(newState)).toEqual([ { columnId: '123', direction: 'asc' }, ]) }) }) describe('selectDefaultSort', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], defaultSort: [{ columnId: '234', direction: 'desc' }], sort: [{ columnId: '123', direction: 'asc' }], sortMode: 'external', }, }) }) it('should return the sort', () => { expect(selectDefaultSort(newState)).toEqual([ { columnId: '234', direction: 'desc' }, ]) }) }) describe('selectIsMultiColumnSorting', () => { it('should return true if more than one column is sorting', () => { expect( selectIsMultiColumnSorting( getStateMock({ columns: [ { columnId: '123', direction: 'asc' }, { columnId: '234', direction: 'desc' }, ], }) ) ).toBe(true) }) it('should return false if one column is sorting', () => { expect( selectIsMultiColumnSorting( getStateMock({ columns: [{ columnId: '123', direction: 'asc' }], }) ) ).toBe(false) }) it('should return false if no columns are sorting', () => { expect(selectIsMultiColumnSorting(getStateMock())).toBe(false) }) }) describe('selectIsMultiColumnSortingEnabled', () => { it('should return the value', () => { expect(selectIsMultiColumnSortingEnabled(getStateMock())).toBe(true) }) }) describe('selectColumnSortNumber', () => { describe('with only one column', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], sort: [{ columnId: '123', direction: 'asc' }], sortMode: 'external', }, }) }) it('should return 0', () => { expect(selectColumnSortNumber(newState, '123')).toBe(0) }) }) describe('with more than one column', () => { let newState: GridSortState beforeEach(() => { newState = sortReducer(getStateMock(), { type: 'applyProps', payload: { columns: [], rows: [], multiColumnSort: true, sort: [ { columnId: '123', direction: 'asc' }, { columnId: '234', direction: 'desc' }, ], sortMode: 'external', }, }) }) it('should return 0 if not found', () => { expect(selectColumnSortNumber(newState, '345')).toBe(0) }) it('should return the correct number', () => { expect(selectColumnSortNumber(newState, '123')).toBe(1) expect(selectColumnSortNumber(newState, '234')).toBe(2) }) }) }) })