sortby/lib/sortby.test.js

58 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2023-09-19 12:10:17 -04:00
import { deepEqual as equal } from 'node:assert/strict'
import { metaltest } from 'metaltest'
2023-09-28 22:56:40 -04:00
const test = metaltest('Sort by')
2023-09-19 12:10:17 -04:00
export { test }
import { sortby } from '../index.js'
const data = [
{ age: 40, name: "Persone 1", new: true },
{ age: 17, name: "Persone 2", new: false },
]
test('sort by name ascending', () => {
2023-09-29 00:15:29 -04:00
const result = data.sort(sortby('name'))
2023-09-19 12:10:17 -04:00
equal(result, [
{ age: 40, name: "Persone 1", new: true },
{ age: 17, name: "Persone 2", new: false },
])
})
test('sort by age ascending', () => {
2023-09-29 00:15:29 -04:00
const result = data.sort(sortby('age'))
2023-09-19 12:10:17 -04:00
equal(result, [
{ age: 17, name: "Persone 2", new: false },
{ age: 40, name: "Persone 1", new: true },
])
})
test('sort by age descending', () => {
2023-09-29 00:15:29 -04:00
const result = data.sort(sortby('age', 'desc'))
2023-09-19 12:10:17 -04:00
equal(result, [
{ age: 40, name: "Persone 1", new: true },
{ age: 17, name: "Persone 2", new: false },
])
})
2023-09-19 14:20:25 -04:00
test('sort by new descending', () => {
2023-09-29 00:15:29 -04:00
const result = data.sort(sortby('new', 'desc'))
2023-09-19 14:20:25 -04:00
equal(result, [
{ age: 40, name: "Persone 1", new: true },
{ age: 17, name: "Persone 2", new: false },
])
})
2023-09-28 23:12:49 -04:00
test('sort with accent, should not change order', () => {
const data = [{ name: 'è' }, { name: 'é' }, { name: 'e' }]
2023-09-29 00:15:29 -04:00
const result = data.sort(sortby('name'))
2023-09-28 23:12:49 -04:00
equal(result, [{ name: 'è' }, { name: 'é' }, { name: 'e' }])
})
2023-09-19 14:20:25 -04:00
2023-09-19 12:10:17 -04:00
import { runifmain, summaryreporter, errorreporter } from 'metaltest'
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))