import * as stubData from './stub-data-large' import type { GridRowId } from '../types' import { flatten } from './flatten' import type { Collection } from './types' import { buildSort } from '../utils/build-sort' const titleSortFn = buildSort( [ { columnId: 'title', direction: 'asc', }, ], 'en-US', { title: { sortStrategy: 'fast' } } ) describe('sort', () => { let collection: Collection let expanded: Set function getField(ids: GridRowId[], fieldName: string) { return ids.map((id) => { const row = collection.entities.get(id)! return row[fieldName as keyof typeof row] }) } describe('basic', () => { beforeEach(() => { collection = stubData.basic expanded = new Set() }) it('should return unsorted records without a sort function', () => { const sorted = flatten(collection, expanded, null) expect(getField(sorted.ids, 'title')).toEqual([ 'Zeta', 'Gamma', 'Alpha', 'Beta', 'Kappa', ]) expect(sorted.indexes).toEqual( new Map([ ['1', 0], ['2', 1], ['3', 2], ['4', 3], ['5', 4], ]) ) expect(sorted.setDetails).toEqual( new Map([ ['1', { setSize: 5, posInset: 1 }], ['2', { setSize: 5, posInset: 2 }], ['3', { setSize: 5, posInset: 3 }], ['4', { setSize: 5, posInset: 4 }], ['5', { setSize: 5, posInset: 5 }], ]) ) }) it('should sort records', () => { const sorted = flatten(collection, expanded, titleSortFn) expect(getField(sorted.ids, 'title')).toEqual([ 'Alpha', 'Beta', 'Gamma', 'Kappa', 'Zeta', ]) expect(sorted.indexes).toEqual( new Map([ ['3', 0], ['4', 1], ['2', 2], ['5', 3], ['1', 4], ]) ) expect(sorted.setDetails).toEqual( new Map([ ['3', { setSize: 5, posInset: 1 }], ['4', { setSize: 5, posInset: 2 }], ['2', { setSize: 5, posInset: 3 }], ['5', { setSize: 5, posInset: 4 }], ['1', { setSize: 5, posInset: 5 }], ]) ) }) it('should sort filtered records', () => { const sorted = flatten( collection, new Set(), titleSortFn, new Set(['1', '3', '5']) ) expect(getField(sorted.ids, 'title')).toEqual([ 'Alpha', 'Kappa', 'Zeta', ]) expect(sorted.indexes).toEqual( new Map([ ['3', 0], ['5', 1], ['1', 2], ]) ) expect(sorted.setDetails).toEqual( new Map([ ['3', { setSize: 3, posInset: 1 }], ['5', { setSize: 3, posInset: 2 }], ['1', { setSize: 3, posInset: 3 }], ]) ) }) }) describe('tree', () => { beforeEach(() => { collection = stubData.tree expanded = stubData.expandedTree }) it('should return unsorted records without a sort function (excluding collapsed trees)', () => { const sorted = flatten(collection, expanded, null) expect(getField(sorted.ids, 'title')).toEqual([ 'Zeta', 'Gamma', 'Kappa', 'Delta', 'Alpha', ]) expect(sorted.indexes).toEqual( new Map([ ['1', 0], ['2', 1], ['5', 2], ['6', 3], ['3', 4], ['4', 5], ]) ) expect(sorted.setDetails).toEqual( new Map([ ['1', { setSize: 2, posInset: 1 }], ['2', { setSize: 3, posInset: 1 }], ['5', { setSize: 3, posInset: 2 }], ['6', { setSize: 3, posInset: 3 }], ['3', { setSize: 2, posInset: 2 }], ['4', { setSize: 1, posInset: 1 }], ]) ) }) it('should sort records (excluding collapsed trees)', () => { const sorted = flatten(collection, expanded, titleSortFn) expect(getField(sorted.ids, 'title')).toEqual([ 'Alpha', 'Zeta', 'Delta', 'Gamma', 'Kappa', ]) expect(sorted.indexes).toEqual( new Map([ ['3', 0], ['4', 1], ['1', 2], ['6', 3], ['2', 4], ['5', 5], ]) ) expect(sorted.setDetails).toEqual( new Map([ ['3', { setSize: 2, posInset: 1 }], ['4', { setSize: 1, posInset: 1 }], ['1', { setSize: 2, posInset: 2 }], ['6', { setSize: 3, posInset: 1 }], ['2', { setSize: 3, posInset: 2 }], ['5', { setSize: 3, posInset: 3 }], ]) ) }) it('should sort filtered records', () => { const sorted = flatten( collection, expanded, titleSortFn, new Set(['1', '3', '5']) ) expect(getField(sorted.ids, 'title')).toEqual([ 'Alpha', 'Zeta', 'Kappa', ]) expect(sorted.indexes).toEqual( new Map([ ['3', 0], ['1', 1], ['5', 2], ]) ) expect(sorted.setDetails).toEqual( new Map([ ['3', { setSize: 2, posInset: 1 }], ['1', { setSize: 2, posInset: 2 }], ['5', { setSize: 1, posInset: 1 }], ]) ) }) }) })