Apps.utils.test.ts 1.7 KB

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