import type { GridState } from '..' import { INITIAL_STATE } from '..' import { jest } from '@jest/globals' import type { StoreSimpleSelectors } from '../selectors' import type { SelectAllSelectableIds, SelectIsRowSelected, SelectIsRowVisuallySelected, SelectRowExcluded, SelectRowSelectionState, SelectSelectionStatus, } from './selection' import generateSelectors from './selection' import type { SelectFilterMatches, SelectFilterMatchesShowOnly } from './filter' const state = {} as GridState describe('compositeSelectors - selection', () => { let selectRowSelectionState: SelectRowSelectionState let selectIsRowSelected: SelectIsRowSelected let selectIsRowVisuallySelected: SelectIsRowVisuallySelected let selectSelectionStatus: SelectSelectionStatus let selectAllSelectableIds: SelectAllSelectableIds let selectRowExcluded: SelectRowExcluded let selectSelectionMode: jest.Mock< StoreSimpleSelectors['selectSelectionMode'] > let selectRowCollection: jest.Mock< StoreSimpleSelectors['selectRowCollection'] > let selectSelection: jest.Mock let selectFilterMatches: jest.Mock let selectFilterMatchesShowOnly: jest.Mock beforeEach(() => { selectSelectionMode = jest .fn() .mockReturnValue('none') selectRowCollection = jest .fn() .mockReturnValue(INITIAL_STATE.rows.collection) selectSelection = jest .fn() .mockReturnValue(new Set()) selectFilterMatches = jest.fn().mockReturnValue({ matchedIds: null, necessaryMatchedIds: null, }) selectFilterMatchesShowOnly = jest .fn() .mockReturnValue(null) ;({ selectRowSelectionState, selectIsRowSelected, selectIsRowVisuallySelected, selectSelectionStatus, selectAllSelectableIds, selectRowExcluded, } = generateSelectors( { selectSelectionMode, selectRowCollection, selectSelection, }, selectFilterMatches as any, selectFilterMatchesShowOnly as any )) }) describe('selectRowSelectionState', () => { describe('when selection mode is none', () => { it('should return none', () => { selectSelectionMode.mockReturnValue('none') expect(selectRowSelectionState(state, '1')).toBe('none') }) }) describe('when selection mode is something other than none', () => { it('should return status', () => { selectSelectionMode.mockReturnValue('single') selectRowCollection.mockReturnValue({ ...INITIAL_STATE.rows.collection, entities: new Map( Object.entries({ 1: { id: '1' }, }) ), ids: ['1'], }) selectSelection.mockReturnValue(new Set(['1'])) expect(selectRowSelectionState(state, '1')).toBe('all') }) }) }) describe('selectIsRowSelected', () => { describe('when selection mode is none', () => { it('should return false', () => { selectSelectionMode.mockReturnValue('none') expect(selectIsRowSelected(state, '1')).toBe(false) }) }) describe('when selection mode is something other than none', () => { it('should return status', () => { selectSelectionMode.mockReturnValue('single') selectRowCollection.mockReturnValue({ ...INITIAL_STATE.rows.collection, entities: new Map( Object.entries({ 1: { id: '1' }, }) ), ids: ['1'], }) selectSelection.mockReturnValue(new Set(['1'])) expect(selectIsRowSelected(state, '1')).toBe(true) }) }) }) describe('selectIsRowVisuallySelected', () => { it('should return true if selection contains id', () => { selectSelectionMode.mockReturnValue('single') selectSelection.mockReturnValue(new Set(['1', '2'])) expect(selectIsRowSelected(state, '1')).toBe(true) }) }) describe('selectSelectionStatus', () => { it('should return the status', () => { expect(selectSelectionStatus(state)).toBe('none') }) }) describe('selectAllSelectableIds', () => { it('should return ids', () => { selectFilterMatchesShowOnly.mockReturnValue(new Set(['1', '3'])) selectRowCollection.mockReturnValue({ ...INITIAL_STATE.rows.collection, entities: new Map( Object.entries({ 1: { id: '1' }, 2: { id: '2' }, 3: { id: '3' }, }) ), ids: ['1', '2', '3'], }) expect(selectAllSelectableIds(state)).toEqual(new Set(['1', '3'])) }) }) describe('selectRowExcluded', () => { it('should return false if filter is not active', () => { expect(selectRowExcluded(state, '1')).toBe(false) }) it('should return true if filter is active and does not contain value', () => { selectFilterMatches.mockReturnValue({ matchedIds: new Set(['1', '3']), necessaryMatchedIds: new Set(['3']), }) expect(selectRowExcluded(state, '1')).toBe(true) }) it('should return false if filter is active and contains value', () => { selectFilterMatches.mockReturnValue({ matchedIds: new Set(['1', '3']), necessaryMatchedIds: new Set(['3']), }) expect(selectRowExcluded(state, '3')).toBe(false) }) }) })