Apps.utils.test.ts57 lines · main
1import { describe, expect, it, vi } from 'vitest'
2
3import type { PrivateApp } from '../PrivateApps.types'
4import type { AppsSort } from './Apps.types'
5import { handleSortChange, sortApps } from './Apps.utils'
6
7describe('handleSortChange', () => {
8 it('toggles from asc to desc when clicking the same column', () => {
9 const setSort = vi.fn()
10 handleSortChange('created_at:asc', 'created_at', setSort)
11 expect(setSort).toHaveBeenCalledWith('created_at:desc')
12 })
13
14 it('toggles from desc to asc when clicking the same column', () => {
15 const setSort = vi.fn()
16 handleSortChange('created_at:desc', 'created_at', setSort)
17 expect(setSort).toHaveBeenCalledWith('created_at:asc')
18 })
19
20 it('defaults to asc when switching to a different column', () => {
21 const setSort = vi.fn()
22 handleSortChange('created_at:desc', 'name', setSort)
23 expect(setSort).toHaveBeenCalledWith('name:asc')
24 })
25})
26
27const makeApp = (id: string, created_at: string): PrivateApp =>
28 ({
29 id,
30 name: id,
31 created_at,
32 description: '',
33 }) as unknown as PrivateApp
34
35describe('sortApps', () => {
36 const apps = [
37 makeApp('b', '2024-01-02T00:00:00Z'),
38 makeApp('a', '2024-01-01T00:00:00Z'),
39 makeApp('c', '2024-01-03T00:00:00Z'),
40 ]
41
42 it('sorts in ascending order', () => {
43 const sorted = sortApps(apps, 'created_at:asc' as AppsSort)
44 expect(sorted.map((a) => a.id)).toEqual(['a', 'b', 'c'])
45 })
46
47 it('sorts in descending order', () => {
48 const sorted = sortApps(apps, 'created_at:desc' as AppsSort)
49 expect(sorted.map((a) => a.id)).toEqual(['c', 'b', 'a'])
50 })
51
52 it('does not mutate the original array', () => {
53 const original = [...apps]
54 sortApps(apps, 'created_at:asc' as AppsSort)
55 expect(apps).toEqual(original)
56 })
57})