1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 12:35:07 +00:00
serenity/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.from.js
Shannon Booth f95117f75d LibJS: Use TimeZoneMethods in GetOffsetNanosecondsFor
Update to the latest version of the spec which was refactored to use
time zone methods record. This requires updating a whole bunch of
callers to pass through a record too.

This also ends up improving exceptions on a missing
getOffsetNanosecondsFor method.
2024-03-02 12:27:20 +01:00

61 lines
2.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.from).toHaveLength(1);
});
test("PlainDate instance argument", () => {
const plainDate = new Temporal.PlainDate(2021, 7, 26);
const createdPlainDate = Temporal.PlainDate.from(plainDate);
expect(createdPlainDate.year).toBe(2021);
expect(createdPlainDate.month).toBe(7);
expect(createdPlainDate.day).toBe(26);
});
test("PlainDateTime instance argument", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 26, 1, 2, 3);
const createdPlainDate = Temporal.PlainDate.from(plainDateTime);
expect(createdPlainDate.year).toBe(2021);
expect(createdPlainDate.month).toBe(7);
expect(createdPlainDate.day).toBe(26);
});
test("ZonedDateTime instance argument", () => {
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = new Temporal.ZonedDateTime(1627318123456789000n, timeZone);
const createdPlainDate = Temporal.PlainDate.from(zonedDateTime);
expect(createdPlainDate.year).toBe(2021);
expect(createdPlainDate.month).toBe(7);
expect(createdPlainDate.day).toBe(26);
});
test("PlainDate string argument", () => {
const createdPlainDate = Temporal.PlainDate.from("2021-07-26");
expect(createdPlainDate.year).toBe(2021);
expect(createdPlainDate.month).toBe(7);
expect(createdPlainDate.day).toBe(26);
});
});
describe("errors", () => {
test("custom time zone doesn't have a getOffsetNanosecondsFor function", () => {
const zonedDateTime = new Temporal.ZonedDateTime(0n, {});
expect(() => {
Temporal.PlainDate.from(zonedDateTime);
}).toThrowWithMessage(TypeError, "getOffsetNanosecondsFor is undefined");
});
test("invalid date time string", () => {
expect(() => {
Temporal.PlainDate.from("foo");
}).toThrowWithMessage(RangeError, "Invalid date time string 'foo'");
});
test("extended year must not be negative zero", () => {
expect(() => {
Temporal.PlainDate.from("-000000-01-01");
}).toThrowWithMessage(RangeError, "Invalid date time string '-000000-01-01'");
expect(() => {
Temporal.PlainDate.from("000000-01-01"); // U+2212
}).toThrowWithMessage(RangeError, "Invalid date time string '000000-01-01'");
});
});