-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): add Sorters full test suite (#164)
* feat(tests): add Sorters full test suite * feat(test): add CollectionService unit tests * feat(test): add CollectionService unit tests * refactor(build): add missing TSlint types * feat(tests): add extra Collection Service option and tests * refactor(circleci): try orb to upload codecov * refactor(circleci): rollback last commit * refactor(tests): remove unnecessary tests code and move to test folder
- Loading branch information
1 parent
5c6d028
commit d375939
Showing
19 changed files
with
587 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 272 additions & 0 deletions
272
src/aurelia-slickgrid/services/collection.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
import { I18N } from 'aurelia-i18n'; | ||
import { EventAggregator } from 'aurelia-event-aggregator'; | ||
import { BindingSignaler } from 'aurelia-templating-resources'; | ||
import { CollectionService } from './collection.service'; | ||
import { | ||
CollectionFilterBy, | ||
CollectionSortBy, | ||
FilterMultiplePassType, | ||
OperatorType, | ||
Column, | ||
FieldType, | ||
} from './../models/index'; | ||
|
||
describe('CollectionService', () => { | ||
let collection = []; | ||
let i18n: I18N; | ||
let service: CollectionService; | ||
|
||
// stub some methods of the SlickGrid Grid instance | ||
const gridStub = { | ||
getOptions: jest.fn() | ||
}; | ||
|
||
beforeEach(() => { | ||
i18n = new I18N(new EventAggregator(), new BindingSignaler()); | ||
i18n.setup({ | ||
resources: { | ||
en: { | ||
translation: { | ||
SALES_REP: 'Sales Rep.', | ||
FINANCE_MANAGER: 'Finance Manager', | ||
HUMAN_RESOURCES: 'Human Resources', | ||
IT_ADMIN: 'IT Admin', | ||
DEVELOPER: 'Developer', | ||
} | ||
}, | ||
fr: { | ||
translation: | ||
{ | ||
SALES_REP: 'Représentant des ventes', | ||
FINANCE_MANAGER: 'Responsable des finances', | ||
HUMAN_RESOURCES: 'Ressources humaines', | ||
IT_ADMIN: 'Administrateur IT', | ||
DEVELOPER: 'Développeur', | ||
} | ||
} | ||
}, | ||
lng: '0', | ||
fallbackLng: 'en', | ||
debug: false | ||
}); | ||
service = new CollectionService(i18n); | ||
|
||
collection = [ | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
]; | ||
}); | ||
|
||
afterEach(() => { | ||
collection = undefined; | ||
}); | ||
|
||
it('should create the service', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should return an array without certain filtered values', () => { | ||
const filterBy = { property: 'firstName', operator: 'NE', value: 'John' } as CollectionFilterBy; | ||
|
||
const result = service.filterCollection(collection, filterBy); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
]); | ||
}); | ||
|
||
it('should return an array without certain filtered valuess in a "chain" way', () => { | ||
const filterBy = [ | ||
{ property: 'firstName', operator: 'NE', value: 'John' }, | ||
{ property: 'lastName', operator: 'NE', value: 'Doe' } | ||
] as CollectionFilterBy[]; | ||
|
||
const result1 = service.filterCollection(collection, filterBy); | ||
const result2 = service.filterCollection(collection, filterBy, 'chain'); // chain is default | ||
|
||
expect(result1).toEqual([ | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
]); | ||
expect(result1).toEqual(result2); | ||
}); | ||
|
||
it('should return an array with merged output of filtered values', () => { | ||
const filterBy = [ | ||
{ property: 'firstName', operator: OperatorType.equal, value: 'John' }, | ||
{ property: 'lastName', value: 'Doe' } // ommitted operator are Equal by default | ||
] as CollectionFilterBy[]; | ||
|
||
const result = service.filterCollection(collection, filterBy, FilterMultiplePassType.merge); | ||
|
||
expect(result).toEqual([ | ||
// the array will have all "John" 1st, then all "Doe" | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
]); | ||
}); | ||
|
||
it('should return an array by using the "contains" filter type', () => { | ||
const filterBy = { property: 'firstName', operator: OperatorType.contains, value: 'Foo' } as CollectionFilterBy; | ||
|
||
const result = service.singleFilterCollection(collection, filterBy); | ||
|
||
expect(result).toEqual([{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }]); | ||
}); | ||
|
||
it('should return an array by using the "notContains" filter type', () => { | ||
const filterBy = { property: 'firstName', operator: OperatorType.notContains, value: 'John' } as CollectionFilterBy; | ||
|
||
const result = service.singleFilterCollection(collection, filterBy); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
]); | ||
}); | ||
|
||
it('should return a collection sorted by a "dataKey"', () => { | ||
const columnDef = { id: 'users', field: 'users', dataKey: 'lastName' } as Column; | ||
|
||
const result = service.sortCollection(columnDef, collection, { property: 'lastName', sortDesc: true, fieldType: FieldType.string }); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
]); | ||
}); | ||
|
||
it('should return a collection sorted by multiple sortBy entities', () => { | ||
const columnDef = { id: 'users', field: 'users', dataKey: 'lastName' } as Column; | ||
const sortBy = [ | ||
{ property: 'firstName', sortDesc: false, fieldType: FieldType.string }, | ||
{ property: 'lastName', sortDesc: true, fieldType: FieldType.string }, | ||
] as CollectionSortBy[]; | ||
|
||
const result = service.sortCollection(columnDef, collection, sortBy); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
]); | ||
}); | ||
|
||
it('should return a collection sorted by a sortyBy entity being a number', () => { | ||
const columnDef = { id: 'users', field: 'users', dataKey: 'lastName' } as Column; | ||
const sortBy = [ | ||
{ property: 'order', sortDesc: true, fieldType: FieldType.number }, | ||
] as CollectionSortBy[]; | ||
|
||
const result = service.sortCollection(columnDef, collection, sortBy); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
]); | ||
}); | ||
|
||
it('should return a collection sorted by multiple sortBy entities and their translated value', () => { | ||
i18n.setLocale('fr'); | ||
const columnDef = { id: 'users', field: 'users', dataKey: 'lastName' } as Column; | ||
const sortBy = [ | ||
{ property: 'firstName', sortDesc: false, fieldType: FieldType.string }, | ||
{ property: 'position', sortDesc: true }, // fieldType is string by default | ||
] as CollectionSortBy[]; | ||
|
||
const result = service.sortCollection(columnDef, collection, sortBy, true); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
]); | ||
}); | ||
|
||
it('should return a collection sorted by a single sortBy entity and their translated value', () => { | ||
i18n.setLocale('en'); | ||
const columnDef = { id: 'users', field: 'users' } as Column; | ||
const sortBy = { property: 'position', sortDesc: false } as CollectionSortBy; // fieldType is string by default | ||
|
||
const result = service.sortCollection(columnDef, collection, sortBy, true); | ||
|
||
expect(result).toEqual([ | ||
{ firstName: 'John', lastName: 'Doe', position: null, order: 5 }, | ||
{ firstName: 'John', lastName: 'Doe', position: 'DEVELOPER', order: 4 }, | ||
{ firstName: 'Jane', lastName: 'Doe', position: 'FINANCE_MANAGER', order: 1 }, | ||
{ firstName: 'Ava Luna', lastName: null, position: 'HUMAN_RESOURCES', order: 13 }, | ||
{ firstName: 'John', lastName: 'Z', position: 'SALES_REP', order: 10 }, | ||
{ firstName: '', lastName: 'Cash', position: 'SALES_REP', order: 3 }, | ||
{ firstName: 'Bob', lastName: 'Cash', position: 'SALES_REP', order: 0 }, | ||
{ firstName: 'John', lastName: 'Zachary', position: 'SALES_REP', order: 2 }, | ||
{ firstName: 'John Foo', lastName: 'Bar', position: 'SALES_REP', order: 8 }, | ||
]); | ||
}); | ||
|
||
it('should return a collection of numbers sorted', () => { | ||
i18n.setLocale('en'); | ||
const columnDef = { id: 'count', field: 'count', fieldType: FieldType.number } as Column; | ||
|
||
const result1 = service.sortCollection(columnDef, [0, -11, 3, 99999, -200], { property: '', sortDesc: false } as CollectionSortBy); | ||
const result2 = service.sortCollection(columnDef, [0, -11, 3, 99999, -200], { property: '', sortDesc: true } as CollectionSortBy); | ||
|
||
expect(result1).toEqual([-200, -11, 0, 3, 99999]); | ||
expect(result2).toEqual([99999, 3, 0, -11, -200]); | ||
}); | ||
|
||
it('should return a collection of translation values sorted', () => { | ||
i18n.setLocale('en'); | ||
const roleCollection = ['SALES_REP', 'DEVELOPER', 'SALES_REP', null, 'HUMAN_RESOURCES', 'FINANCE_MANAGER', 'UNKNOWN']; | ||
const columnDef = { id: 'count', field: 'count', fieldType: FieldType.string } as Column; | ||
|
||
const result1 = service.sortCollection(columnDef, [...roleCollection], { property: '', sortDesc: false } as CollectionSortBy, true); | ||
const result2 = service.sortCollection(columnDef, [...roleCollection], { property: '', sortDesc: true } as CollectionSortBy, true); | ||
|
||
expect(result1).toEqual([null, 'DEVELOPER', 'FINANCE_MANAGER', 'HUMAN_RESOURCES', 'SALES_REP', 'SALES_REP', 'UNKNOWN']); | ||
expect(result2).toEqual(['UNKNOWN', 'SALES_REP', 'SALES_REP', 'HUMAN_RESOURCES', 'FINANCE_MANAGER', 'DEVELOPER', null]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.