Skip to content

Commit fb3b1d8

Browse files
committed
test(format): add unit tests for formatDuration
1 parent bccc27b commit fb3b1d8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/utils/format.spec.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22

3-
import { formatDate, formatDistance } from './format'
3+
import { formatDate, formatDistance, formatDuration } from './format'
44

55

66
describe('formatDistance', () => {
@@ -14,6 +14,20 @@ describe('formatDistance', () => {
1414
})
1515

1616

17+
describe('formatDuration', () => {
18+
it('should format duration', () => {
19+
expect(formatDuration(0)).toBe('0m')
20+
expect(formatDuration(12)).toBe('12m')
21+
expect(formatDuration(12.34)).toBe('12m')
22+
expect(formatDuration(90)).toBe('1h 30m')
23+
expect(formatDuration(120)).toBe('2h 0m')
24+
})
25+
it('should be blank for undefined duration', () => {
26+
expect(formatDuration(undefined)).toBe('')
27+
})
28+
})
29+
30+
1731
describe('formatDate', () => {
1832
beforeEach(() => {
1933
vi.useFakeTimers()

src/utils/format.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const formatDistance = (miles: number | undefined): string => {
1717
}
1818

1919
const _formatDuration = (duration: Duration): string => {
20-
if (duration.asHours() > 0) {
20+
if (duration.hours() > 0) {
2121
return duration.format('H[h] m[m]')
2222
} else {
2323
return duration.format('m[m]')
@@ -28,7 +28,7 @@ export const formatDuration = (minutes: number | undefined): string => {
2828
if (minutes === undefined) return ''
2929
const duration = dayjs.duration({
3030
hours: Math.floor(minutes / 60),
31-
minutes: minutes % 60,
31+
minutes: Math.round(minutes % 60),
3232
})
3333
return _formatDuration(duration)
3434
}

0 commit comments

Comments
 (0)