sortby/lib/sort.test.js

44 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-09-19 12:10:17 -04:00
import { deepEqual as equal } from 'node:assert/strict'
import { metaltest } from 'metaltest'
const test = metaltest('Sort')
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', () => {
const result = data.sort(sortby(['name']))
equal(result, [
{ age: 40, name: "Persone 1", new: true },
{ age: 17, name: "Persone 2", new: false },
])
})
test('sort by age ascending', () => {
const result = data.sort(sortby(['age']))
equal(result, [
{ age: 17, name: "Persone 2", new: false },
{ age: 40, name: "Persone 1", new: true },
])
})
test('sort by age descending', () => {
const result = data.sort(sortby(['age', 'desc']))
equal(result, [
{ age: 40, name: "Persone 1", new: true },
{ age: 17, name: "Persone 2", new: false },
])
})
import { runifmain, summaryreporter, errorreporter } from 'metaltest'
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))