Skip to content

Commit

Permalink
feat(filter): add Filter Service, Filter Conditions and few unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Mar 16, 2020
1 parent 30af496 commit 2baed7f
Show file tree
Hide file tree
Showing 61 changed files with 9,409 additions and 93 deletions.
8 changes: 8 additions & 0 deletions packages/common/src/enums/gridAutosizeColsMode.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum GridAutosizeColsMode {
none = 'NOA',
legacyOff = 'LOF',
legacyForceFit = 'LFF',
ignoreViewport = 'IGV',
fitColsToViewport = 'FCV',
fitViewportToCols = 'FVC'
};
5 changes: 3 additions & 2 deletions packages/common/src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ export * from './delimiterType.enum';
export * from './emitterType.enum';
export * from './eventNamingStyle.enum';
export * from './extensionName.enum';
export * from './fieldType.enum';
export * from './fileType.enum';
export * from './filterMultiplePassType.enum';
export * from './filterMultiplePassTypeString.type';
export * from './gridAutosizeColsMode.enum';
export * from './keyCode.enum';
export * from './operatorString.type';
export * from './fieldType.enum';
export * from './operatorType.enum';
export * from './searchTerm.type';
export * from './sortDirection.enum';
export * from './sortDirectionNumber.enum';
export * from './sortDirectionString.type';
export * from './sortDirection.enum';
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { FieldType } from '../../enums/index';
import { FilterConditionOption } from '../../interfaces/index';
import { booleanFilterCondition } from '../booleanFilterCondition';
import { executeMappedCondition } from '../executeMappedCondition';

/** will return True in all cases with only 1 exception when the only searchTerm is inversed to the cell value */

describe('booleanFilterCondition method', () => {
it('should return True when no cell value is provided, neither search terms', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: '', fieldType: FieldType.boolean } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when any cell value is provided', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: 'foo', fieldType: FieldType.boolean } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when boolean value True is provided as cell value and called from the "executeMappedCondition"', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: 'true', fieldType: FieldType.boolean, searchTerms: ['true'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return True when boolean value True is provided as cell value', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: 'true', fieldType: FieldType.boolean, searchTerms: ['true'] } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when boolean value provided is equal to the searchTerms even when it is a string type', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: true, fieldType: FieldType.boolean, searchTerms: ['true'] } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when the cell value is equal to at least 1 of the searchTerms', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: true, fieldType: FieldType.boolean, searchTerms: ['true', 'false'] } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(true);
});

it('should return False when cell value is inversed to the searchTerm', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: false, fieldType: FieldType.boolean, searchTerms: ['true'] } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(false);
});

