Add unit tests for getDurationParts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matt Isenhower
2026-02-15 15:40:52 -08:00
parent 7257c26869
commit b0083d08cc
2 changed files with 64 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import { useI18n } from 'vue-i18n';
import { useTimeStore } from '@/stores/time';
function getDurationParts(value) {
export function getDurationParts(value) {
let negative = (value < 0) ? '-' : '';
value = Math.abs(value);

63
src/common/time.test.js Normal file
View File

@@ -0,0 +1,63 @@
import { describe, it, expect } from 'vitest';
import { getDurationParts } from './time.js';
describe('getDurationParts', () => {
it('returns all zeros for zero', () => {
expect(getDurationParts(0)).toEqual({
negative: '',
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
});
});
it('computes 1d 1h 1m 1s for 90061', () => {
expect(getDurationParts(90061)).toEqual({
negative: '',
days: 1,
hours: 1,
minutes: 1,
seconds: 1,
});
});
it('sets negative flag for negative values', () => {
const result = getDurationParts(-90061);
expect(result.negative).toBe('-');
expect(result.days).toBe(1);
expect(result.hours).toBe(1);
expect(result.minutes).toBe(1);
expect(result.seconds).toBe(1);
});
it('computes exactly 1 day for 86400', () => {
expect(getDurationParts(86400)).toEqual({
negative: '',
days: 1,
hours: 0,
minutes: 0,
seconds: 0,
});
});
it('computes only seconds for 59', () => {
expect(getDurationParts(59)).toEqual({
negative: '',
days: 0,
hours: 0,
minutes: 0,
seconds: 59,
});
});
it('computes hours and minutes without days', () => {
expect(getDurationParts(3661)).toEqual({
negative: '',
days: 0,
hours: 1,
minutes: 1,
seconds: 1,
});
});
});