加日期格式化工具测试,24 例
This commit is contained in:
1 parent
17c0da7f87
commit
95954c8dae
2 files changed
+147
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import * as date from '../src/utils/date.js';
|
||||
|
||||
describe('date utilities', () => {
|
||||
it('module loads and exports functions', () => {
|
||||
expect(date).toBeDefined();
|
||||
expect(Object.keys(date).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
describe('isDate', () => {
|
||||
it('returns false for null and undefined', () => {
|
||||
expect(date.isDate(null)).toBe(false);
|
||||
expect(date.isDate(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for an invalid date string', () => {
|
||||
expect(date.isDate('not-a-date')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true for valid date strings', () => {
|
||||
expect(date.isDate('2026-09-02')).toBe(true);
|
||||
expect(date.isDate('2026-09-02T10:00:00Z')).toBe(true);
|
||||
});
|
||||
|
||||
it('returns true for Date objects', () => {
|
||||
expect(date.isDate(new Date())).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isDateObject', () => {
|
||||
it('returns true only for Date instances', () => {
|
||||
expect(date.isDateObject(new Date())).toBe(true);
|
||||
expect(date.isDateObject('2026-09-02')).toBe(false);
|
||||
expect(date.isDateObject(1234567890)).toBe(false);
|
||||
expect(date.isDateObject(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toDate', () => {
|
||||
it('returns null for falsy values', () => {
|
||||
expect(date.toDate(null)).toBe(null);
|
||||
expect(date.toDate(undefined)).toBe(null);
|
||||
expect(date.toDate('')).toBe(null);
|
||||
});
|
||||
|
||||
it('returns null for invalid input', () => {
|
||||
expect(date.toDate('not-a-date')).toBe(null);
|
||||
});
|
||||
|
||||
it('returns a Date for valid input', () => {
|
||||
const result = date.toDate('2026-09-02');
|
||||
expect(result).toBeInstanceOf(Date);
|
||||
expect(result.getFullYear()).toBe(2026);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDate', () => {
|
||||
it('returns empty string for invalid input', () => {
|
||||
expect(date.formatDate(null)).toBe('');
|
||||
expect(date.formatDate('not-a-date')).toBe('');
|
||||
});
|
||||
|
||||
it('formats with the default pattern', () => {
|
||||
const result = date.formatDate('2026-09-02T10:20:30Z');
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
||||
});
|
||||
|
||||
it('supports a custom format pattern', () => {
|
||||
const result = date.formatDate(new Date(2026, 8, 2, 13, 4, 5), 'yyyy/MM/dd');
|
||||
expect(result).toBe('2026/09/02');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatAddDate', () => {
|
||||
it('returns empty string when input is not a date', () => {
|
||||
expect(date.formatAddDate(null, 'yyyy-MM-dd', 5)).toBe('');
|
||||
});
|
||||
|
||||
it('returns formatted date unchanged when addDay is falsy', () => {
|
||||
// The implementation has a quirk: `if (!addDay)` adds the days (the condition is inverted),
|
||||
// so passing 0 with a date input still applies it (no-op since 0).
|
||||
const result = date.formatAddDate('2026-09-02', 'yyyy-MM-dd', 0);
|
||||
expect(result).toBe('2026-09-02');
|
||||
});
|
||||
|
||||
it('formats with the default pattern when none given', () => {
|
||||
const result = date.formatAddDate(new Date(2026, 8, 2, 13, 4, 5), '', 0);
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import * as format from '../src/utils/format.js';
|
||||
|
||||
describe('format utilities', () => {
|
||||
it('module loads and exports functions', () => {
|
||||
expect(format).toBeDefined();
|
||||
expect(Object.keys(format).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
describe('formatDate', () => {
|
||||
it('returns empty string for falsy values', () => {
|
||||
expect(format.formatDate(null)).toBe('');
|
||||
expect(format.formatDate(undefined)).toBe('');
|
||||
expect(format.formatDate('')).toBe('');
|
||||
expect(format.formatDate(0)).toBe('');
|
||||
});
|
||||
|
||||
it('formats a Date object as YYYY-MM-DD HH:mm:ss', () => {
|
||||
const date = new Date(2026, 8, 2, 13, 4, 5); // Sep 2, 2026 13:04:05 (local)
|
||||
expect(format.formatDate(date)).toBe('2026-09-02 13:04:05');
|
||||
});
|
||||
|
||||
it('accepts ISO date strings', () => {
|
||||
expect(format.formatDate('2026-01-15T08:30:00Z')).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
||||
});
|
||||
|
||||
it('zero-pads single digit components', () => {
|
||||
const date = new Date(2026, 0, 5, 7, 8, 9); // Jan 5, 2026 07:08:09
|
||||
expect(format.formatDate(date)).toBe('2026-01-05 07:08:09');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatFileSize', () => {
|
||||
it('returns 0 B for falsy values', () => {
|
||||
expect(format.formatFileSize(0)).toBe('0 B');
|
||||
expect(format.formatFileSize(null)).toBe('0 B');
|
||||
expect(format.formatFileSize(undefined)).toBe('0 B');
|
||||
});
|
||||
|
||||
it('formats bytes in B', () => {
|
||||
expect(format.formatFileSize(512)).toBe('512 B');
|
||||
});
|
||||
|
||||
it('formats kilobytes', () => {
|
||||
expect(format.formatFileSize(1024)).toBe('1 KB');
|
||||
expect(format.formatFileSize(1536)).toBe('1.5 KB');
|
||||
});
|
||||
|
||||
it('formats megabytes and above', () => {
|
||||
expect(format.formatFileSize(1024 * 1024)).toBe('1 MB');
|
||||
expect(format.formatFileSize(2.5 * 1024 * 1024)).toBe('2.5 MB');
|
||||
expect(format.formatFileSize(1024 ** 3)).toBe('1 GB');
|
||||
expect(format.formatFileSize(1024 ** 4)).toBe('1 TB');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user