Add unit tests for sorting functions

This commit is contained in:
Jos de Jong 2020-08-23 17:30:05 +02:00
parent aeb3374406
commit ec581ad674
2 changed files with 42 additions and 9 deletions

View File

@ -1,14 +1,14 @@
import { getIn } from '../utils/immutabilityHelpers.js' import { getIn } from '../utils/immutabilityHelpers.js'
import naturalSort from 'javascript-natural-sort'
/** /**
* Sort the items of an array * Sort the items of an array
* @param {Array} array The array to be sorted * @param {Array} array The array to be sorted
* @param {Path} path Nested path to the property on which to sort the contents * @param {Path} [path=[]] Nested path to the property on which to sort the contents
* @param {1 | -1} direction Pass 1 to sort ascending, -1 to sort descending * @param {1 | -1} [direction=1] Pass 1 to sort ascending, -1 to sort descending
* @return {Array} Returns a sorted shallow copy of the array * @return {Array} Returns a sorted shallow copy of the array
*/ */
// TODO: write unit tests for sortArray export function sortArray (array, path = [], direction = 1) {
export function sortArray (array, path, direction) {
function comparator (a, b) { function comparator (a, b) {
const valueA = getIn(a, path) const valueA = getIn(a, path)
const valueB = getIn(b, path) const valueB = getIn(b, path)
@ -41,12 +41,11 @@ export function sortArray (array, path, direction) {
/** /**
* Sort the keys of an object * Sort the keys of an object
* @param {Object} object The object to be sorted * @param {Object} object The object to be sorted
* @param {1 | -1} direction Pass 1 to sort ascending, -1 to sort descending * @param {1 | -1} [direction=1] Pass 1 to sort ascending, -1 to sort descending
* @return {Object} Returns a sorted shallow copy of the object * @return {Object} Returns a sorted shallow copy of the object
*/ */
// TODO: write unit tests for sortObjectKeys export function sortObjectKeys (object, direction = 1) {
export function sortObjectKeys (object, direction) {
const keys = Object.keys(object) const keys = Object.keys(object)
keys.sort((keyA, keyB) => { keys.sort((keyA, keyB) => {
return direction * naturalSort(keyA, keyB) return direction * naturalSort(keyA, keyB)

34
src/logic/sort.test.js Normal file
View File

@ -0,0 +1,34 @@
import assert from 'assert'
import { sortArray, sortObjectKeys } from './sort.js'
describe('sort', () => {
it('should sort array', () => {
assert.deepStrictEqual(sortArray([ 2, 3, 1 ]), [1, 2, 3])
assert.deepStrictEqual(sortArray([ 2, 3, 1 ], undefined, -1), [3, 2, 1])
})
it('should sort array using natural sort', () => {
assert.deepStrictEqual(sortArray([ '10', '2', '1' ]), ['1', '2', '10'])
})
it('should sort array by nested properties', () => {
const a = {data: { value: 1 }}
const b = {data: { value: 2 }}
const c = {data: { value: 3 }}
assert.deepStrictEqual(sortArray([ b, c, a ], ['data', 'value']), [a, b, c])
assert.deepStrictEqual(sortArray([ b, a, c ], ['data', 'value']), [a, b, c])
assert.deepStrictEqual(sortArray([ b, a, c ], ['data', 'value'], 1), [a, b, c])
assert.deepStrictEqual(sortArray([ b, a, c ], ['data', 'value'], -1), [c, b, a])
})
it('should sort object keys', () => {
const object = { b: 1, c: 1, a: 1 }
assert.deepStrictEqual(Object.keys(sortObjectKeys(object)), ['a', 'b', 'c'])
assert.deepStrictEqual(Object.keys(sortObjectKeys(object, 1)), ['a', 'b', 'c'])
assert.deepStrictEqual(Object.keys(sortObjectKeys(object, -1)), ['c', 'b', 'a'])
})
})