import * as stubData from './stub-data' import type { Collection } from './types' import { filter } from './filter' import type { GridRowMeta } from '../types' describe('filter', () => { let collection: Collection describe('basic', () => { beforeEach(() => { collection = stubData.basic }) it('should return matching ids (custom fn)', () => { const filterFn = (row: any) => row.title === 'Beta' const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual(['4']) expect([...filteredIds.necessaryMatches]).toEqual(['4']) }) it('should return matching ids (id based fn)', () => { const incomingIds = new Set(['1', '3']) const filterFn = (row: any) => incomingIds.has(row.id) const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual(['1', '3']) expect([...filteredIds.necessaryMatches]).toEqual(['1', '3']) }) }) describe('tree', () => { beforeEach(() => { collection = stubData.tree }) it('should return matching ids (custom fn)', () => { const filterFn = (row: any) => row.title === 'Beta' || row.title === 'Zeta' const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual(['1', '2', '4']) expect([...filteredIds.necessaryMatches]).toEqual([ '1', '2', '3', '4', ]) }) it('should return matching ids (id based fn)', () => { const incomingIds = new Set(['2']) const filterFn = (row: any) => incomingIds.has(row.id) const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual(['2']) expect([...filteredIds.necessaryMatches]).toEqual(['1', '2']) }) }) describe('group', () => { beforeEach(() => { collection = stubData.group }) describe('with normal filtering', () => { it('should return matching ids (custom fn)', () => { const filterFn = (row: any, meta: GridRowMeta) => row.title === 'Beta' || row.title === 'Alpha' || !!meta.children?.includes('1') const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual([ 'group-1', '1', '4', '3', ]) expect([...filteredIds.necessaryMatches]).toEqual([ 'group-1', '1', '4', 'group-2', '3', ]) }) it('should return matching ids (id based fn)', () => { const incomingIds = new Set(['2']) const filterFn = (row: any) => incomingIds.has(row.id) const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual(['2']) expect([...filteredIds.necessaryMatches]).toEqual([ 'group-2', '2', ]) }) }) }) describe('grouped tree', () => { beforeEach(() => { collection = stubData.groupedTree }) describe('with normal filtering', () => { it('should return matching ids (custom fn)', () => { const filterFn = (row: any, meta: GridRowMeta) => row.title === 'Beta' || row.title === 'Zeta' || !!meta.children?.includes('1') const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual([ 'group-1', '1', '2', '4', ]) expect([...filteredIds.necessaryMatches]).toEqual([ 'group-1', '1', '2', 'group-2', '3', '4', ]) }) it('should return matching ids (id based fn)', () => { const incomingIds = new Set(['2']) const filterFn = (row: any) => incomingIds.has(row.id) const filteredIds = filter(collection, filterFn) expect([...filteredIds.actualMatches]).toEqual(['2']) expect([...filteredIds.necessaryMatches]).toEqual([ 'group-1', '1', '2', ]) }) }) }) })