import * as stubData from './stub-data' import type { GridRowId } from '../types' import { setSelection } from './selection' import type { Collection } from './types' describe('setSelection', () => { let collection: Collection let renderedIds: GridRowId[] describe('basic', () => { beforeEach(() => { collection = stubData.basic renderedIds = ['1', '2', '3', '4'] }) it('should select a single id', () => { expect( setSelection(renderedIds, collection, new Set(['1']), '2', true) ).toEqual(new Set(['1', '2'])) }) it('should remove a single id', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2']), '2', false ) ).toEqual(new Set(['1'])) }) it('should not error when removing a value that is not selected', () => { expect( setSelection( renderedIds, collection, new Set(['1']), '2', false ) ).toEqual(new Set(['1'])) }) it('should select a range', () => { expect( setSelection( renderedIds, collection, new Set([]), ['2', '4'], true ) ).toEqual(new Set(['2', '3', '4'])) }) it('should select a range (excluding filtered rows)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['2', '4'], true, new Set(['2', '4']) ) ).toEqual(new Set(['2', '4'])) }) it('should select a range (inverted location)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['4', '2'], true ) ).toEqual(new Set(['2', '3', '4'])) }) it('should remove a range', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2', '4']), ['4', '2'], false ) ).toEqual(new Set(['1'])) }) }) describe('tree', () => { beforeEach(() => { collection = stubData.tree renderedIds = ['1', '2', '3'] }) it('should select a single id', () => { expect( setSelection(renderedIds, collection, new Set(['1']), '2', true) ).toEqual(new Set(['1', '2'])) }) it('should remove a single id', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2']), '2', false ) ).toEqual(new Set(['1'])) }) it('should not error when removing a value that is not selected', () => { expect( setSelection( renderedIds, collection, new Set(['1']), '2', false ) ).toEqual(new Set(['1'])) }) it('should select a range', () => { expect( setSelection( renderedIds, collection, new Set([]), ['1', '3'], true ) ).toEqual(new Set(['1', '2', '3'])) }) it('should select a range (excluding filtered rows)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['1', '3'], true, new Set(['2']) ) ).toEqual(new Set(['2'])) }) it('should select a range (inverted location)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['3', '1'], true ) ).toEqual(new Set(['1', '2', '3'])) }) it('should remove a range', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2', '4']), ['1', '3'], false ) ).toEqual(new Set(['4'])) }) }) describe('group', () => { beforeEach(() => { collection = stubData.group renderedIds = ['group-1', '1', '4', 'group-2'] }) it('should select a single group (all descendants)', () => { expect( setSelection( renderedIds, collection, new Set(['1']), 'group-1', true ) ).toEqual(new Set(['1', '4'])) }) it('should remove a single group (all descendants)', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2']), 'group-1', false ) ).toEqual(new Set(['2'])) }) it('should not error when removing a value that is not selected', () => { expect( setSelection( renderedIds, collection, new Set(['1']), 'group-2', false ) ).toEqual(new Set(['1'])) }) it('should select a range', () => { expect( setSelection( renderedIds, collection, new Set([]), ['4', 'group-2'], true ) ).toEqual(new Set(['4', '2', '3'])) }) it('should select a range (excluding filtered rows)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['4', 'group-2'], true, new Set(['4', 'group-2', '2']) ) ).toEqual(new Set(['4', '2'])) }) it('should select a range (inverted location)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['group-2', '4'], true ) ).toEqual(new Set(['4', '2', '3'])) }) it('should remove a range', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2', '4']), ['4', 'group-2'], false ) ).toEqual(new Set(['1'])) }) }) describe('groupedTree', () => { beforeEach(() => { collection = stubData.groupedTree renderedIds = ['group-1', '1', 'group-2', '3', '4'] }) it('should select a single group (all descendants)', () => { expect( setSelection( renderedIds, collection, new Set(['1']), 'group-1', true ) ).toEqual(new Set(['1', '2'])) }) it('should remove a single group (all descendants)', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2']), 'group-1', false ) ).toEqual(new Set([])) }) it('should not error when removing a value that is not selected', () => { expect( setSelection( renderedIds, collection, new Set(['1']), 'group-2', false ) ).toEqual(new Set(['1'])) }) it('should select a range (with group)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['group-1', '1'], true ) ).toEqual(new Set(['1', '2'])) }) it('should select a range (with group)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['group-1', '1'], true, new Set(['1']) ) ).toEqual(new Set(['1'])) }) it('should select a range (without group)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['3', '4'], true ) ).toEqual(new Set(['3', '4'])) }) it('should select a range (inverted location)', () => { expect( setSelection( renderedIds, collection, new Set([]), ['1', 'group-1'], true ) ).toEqual(new Set(['1', '2'])) }) it('should remove a range', () => { expect( setSelection( renderedIds, collection, new Set(['1', '2', '4']), ['1', 'group-1'], false ) ).toEqual(new Set(['4'])) }) }) })