Просто вызовите методы с другим набором аргументов (чтобы он охватывал все потоки).И сравните возвращаемые значения.
Пример
Метод
export const sortByField = (field, isRevered = false, primerFn) => {
if (field) {
var key = primerFn ? (x) => primerFn(x[field]) : (x) => x[field];
isRevered = !isRevered ? 1 : -1;
return (a, b) => {
/*eslint-disable */
return a = key(a), b = key(b), isRevered * ((a > b) - (b > a));
/*eslint-enable */
}
}
else {
return (a, b) => {
return isRevered ? a < b : a > b;
}
}
}
Тестирование
describe('testing sortByField', () => {
let arrayOfObject = [];
beforeAll(() => {
arrayOfObject = [
{ name: "Name 1", class: "CLASS 4" },
{ name: "Name 2", class: "Class 3" },
{ name: "Name 3", class: "Class 2" },
{ name: "Name 4", class: "Class 1" },
]
})
it('should sort the json array with specified field in ascending order with class as parameter', () => {
expect(arrayOfObject.sort(sortByField("class"))[0].name).toBe("Name 1");
})
it('should sort the json array with specified field in descending order with class and true as parameters', () => {
expect(arrayOfObject.sort(sortByField("class", true))[0].name).toBe("Name 2");
})
it('should sort the json array with specified field in ascending order with class, false and a primerFunction as parameter', () => {
let tPrimerFn = (x) => x.toLowerCase();
expect(arrayOfObject.sort(sortByField("class", false, tPrimerFn))[0].name).toBe("Name 4");
})
it('should sort the simple array in ascending order without passing any field', () => {
expect([4, 3, 2, 1].sort(sortByField())[0]).toBe(1);
})
it('should sort the simple array in ascending order without passing any field', () => {
expect([4, 3, 2, 1].sort(sortByField(null, true))[0]).toBe(4);
})
})