| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { describe, test, it, expect } from 'vitest'
- import ArrayUtils from "~/services/utils/arrayUtils";
- import DateUtils from "~/services/utils/dateUtils";
- describe('sort', () => {
- test('with integers', () => {
- expect(ArrayUtils.sort([2, 1, 3])).toEqual([1, 2, 3])
- expect(ArrayUtils.sort([2, 1, 3], true)).toEqual([3, 2, 1])
- })
- test('with string', () => {
- expect(ArrayUtils.sort(['b', 'a', 'c'])).toEqual(['a', 'b', 'c'])
- expect(ArrayUtils.sort(['b', 'a', 'c'], true)).toEqual(['c', 'b', 'a'])
- })
- test('with dates', () => {
- const input = [
- new Date(2023, 0, 12),
- new Date(2023, 0, 10),
- new Date(2023, 0, 11)
- ]
- const expected = [
- new Date(2023, 0, 10),
- new Date(2023, 0, 11),
- new Date(2023, 0, 12)
- ]
- const expectedReverse = [
- new Date(2023, 0, 12),
- new Date(2023, 0, 11),
- new Date(2023, 0, 10)
- ]
- expect(DateUtils.sort(input)).toEqual(expected)
- expect(DateUtils.sort(input, true)).toEqual(expectedReverse)
- })
- })
- describe('sortObjectsByProp', () => {
- test('existing prop', () => {
- const input = [
- {'id': 2, 'name': 'b'},
- {'id': 1, 'name': 'a'},
- {'id': 3, 'name': 'c'},
- ]
- const expected = [
- {'id': 1, 'name': 'a'},
- {'id': 2, 'name': 'b'},
- {'id': 3, 'name': 'c'},
- ]
- expect(ArrayUtils.sortObjectsByProp(input, 'id')).toEqual(expected)
- })
- test('existing prop (reverse)', () => {
- const input = [
- {'id': 2, 'name': 'b'},
- {'id': 1, 'name': 'a'},
- {'id': 3, 'name': 'c'},
- ]
- const expected = [
- {'id': 3, 'name': 'c'},
- {'id': 2, 'name': 'b'},
- {'id': 1, 'name': 'a'},
- ]
- expect(ArrayUtils.sortObjectsByProp(input, 'id', true)).toEqual(expected)
- })
- })
|