PlainDate.from.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.PlainDate.from).toHaveLength(1);
  4. });
  5. test("PlainDate instance argument", () => {
  6. const plainDate = new Temporal.PlainDate(2021, 7, 26);
  7. const createdPlainDate = Temporal.PlainDate.from(plainDate);
  8. expect(createdPlainDate.year).toBe(2021);
  9. expect(createdPlainDate.month).toBe(7);
  10. expect(createdPlainDate.day).toBe(26);
  11. });
  12. test("PlainDate string argument", () => {
  13. const createdPlainDate = Temporal.PlainDate.from("2021-07-26");
  14. expect(createdPlainDate.year).toBe(2021);
  15. expect(createdPlainDate.month).toBe(7);
  16. expect(createdPlainDate.day).toBe(26);
  17. });
  18. });
  19. describe("errors", () => {
  20. test("missing fields", () => {
  21. expect(() => {
  22. Temporal.PlainDate.from({});
  23. }).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
  24. expect(() => {
  25. Temporal.PlainDate.from({ year: 0 });
  26. }).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
  27. expect(() => {
  28. Temporal.PlainDate.from({ month: 1 });
  29. }).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
  30. });
  31. test("invalid date time string", () => {
  32. expect(() => {
  33. Temporal.PlainDate.from("foo");
  34. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  35. });
  36. test("extended year must not be negative zero", () => {
  37. expect(() => {
  38. Temporal.PlainDate.from("-000000-01-01");
  39. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  40. expect(() => {
  41. Temporal.PlainDate.from("−000000-01-01"); // U+2212
  42. }).toThrowWithMessage(RangeError, "Invalid ISO date time");
  43. });
  44. });