import { getSortedChildren } from './get-sorted-children' import type { Collection } from './types' const collection: Collection = { ids: ['1', '2', '3', '4'], entities: new Map([ ['1', { id: '1' }], ['2', { id: '2' }], ['3', { id: '3' }], ['4', { id: '4' }], ['11', { id: '11' }], ['12', { id: '12' }], ['13', { id: '13' }], ['22', { id: '22' }], ['44', { id: '44' }], ]), meta: new Map([ ['1', { type: 'tree', children: ['11', '12', '13', '14'] }], ['2', { type: 'tree', children: ['22'] }], ['4', { type: 'tree', children: ['44'] }], ]), } describe('getSortedChildren', () => { describe('with no sort function', () => { describe('with parentId', () => { it('should return the children if present', () => { expect(getSortedChildren(collection, '1', null)).toEqual([ '11', '12', '13', '14', ]) }) it('should return an empty array if no children are present', () => { expect(getSortedChildren(collection, '3', null)).toEqual([]) }) }) describe('with a null parentId', () => { it('should return root ids', () => { expect(getSortedChildren(collection, null, null)).toEqual([ '1', '2', '3', '4', ]) }) }) }) describe('with a sort function', () => { describe('with parentId', () => { it('should return the sorted children', () => { const sort = jest.fn().mockReturnValue(['14', '13', '12', '11']) expect(getSortedChildren(collection, '1', sort)).toEqual([ '14', '13', '12', '11', ]) expect(sort).toBeCalledWith( ['11', '12', '13', '14'], collection.entities, collection.meta ) }) }) describe('with a null parentId', () => { it('should return sorted root ids', () => { const sort = jest.fn().mockReturnValue(['4', '3', '2', '1']) expect(getSortedChildren(collection, null, sort)).toEqual([ '4', '3', '2', '1', ]) expect(sort).toBeCalledWith( ['1', '2', '3', '4'], collection.entities, collection.meta ) }) }) }) })