Files
upstream-rocketchat/apps/meteor/client/lib/utils/dateFormat.spec.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

173 lines
6.6 KiB
TypeScript

/**
* @jest-environment <rootDir>/tests/environments/timezone.ts
* @jest-environment-options {"timezone": "America/Argentina/Buenos_Aires"}
*/
import { formatDate, momentFormatToDateFns, toDate } from './dateFormat';
describe('momentFormatToDateFns', () => {
it('maps locale tokens', () => {
expect(momentFormatToDateFns('L')).toBe('P');
expect(momentFormatToDateFns('LT')).toBe('p');
expect(momentFormatToDateFns('LTS')).toBe('pp');
expect(momentFormatToDateFns('LL')).toBe('PPP');
expect(momentFormatToDateFns('LLL')).toBe('PPP p');
expect(momentFormatToDateFns('LLLL')).toBe('EEEE, PPP p');
});
it('maps common tokens', () => {
expect(momentFormatToDateFns('YYYY-MM-DD HH:mm:ss')).toBe('yyyy-MM-dd HH:mm:ss');
expect(momentFormatToDateFns('MMMM Do YYYY, h:mm:ss a')).toBe('MMMM do yyyy, h:mm:ss aaa');
});
it('preserves AM/PM casing (Moment A = uppercase, a = lowercase)', () => {
// date-fns `a` is always uppercase; `aaa` is lowercase. So Moment `a` → `aaa`.
expect(momentFormatToDateFns('A')).toBe('a');
expect(momentFormatToDateFns('a')).toBe('aaa');
});
it('translates moment [literal] escape to date-fns single-quoted literal', () => {
expect(momentFormatToDateFns('[Today at] LT')).toBe("'Today at' p");
expect(momentFormatToDateFns('[Session started at] HH:mm [on] LL')).toBe("'Session started at' HH:mm 'on' PPP");
});
it("escapes embedded single quotes inside literals as ''", () => {
expect(momentFormatToDateFns("[it's] LT")).toBe("'it''s' p");
});
it('drops empty literal blocks since date-fns has no empty-string syntax', () => {
// In date-fns, '' represents a literal apostrophe, not an empty string.
expect(momentFormatToDateFns('[] LT')).toBe(' p');
});
it('quotes letters that are not Moment tokens (T in ISO 8601 separator)', () => {
// In Moment, T is a literal; in date-fns T = ms timestamp. Must quote.
expect(momentFormatToDateFns('YYYY-MM-DDTHH:mm:ss')).toBe("yyyy-MM-dd'T'HH:mm:ss");
});
it('maps day-of-year tokens (DDD, DDDo, DDDD)', () => {
expect(momentFormatToDateFns('DDD')).toBe('D');
expect(momentFormatToDateFns('DDDo')).toBe('Do');
expect(momentFormatToDateFns('DDDD')).toBe('DDD');
});
it('maps day-of-week ordinal (do)', () => {
expect(momentFormatToDateFns('do')).toBe('io');
});
it('maps the 6-digit padded year (YYYYYY)', () => {
expect(momentFormatToDateFns('YYYYYY')).toBe('yyyyyy');
});
it('maps timezone abbreviation tokens (z, zz)', () => {
expect(momentFormatToDateFns('z')).toBe('zzz');
expect(momentFormatToDateFns('zz')).toBe('zzz');
});
it('maps extended fractional-second tokens (SSSS…SSSSSSSSS)', () => {
expect(momentFormatToDateFns('SSSS')).toBe('SSSS');
expect(momentFormatToDateFns('SSSSSSSSS')).toBe('SSSSSSSSS');
});
it('maps era year (y) and era name (N…NNNNN)', () => {
expect(momentFormatToDateFns('y')).toBe('y');
expect(momentFormatToDateFns('N')).toBe('G');
expect(momentFormatToDateFns('NN')).toBe('GG');
expect(momentFormatToDateFns('NNN')).toBe('GGG');
expect(momentFormatToDateFns('NNNN')).toBe('GGGG');
expect(momentFormatToDateFns('NNNNN')).toBe('GGGGG');
});
it('maps lowercase locale variants (l, ll, lll, llll)', () => {
expect(momentFormatToDateFns('l')).toBe('P');
expect(momentFormatToDateFns('ll')).toBe('PP');
expect(momentFormatToDateFns('lll')).toBe('PP p');
expect(momentFormatToDateFns('llll')).toBe('EEE, PP p');
});
it('preserves tokens that are identical between Moment and date-fns (kk, Q, ww)', () => {
// Regression: these used to pass through unchanged; tokenizer now must list them
// explicitly so they aren't quoted as literals.
expect(momentFormatToDateFns('kk:mm')).toBe('kk:mm');
expect(momentFormatToDateFns('k:mm')).toBe('k:mm');
expect(momentFormatToDateFns('Q')).toBe('Q');
expect(momentFormatToDateFns('ww')).toBe('ww');
});
it('maps ISO week-of-year tokens (W, WW, Wo)', () => {
expect(momentFormatToDateFns('W')).toBe('I');
expect(momentFormatToDateFns('WW')).toBe('II');
expect(momentFormatToDateFns('Wo')).toBe('Io');
});
it('maps week-year tokens (gg/gggg locale, GG/GGGG ISO)', () => {
expect(momentFormatToDateFns('gg')).toBe('YY');
expect(momentFormatToDateFns('gggg')).toBe('YYYY');
expect(momentFormatToDateFns('GG')).toBe('RR');
expect(momentFormatToDateFns('GGGG')).toBe('RRRR');
});
it('maps Moment timezone offset tokens to date-fns equivalents', () => {
expect(momentFormatToDateFns('Z')).toBe('xxx');
expect(momentFormatToDateFns('ZZ')).toBe('xx');
expect(momentFormatToDateFns('Z ZZ')).toBe('xxx xx');
expect(momentFormatToDateFns('LT Z')).toBe('p xxx');
expect(momentFormatToDateFns('YYYY-MM-DDTHH:mm:ssZ')).toBe("yyyy-MM-dd'T'HH:mm:ssxxx");
});
});
describe('formatDate', () => {
const sample = new Date('2026-04-24T20:30:45');
it('formats literal blocks with locale tokens without throwing', () => {
expect(() => formatDate(sample, '[Today at] LT')).not.toThrow();
expect(formatDate(sample, '[Today at] LT')).toMatch(/^Today at /);
});
it('keeps the ISO 8601 T as a literal instead of inserting a ms timestamp', () => {
expect(formatDate(sample, 'YYYY-MM-DDTHH:mm:ss')).toBe('2026-04-24T20:30:45');
});
it('does not throw on Moment timezone tokens', () => {
expect(() => formatDate(sample, 'LT Z')).not.toThrow();
expect(() => formatDate(sample, 'Z ZZ')).not.toThrow();
expect(() => formatDate(sample, 'YYYY-MM-DDTHH:mm:ssZ')).not.toThrow();
});
it('falls back instead of crashing on a malformed format', () => {
// Unterminated bracket — translator buffers but date-fns may still refuse.
expect(() => formatDate(sample, '[unterminated')).not.toThrow();
});
it('formats week-year tokens without throwing on date-fns Y warning', () => {
// date-fns refuses Y/YY/YYYY without useAdditionalWeekYearTokens — verify the
// option is wired through.
expect(() => formatDate(sample, 'gggg')).not.toThrow();
expect(formatDate(sample, 'gggg')).toMatch(/^\d{4}$/);
});
});
describe('toDate', () => {
it('parses a bare yyyy-MM-dd string at LOCAL midnight (not UTC midnight)', () => {
const d = toDate('2026-07-02');
// Pre-fix `new Date('2026-07-02')` (UTC midnight) is the previous day / 21:00 here.
expect(d.getFullYear()).toBe(2026);
expect(d.getMonth()).toBe(6); // July (0-indexed)
expect(d.getDate()).toBe(2);
expect(d.getHours()).toBe(0);
});
it('keeps full ISO datetimes as instants (UTC preserved)', () => {
expect(toDate('2026-07-02T15:30:00.000Z').toISOString()).toBe('2026-07-02T15:30:00.000Z');
});
it('returns Date objects unchanged', () => {
const d = new Date();
expect(toDate(d)).toBe(d);
});
it('accepts epoch milliseconds', () => {
const ms = Date.UTC(2026, 6, 2, 12);
expect(toDate(ms).getTime()).toBe(ms);
});
});