|
| 1 | +import dayjs from 'dayjs'; |
| 2 | +import timezone from 'dayjs/plugin/timezone'; |
| 3 | +import utc from 'dayjs/plugin/utc'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { |
| 7 | + formatDate, |
| 8 | + formatDateTime, |
| 9 | + getCurrentTimezone, |
| 10 | + getSystemTimezone, |
| 11 | + isDate, |
| 12 | + isDayjsObject, |
| 13 | + setCurrentTimezone, |
| 14 | +} from '../date'; |
| 15 | + |
| 16 | +dayjs.extend(utc); |
| 17 | +dayjs.extend(timezone); |
| 18 | + |
| 19 | +describe('dateUtils', () => { |
| 20 | + const sampleISO = '2024-10-30T12:34:56Z'; |
| 21 | + const sampleTimestamp = Date.parse(sampleISO); |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + // 重置时区 |
| 25 | + dayjs.tz.setDefault(); |
| 26 | + setCurrentTimezone(); // 重置为系统默认 |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.restoreAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + // =============================== |
| 34 | + // formatDate |
| 35 | + // =============================== |
| 36 | + describe('formatDate', () => { |
| 37 | + it('should format a valid ISO date string', () => { |
| 38 | + const formatted = formatDate(sampleISO, 'YYYY/MM/DD'); |
| 39 | + expect(formatted).toMatch(/2024\/10\/30/); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should format a timestamp correctly', () => { |
| 43 | + const formatted = formatDate(sampleTimestamp); |
| 44 | + expect(formatted).toMatch(/2024-10-30/); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should format a Date object', () => { |
| 48 | + const formatted = formatDate(new Date(sampleISO)); |
| 49 | + expect(formatted).toMatch(/2024-10-30/); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should format a dayjs object', () => { |
| 53 | + const formatted = formatDate(dayjs(sampleISO)); |
| 54 | + expect(formatted).toMatch(/2024-10-30/); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return original input if date is invalid', () => { |
| 58 | + const invalid = 'not-a-date'; |
| 59 | + const spy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 60 | + const formatted = formatDate(invalid); |
| 61 | + expect(formatted).toBe(invalid); |
| 62 | + expect(spy).toHaveBeenCalledOnce(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should apply given format', () => { |
| 66 | + const formatted = formatDate(sampleISO, 'YYYY-MM-DD HH:mm'); |
| 67 | + expect(formatted).toMatch(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + // =============================== |
| 72 | + // formatDateTime |
| 73 | + // =============================== |
| 74 | + describe('formatDateTime', () => { |
| 75 | + it('should format date into full datetime', () => { |
| 76 | + const result = formatDateTime(sampleISO); |
| 77 | + expect(result).toMatch(/2024-10-30 \d{2}:\d{2}:\d{2}/); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + // =============================== |
| 82 | + // isDate |
| 83 | + // =============================== |
| 84 | + describe('isDate', () => { |
| 85 | + it('should return true for Date instances', () => { |
| 86 | + expect(isDate(new Date())).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return false for non-Date values', () => { |
| 90 | + expect(isDate('2024-10-30')).toBe(false); |
| 91 | + expect(isDate(null)).toBe(false); |
| 92 | + expect(isDate(undefined)).toBe(false); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + // =============================== |
| 97 | + // isDayjsObject |
| 98 | + // =============================== |
| 99 | + describe('isDayjsObject', () => { |
| 100 | + it('should return true for dayjs objects', () => { |
| 101 | + expect(isDayjsObject(dayjs())).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return false for other values', () => { |
| 105 | + expect(isDayjsObject(new Date())).toBe(false); |
| 106 | + expect(isDayjsObject('string')).toBe(false); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + // =============================== |
| 111 | + // getSystemTimezone |
| 112 | + // =============================== |
| 113 | + describe('getSystemTimezone', () => { |
| 114 | + it('should return a valid IANA timezone string', () => { |
| 115 | + const tz = getSystemTimezone(); |
| 116 | + expect(typeof tz).toBe('string'); |
| 117 | + expect(tz).toMatch(/^[A-Z]+\/[A-Z_]+/i); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + // =============================== |
| 122 | + // setCurrentTimezone / getCurrentTimezone |
| 123 | + // =============================== |
| 124 | + describe('setCurrentTimezone & getCurrentTimezone', () => { |
| 125 | + it('should set and retrieve the current timezone', () => { |
| 126 | + setCurrentTimezone('Asia/Shanghai'); |
| 127 | + expect(getCurrentTimezone()).toBe('Asia/Shanghai'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should reset to system timezone when called with no args', () => { |
| 131 | + const guessed = getSystemTimezone(); |
| 132 | + setCurrentTimezone(); |
| 133 | + expect(getCurrentTimezone()).toBe(guessed); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should update dayjs default timezone', () => { |
| 137 | + setCurrentTimezone('America/New_York'); |
| 138 | + const d = dayjs('2024-01-01T00:00:00Z'); |
| 139 | + // 校验时区转换生效(小时变化) |
| 140 | + expect(d.tz().format('HH')).not.toBe('00'); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments