@bigfishtv/cockpit
Version:
71 lines (64 loc) • 2.21 kB
JavaScript
// import { ASC, DESC } from '../constants/SortTypes'
import { sortByObjectKey } from './tableUtils.js'
describe('sorts array of objects by an object key', () => {
it('should sort by key where values are lowercase strings', () => {
const input = [{ title: 'anaconda' }, { title: 'cobra' }, { title: 'basilisk' }]
const expected = [{ title: 'anaconda' }, { title: 'basilisk' }, { title: 'cobra' }]
expect(input.sort(sortByObjectKey('title'))).toEqual(expected)
})
it('should sort by key where values are mixed-case strings', () => {
const input = [{ title: 'anaconda' }, { title: 'cobra' }, { title: 'Basilisk' }]
const expected = [{ title: 'anaconda' }, { title: 'Basilisk' }, { title: 'cobra' }]
expect(input.sort(sortByObjectKey('title'))).toEqual(expected)
})
it('should sort by key where values are numbers', () => {
const input = [{ title: 123 }, { title: 1 }, { title: 1.2 }]
const expected = [{ title: 1 }, { title: 1.2 }, { title: 123 }]
expect(input.sort(sortByObjectKey('title'))).toEqual(expected)
})
it('should sort by key where values are booleans', () => {
const input = [{ title: true }, { title: false }, { title: true }]
const expected = [{ title: false }, { title: true }, { title: true }]
expect(input.sort(sortByObjectKey('title'))).toEqual(expected)
})
it('should parseFloat string numbers if it can', () => {
const input = [
{ title: 123 },
{ title: '1.2' },
{ title: '1' },
{ title: 1.2 },
{ title: 1.4 },
{ title: '0122' },
{ title: 1.3 },
]
const expected = [
{ title: '1' },
{ title: '1.2' },
{ title: 1.2 },
{ title: 1.3 },
{ title: 1.4 },
{ title: '0122' },
{ title: 123 },
]
expect(input.sort(sortByObjectKey('title'))).toEqual(expected)
})
it('should not parseFloat string numbers even if all are strings', () => {
const input = [
{ title: '123' },
{ title: '1.2' },
{ title: '1' },
{ title: '1.2' },
{ title: '1.4' },
{ title: '1.3' },
]
const expected = [
{ title: '1' },
{ title: '1.2' },
{ title: '1.2' },
{ title: '1.3' },
{ title: '1.4' },
{ title: '123' },
]
expect(input.sort(sortByObjectKey('title'))).toEqual(expected)
})
})