From 307ca39a399da710095165cbb980068bc4ef4e1c Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Wed, 17 Mar 2021 20:25:20 -0400 Subject: [PATCH] fix(sorting): add some unit tests that were previously commented out --- .../services/__tests__/sort.service.spec.ts | 67 ++++++++++--------- packages/common/src/services/sort.service.ts | 1 - 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/packages/common/src/services/__tests__/sort.service.spec.ts b/packages/common/src/services/__tests__/sort.service.spec.ts index 00b71f9db..7b753774c 100644 --- a/packages/common/src/services/__tests__/sort.service.spec.ts +++ b/packages/common/src/services/__tests__/sort.service.spec.ts @@ -639,51 +639,54 @@ describe('SortService', () => { const mockColumns = [{ id: 'firstName', field: 'firstName' }, { id: 'lastName', field: 'lastName' }] as Column[]; beforeEach(() => { - // gridOptionMock.presets = { - // sorters: [{ columnId: 'firstName', direction: 'ASC' }, { columnId: 'lastName', direction: 'DESC' }], - // }; + gridOptionMock.presets = { + sorters: [{ columnId: 'firstName', direction: 'ASC' }, { columnId: 'lastName', direction: 'DESC' }], + }; jest.spyOn(gridStub, 'getColumns').mockReturnValue(mockColumns); }); - // it('should load local grid presets', () => { - // const spySetCols = jest.spyOn(gridStub, 'setSortColumns'); - // const spySortChanged = jest.spyOn(service, 'onLocalSortChanged'); - // const expectation = [ - // { columnId: 'firstName', sortAsc: true, sortCol: { id: 'firstName', field: 'firstName' } }, - // { columnId: 'lastName', sortAsc: false, sortCol: { id: 'lastName', field: 'lastName' } }, - // ]; + it('should load local grid presets', () => { + const spySetCols = jest.spyOn(gridStub, 'setSortColumns'); + const spySortChanged = jest.spyOn(service, 'onLocalSortChanged'); + const expectation = [ + { columnId: 'firstName', sortAsc: true, sortCol: { id: 'firstName', field: 'firstName' } }, + { columnId: 'lastName', sortAsc: false, sortCol: { id: 'lastName', field: 'lastName' } }, + ]; - // service.bindLocalOnSort(gridStub); - // service.loadGridSorters(gridOptionMock.presets.sorters); + service.bindLocalOnSort(gridStub); + service.loadGridSorters(gridOptionMock.presets.sorters); - // expect(spySetCols).toHaveBeenCalledWith(expectation); - // expect(spySortChanged).toHaveBeenCalledWith(gridStub, expectation); - // }); + expect(spySetCols).toHaveBeenCalledWith([ + { columnId: 'firstName', sortAsc: true, }, + { columnId: 'lastName', sortAsc: false }, + ]); + expect(spySortChanged).toHaveBeenCalledWith(gridStub, expectation); + }); }); describe('undefined getColumns & getOptions', () => { - // it('should use an empty column definition when grid "getColumns" method is not available', () => { - // gridOptionMock.presets = { - // sorters: [{ columnId: 'firstName', direction: 'ASC' }, { columnId: 'lastName', direction: 'DESC' }], - // }; - // const spySetCols = jest.spyOn(gridStub, 'setSortColumns'); - // gridStub.getColumns = undefined; + it('should use an empty column definition when grid "getColumns" method is not available', () => { + gridOptionMock.presets = { + sorters: [{ columnId: 'firstName', direction: 'ASC' }, { columnId: 'lastName', direction: 'DESC' }], + }; + const spySetCols = jest.spyOn(gridStub, 'setSortColumns'); + gridStub.getColumns = undefined; - // service.bindLocalOnSort(gridStub); - // service.loadGridSorters(gridOptionMock.presets.sorters); + service.bindLocalOnSort(gridStub); + service.loadGridSorters(gridOptionMock.presets.sorters); - // expect(spySetCols).not.toHaveBeenCalled(); - // }); + expect(spySetCols).toHaveBeenCalledWith([]); + }); - // it('should use an empty grid option object when grid "getOptions" method is not available', () => { - // const spySetCols = jest.spyOn(gridStub, 'setSortColumns'); - // gridStub.getOptions = undefined; + it('should use an empty grid option object when grid "getOptions" method is not available', () => { + const spySetCols = jest.spyOn(gridStub, 'setSortColumns'); + gridStub.getOptions = undefined; - // service.bindLocalOnSort(gridStub); - // service.loadGridSorters(gridOptionMock.presets.sorters); + service.bindLocalOnSort(gridStub); + service.loadGridSorters(gridOptionMock.presets.sorters); - // expect(spySetCols).not.toHaveBeenCalled(); - // }); + expect(spySetCols).toHaveBeenCalledWith([]); + }); }); describe('onLocalSortChanged method', () => { diff --git a/packages/common/src/services/sort.service.ts b/packages/common/src/services/sort.service.ts index 7ff8530a3..7b1869e42 100644 --- a/packages/common/src/services/sort.service.ts +++ b/packages/common/src/services/sort.service.ts @@ -296,7 +296,6 @@ export class SortService { this.onLocalSortChanged(this._grid, sortCols); this._grid.setSortColumns(sortCols.map(col => ({ columnId: col.columnId, sortAsc: col.sortAsc }))); // use this to add sort icon(s) in UI - } return sortCols; }