From bbe663fde959d28524e06b1176468d7d0a38da55 Mon Sep 17 00:00:00 2001 From: Florent Odier Date: Tue, 8 Nov 2022 20:12:22 +0100 Subject: [PATCH] Add tests for Interval.toLocaleString() --- test/interval/format.test.js | 160 +++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/test/interval/format.test.js b/test/interval/format.test.js index ca4658a49..c983823b8 100644 --- a/test/interval/format.test.js +++ b/test/interval/format.test.js @@ -16,6 +16,166 @@ test("Interval#toString returns a simple range format", () => test("Interval#toString returns an unfriendly string for invalid intervals", () => expect(invalid.toString()).toBe("Invalid Interval")); +//------ +// .toLocaleString() +//------ + +test("Interval#toLocaleString defaults to the DATE_SHORT format", () => + expect(interval.toLocaleString()).toBe("5/25/1982 – 10/14/1983")); + +test("Interval#toLocaleString returns an unfriendly string for invalid intervals", () => + expect(invalid.toLocaleString()).toBe("Invalid Interval")); + +test("Interval#toLocaleString lets the locale set the numbering system", () => { + expect( + Interval.after(interval.start.reconfigure({ locale: "ja-JP" }), { hour: 2 }).toLocaleString({ + hour: "numeric", + }) + ).toBe("9時~11時"); +}); + +test("Interval#toLocaleString accepts locale settings from the start DateTime", () => { + expect( + Interval.fromDateTimes( + interval.start.reconfigure({ locale: "be" }), + interval.end + ).toLocaleString() + ).toBe("25.5.1982 – 14.10.1983"); +}); + +test("Interval#toLocaleString accepts numbering system settings from the start DateTime", () => { + expect( + Interval.fromDateTimes( + interval.start.reconfigure({ numberingSystem: "beng" }), + interval.end + ).toLocaleString() + ).toBe("৫/২৫/১৯৮২ – ১০/১৪/১৯৮৩"); +}); + +test("Interval#toLocaleString accepts ouptput calendar settings from the start DateTime", () => { + expect( + Interval.fromDateTimes( + interval.start.reconfigure({ outputCalendar: "islamic" }), + interval.end + ).toLocaleString() + ).toBe("8/2/1402 – 1/8/1404 AH"); +}); + +test("Interval#toLocaleString accepts options to the formatter", () => { + expect(interval.toLocaleString({ weekday: "short" })).toBe("Tue – Fri"); +}); + +test("Interval#toLocaleString can override the start DateTime's locale", () => { + expect( + Interval.fromDateTimes( + interval.start.reconfigure({ locale: "be" }), + interval.end + ).toLocaleString({}, { locale: "fr" }) + ).toBe("25/05/1982 – 14/10/1983"); +}); + +test("Interval#toLocaleString can override the start DateTime's numbering system", () => { + expect( + Interval.fromDateTimes( + interval.start.reconfigure({ numberingSystem: "beng" }), + interval.end + ).toLocaleString({ numberingSystem: "mong" }) + ).toBe("᠕/᠒᠕/᠑᠙᠘᠒ – ᠑᠐/᠑᠔/᠑᠙᠘᠓"); +}); + +test("Interval#toLocaleString can override the start DateTime's output calendar", () => { + expect( + Interval.fromDateTimes( + interval.start.reconfigure({ outputCalendar: "islamic" }), + interval.end + ).toLocaleString({}, { outputCalendar: "coptic" }) + ).toBe("9/17/1698 – 2/3/1700 ERA1"); +}); + +test("Interval#toLocaleString shows things in the right IANA zone", () => { + expect( + Interval.fromDateTimes( + interval.start.setZone("Australia/Melbourne"), + interval.end + ).toLocaleString(DateTime.DATETIME_SHORT) + ).toBe("5/25/1982, 7:00 PM – 10/14/1983, 11:30 PM"); +}); + +test("Interval#toLocaleString shows things in the right fixed-offset zone", () => { + expect( + Interval.fromDateTimes(interval.start.setZone("UTC-8"), interval.end).toLocaleString( + DateTime.DATETIME_SHORT + ) + ).toBe("5/25/1982, 1:00 AM – 10/14/1983, 5:30 AM"); +}); + +test("Interval#toLocaleString shows things in the right fixed-offset zone when showing the zone", () => { + expect( + Interval.fromDateTimes(interval.start.setZone("UTC-8"), interval.end).toLocaleString( + DateTime.DATETIME_FULL + ) + ).toBe("May 25, 1982 at 1:00 AM GMT-8 – October 14, 1983 at 5:30 AM GMT-8"); +}); + +test("Interval#toLocaleString shows things with UTC if fixed-offset with 0 offset is used", () => { + expect( + Interval.fromDateTimes(interval.start.setZone("UTC"), interval.end).toLocaleString( + DateTime.DATETIME_FULL + ) + ).toBe("May 25, 1982 at 9:00 AM UTC – October 14, 1983 at 1:30 PM UTC"); +}); + +test("Interval#toLocaleString does the best it can with unsupported fixed-offset zone when showing the zone", () => { + expect( + Interval.fromDateTimes(interval.start.setZone("UTC+4:30"), interval.end).toLocaleString( + DateTime.DATETIME_FULL + ) + ).toBe("May 25, 1982 at 9:00 AM UTC – October 14, 1983 at 1:30 PM UTC"); +}); + +test("Interval#toLocaleString uses locale-appropriate time formats", () => { + expect( + Interval.after(interval.start.reconfigure({ locale: "en-US" }), { hour: 2 }).toLocaleString( + DateTime.TIME_SIMPLE + ) + ).toBe("9:00 – 11:00 AM"); + expect( + Interval.after(interval.start.reconfigure({ locale: "en-US" }), { hour: 2 }).toLocaleString( + DateTime.TIME_24_SIMPLE + ) + ).toBe("09:00 – 11:00"); + + // France has 24-hour by default + expect( + Interval.after(interval.start.reconfigure({ locale: "fr" }), { hour: 2 }).toLocaleString( + DateTime.TIME_SIMPLE + ) + ).toBe("09:00 – 11:00"); + expect( + Interval.after(interval.start.reconfigure({ locale: "fr" }), { hour: 2 }).toLocaleString( + DateTime.TIME_24_SIMPLE + ) + ).toBe("09:00 – 11:00"); + + // Spain does't prefix with "0" and doesn't use spaces + expect( + Interval.after(interval.start.reconfigure({ locale: "es" }), { hour: 2 }).toLocaleString( + DateTime.TIME_SIMPLE + ) + ).toBe("9:00–11:00"); + expect( + Interval.after(interval.start.reconfigure({ locale: "es" }), { hour: 2 }).toLocaleString( + DateTime.TIME_24_SIMPLE + ) + ).toBe("9:00–11:00"); +}); + +test("Interval#toLocaleString sets the separator between days for same-month dates", () => { + expect(Interval.after(interval.start, { day: 2 }).toLocaleString(DateTime.DATE_MED)).toBe( + "May 25 – 27, 1982" + ); +}); + //------ // .toISO() //------