89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
|
|
// We test the timer composable logic directly since it's a pure Vue composable
|
|
// Extract the logic to test it in isolation
|
|
|
|
describe('useTimer', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
// Test the core timer logic that the composable uses
|
|
it('formatTime converts seconds to HH:MM:SS', () => {
|
|
const formatTime = (totalSeconds: number): string => {
|
|
const hours = Math.floor(totalSeconds / 3600)
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
|
const seconds = totalSeconds % 60
|
|
return [hours, minutes, seconds].map(v => String(v).padStart(2, '0')).join(':')
|
|
}
|
|
|
|
expect(formatTime(0)).toBe('00:00:00')
|
|
expect(formatTime(61)).toBe('00:01:01')
|
|
expect(formatTime(3661)).toBe('01:01:01')
|
|
expect(formatTime(59)).toBe('00:00:59')
|
|
expect(formatTime(3600)).toBe('01:00:00')
|
|
})
|
|
|
|
it('formatTime handles large values', () => {
|
|
const formatTime = (totalSeconds: number): string => {
|
|
const hours = Math.floor(totalSeconds / 3600)
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
|
const seconds = totalSeconds % 60
|
|
return [hours, minutes, seconds].map(v => String(v).padStart(2, '0')).join(':')
|
|
}
|
|
|
|
expect(formatTime(86399)).toBe('23:59:59')
|
|
expect(formatTime(100000)).toBe('27:46:40')
|
|
})
|
|
|
|
it('timer increments elapsedSeconds over time', () => {
|
|
let elapsed = 0
|
|
const interval = setInterval(() => {
|
|
elapsed++
|
|
}, 1000)
|
|
|
|
vi.advanceTimersByTime(3000)
|
|
expect(elapsed).toBe(3)
|
|
|
|
vi.advanceTimersByTime(2000)
|
|
expect(elapsed).toBe(5)
|
|
|
|
clearInterval(interval)
|
|
})
|
|
|
|
it('timer can be paused and resumed', () => {
|
|
let elapsed = 0
|
|
let intervalId: ReturnType<typeof setInterval> | null = null
|
|
|
|
const start = () => {
|
|
if (!intervalId) {
|
|
intervalId = setInterval(() => { elapsed++ }, 1000)
|
|
}
|
|
}
|
|
const pause = () => {
|
|
if (intervalId) {
|
|
clearInterval(intervalId)
|
|
intervalId = null
|
|
}
|
|
}
|
|
|
|
start()
|
|
vi.advanceTimersByTime(2000)
|
|
expect(elapsed).toBe(2)
|
|
|
|
pause()
|
|
vi.advanceTimersByTime(3000)
|
|
expect(elapsed).toBe(2) // Should not change while paused
|
|
|
|
start()
|
|
vi.advanceTimersByTime(2000)
|
|
expect(elapsed).toBe(4)
|
|
|
|
pause()
|
|
})
|
|
})
|