eletrotupi / tcc/ api/tests/utils/calc.unit.test.ts5dc751d753dcbc4f8d8a9f461b79992e8ba1c9ac
1.3 KB | 52 lines Raw
1
import { describe, it, expect } from 'vitest'
2
import { avg, formatHours, formatDiff } from '@app/utils/calc'
3

4
describe('avg', () => {
5
  it('averages a list of numbers', () => {
6
    expect(avg([2, 4, 6])).toBe(4)
7
  })
8

9
  it('yields NaN for an empty list', () => {
10
    expect(avg([])).toBeNaN()
11
  })
12
})
13

14
describe('formatHours', () => {
15
  it('drops the minutes for a whole hour', () => {
16
    expect(formatHours(7)).toBe('7h')
17
  })
18

19
  it('renders the remaining minutes', () => {
20
    expect(formatHours(7.5)).toBe('7h 30min')
21
  })
22

23
  it('carries a rounding of sixty minutes into the next hour', () => {
24
    expect(formatHours(7.999)).toBe('8h')
25
  })
26
})
27

28
describe('formatDiff', () => {
29
  it('signs a positive whole-hour difference', () => {
30
    expect(formatDiff(120)).toBe('+2h')
31
  })
32

33
  it('signs a negative whole-hour difference', () => {
34
    expect(formatDiff(-120)).toBe('-2h')
35
  })
36

37
  it('signs a positive difference with hours and minutes', () => {
38
    expect(formatDiff(90)).toBe('+1h 30min')
39
  })
40

41
  it('signs a negative difference with hours and minutes', () => {
42
    expect(formatDiff(-90)).toBe('-1h 30min')
43
  })
44

45
  it('renders a positive sub-hour difference in minutes only', () => {
46
    expect(formatDiff(30)).toBe('+30min')
47
  })
48

49
  it('renders a negative sub-hour difference in minutes only', () => {
50
    expect(formatDiff(-30)).toBe('-30min')
51
  })
52
})