it('should return False even when Operator is Not Equal because condition is always a strict equal check', () => {
const options = { dataKey: '', operator: 'NE', cellValue: false, fieldType: FieldType.boolean, searchTerms: ['true'] } as FilterConditionOption;
const output = booleanFilterCondition(options);
expect(output).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { FieldType } from '../../enums/index';
import { FilterConditionOption } from '../../interfaces/index';
import { collectionSearchFilterCondition } from '../collectionSearchFilterCondition';
import { executeMappedCondition } from '../executeMappedCondition';

describe('collectionSearchFilterCondition method', () => {
it('should return False when searchTerms is empty', () => {
const options = { dataKey: '', operator: 'IN', cellValue: 3, fieldType: FieldType.string } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(false);
});

it('should return True when input value is in the searchTerms', () => {
const options = { dataKey: '', operator: 'IN', cellValue: 3, fieldType: FieldType.string, searchTerms: ['3'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when input value provided is equal to the searchTerms', () => {
const options = { dataKey: '', operator: 'IN', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['foo'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when input value provided is equal to the searchTerms even though there are no Operator provided (it will use EQ as default)', () => {
const options = { dataKey: '', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['foo'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when the cell value is equal to at least 1 of the searchTerms', () => {
const options = { dataKey: '', operator: 'IN', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['bar', 'foo', 'John'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when the cell value is equal to at least 1 of the searchTerms and called by the "executeMappedCondition"', () => {
const options = { dataKey: '', operator: 'IN', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['bar', 'foo', 'John'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when cell value is not within the searchTerms', () => {
const options = { dataKey: '', operator: 'IN', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['bar'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(false);
});

it('should return False even when Operator is Not IN because condition is always a strict equal check', () => {
const options = { dataKey: '', operator: 'NOT_IN', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['foo'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(false);
});

it('should return False even when Operator is NIN because condition is always a strict equal check', () => {
const options = { dataKey: '', operator: 'NIN', cellValue: 'foo', fieldType: FieldType.string, searchTerms: ['foo'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(false);
});

it('should return True when input value contains searchTerms content', () => {
const options = { dataKey: '', operator: 'IN_CONTAINS', cellValue: 'Task2,Task3', fieldType: FieldType.string, searchTerms: ['Task2', 'Task3'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(true);
});

it('should return False when input value not in contains searchTerms content', () => {
const options = { dataKey: '', operator: 'NIN_CONTAINS', cellValue: 'Task2,Task3', fieldType: FieldType.string, searchTerms: ['Task2', 'Task3'] } as FilterConditionOption;
const output = collectionSearchFilterCondition(options);
expect(output).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { FieldType } from '../../enums/index';
import { FilterConditionOption } from '../../interfaces/index';
import { executeMappedCondition } from '../executeMappedCondition';

describe('dateEuroFilterCondition method', () => {
it('should return False when no cell value is provided, neither search terms', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuro, cellValue: '' } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when any cell value is provided without any search terms', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuro, cellValue: '25/12/2000' } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when search term is not a valid date', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuro, cellValue: '25/12/2000', searchTerms: ['14/25/2000'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True when input value provided is equal to the searchTerms', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: ['25/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when cell value is not the same value as the searchTerm', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: ['14/03/2003'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False even when the cell value is found in the searchTerms since it only compares first term', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: ['14/03/2003', '25/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False even when Operator is Not Equal and cell value equals the search term', () => {
const options = { dataKey: '', operator: 'NE', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: ['25/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True even when Operator is Not Equal and cell value is different than the search term', () => {
const options = { dataKey: '', operator: 'NE', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: ['25/12/2002'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when there are no search term and no operator', () => {
const options = { dataKey: '', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: [null] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when search and cell values are different and there are no operator passed, it will use default EQ operator', () => {
const options = { dataKey: '', fieldType: FieldType.dateEuro, cellValue: '25/12/1993', searchTerms: ['27/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True when input value is in the range of search terms', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: '25/12/1993', fieldType: FieldType.dateEuro, searchTerms: ['01/12/1993..31/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when input value is not in the range of search terms', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: '25/11/1993', fieldType: FieldType.dateEuro, searchTerms: ['01/12/1993..31/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True when input value equals the search terms min inclusive value and operator is set to "rangeInclusive"', () => {
const options = { dataKey: '', operator: 'RangeInclusive', cellValue: '01/12/1993', fieldType: FieldType.dateEuro, searchTerms: ['01/12/1993..31/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when input value equals the search terms min inclusive value and operator is set to "RangeExclusive"', () => {
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '01/12/1993', fieldType: FieldType.dateEuro, searchTerms: ['01/12/1993..31/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when any of the 2 search term value is not a valid date', () => {
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '05/12/1993', fieldType: FieldType.dateEuro, searchTerms: ['01/12/1993..60/12/1993'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { FieldType } from '../../enums/index';
import { FilterConditionOption } from '../../interfaces/index';
import { executeMappedCondition } from '../executeMappedCondition';

describe('dateEuroShortFilterCondition method', () => {
it('should return False when no cell value is provided, neither search terms', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuroShort, cellValue: '' } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when any cell value is provided without any search terms', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuroShort, cellValue: '25/12/00' } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when search term is not a valid date', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuroShort, cellValue: '25/12/00', searchTerms: ['14/25/00'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True when input value provided is equal to the searchTerms', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: ['25/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when cell value is not the same value as the searchTerm', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: ['14/03/03'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False even when the cell value is found in the searchTerms since it only compares first term', () => {
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: ['14/03/2003', '25/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False even when Operator is Not Equal and cell value equals the search term', () => {
const options = { dataKey: '', operator: 'NE', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: ['25/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True even when Operator is Not Equal and cell value is different than the search term', () => {
const options = { dataKey: '', operator: 'NE', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: ['25/12/02'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when there are no search term and no operator', () => {
const options = { dataKey: '', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: [null] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when search and cell values are different and there are no operator passed, it will use default EQ operator', () => {
const options = { dataKey: '', fieldType: FieldType.dateEuroShort, cellValue: '25/12/93', searchTerms: ['27/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True when input value is in the range of search terms', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: '25/12/93', fieldType: FieldType.dateEuroShort, searchTerms: ['01/12/93..31/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when input value is not in the range of search terms', () => {
const options = { dataKey: '', operator: 'EQ', cellValue: '25/11/93', fieldType: FieldType.dateEuroShort, searchTerms: ['01/12/93..31/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return True when input value equals the search terms min inclusive value and operator is set to "rangeInclusive"', () => {
const options = { dataKey: '', operator: 'RangeInclusive', cellValue: '01/12/93', fieldType: FieldType.dateEuroShort, searchTerms: ['01/12/93..31/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(true);
});

it('should return False when input value equals the search terms min inclusive value and operator is set to "RangeExclusive"', () => {
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '01/12/93', fieldType: FieldType.dateEuroShort, searchTerms: ['01/12/93..31/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});

it('should return False when any of the 2 search term value is not a valid date', () => {
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '05/12/93', fieldType: FieldType.dateEuroShort, searchTerms: ['01/12/93..60/12/93'] } as FilterConditionOption;
const output = executeMappedCondition(options);
expect(output).toBe(false);
});
});
Loading

0 comments on commit 2baed7f

Please sign in to comment.