|
| 1 | +import { epoch, iso8601, rfc822, toDate } from "../"; |
| 2 | + |
| 3 | +const toIsoString = "2017-05-22T19:33:14.175Z"; |
| 4 | +const iso8601String = "2017-05-22T19:33:14Z"; |
| 5 | +const rfc822String = "Mon, 22 May 2017 19:33:14 GMT"; |
| 6 | +const epochTs = 1495481594; |
| 7 | + |
| 8 | +describe("iso8601", () => { |
| 9 | + it("should convert date objects to ISO-8601 strings", () => { |
| 10 | + expect(iso8601(new Date(toIsoString))).toBe(iso8601String); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should convert parseable date strings to ISO-8601 strings", () => { |
| 14 | + let date = new Date(toIsoString); |
| 15 | + |
| 16 | + expect(iso8601(date.toUTCString())).toBe(iso8601String); |
| 17 | + expect(iso8601(date.toISOString())).toBe(iso8601String); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should assume numbers are epoch timestamps and convert them to ISO-8601 strings accordingly", () => { |
| 21 | + expect(iso8601(epochTs)).toBe(iso8601String); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("rfc822", () => { |
| 26 | + it("should convert date objects to RFC 822 strings", () => { |
| 27 | + expect(rfc822(new Date(toIsoString))).toBe(rfc822String); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should convert parseable date strings to RFC 822 strings", () => { |
| 31 | + let date = new Date(toIsoString); |
| 32 | + |
| 33 | + expect(rfc822(date.toUTCString())).toBe(rfc822String); |
| 34 | + expect(rfc822(date.toISOString())).toBe(rfc822String); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should assume numbers are epoch timestamps and convert them to RFC 822 strings accordingly", () => { |
| 38 | + expect(rfc822(epochTs)).toBe(rfc822String); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("epoch", () => { |
| 43 | + it("should convert date objects to epoch timestamps", () => { |
| 44 | + expect(epoch(new Date(toIsoString))).toBe(epochTs); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should convert parseable date strings to epoch timestamps", () => { |
| 48 | + let date = new Date(toIsoString); |
| 49 | + |
| 50 | + expect(epoch(date.toUTCString())).toBe(epochTs); |
| 51 | + expect(epoch(date.toISOString())).toBe(epochTs); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should assume numbers are epoch timestamps and convert them to epoch timestamps accordingly", () => { |
| 55 | + expect(epoch(epochTs)).toBe(epochTs); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe("toDate", () => { |
| 60 | + it("should convert epoch timestamps to date objects", () => { |
| 61 | + const date = toDate(epochTs); |
| 62 | + expect(date).toBeInstanceOf(Date); |
| 63 | + expect(date.valueOf()).toBe(epochTs * 1000); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should convert ISO-8601 strings to date objects", () => { |
| 67 | + const date = toDate(iso8601String); |
| 68 | + expect(date).toBeInstanceOf(Date); |
| 69 | + expect(date.valueOf()).toBe(epochTs * 1000); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should convert RFC 822 strings to date objects", () => { |
| 73 | + const date = toDate(rfc822String); |
| 74 | + expect(date).toBeInstanceOf(Date); |
| 75 | + expect(date.valueOf()).toBe(epochTs * 1000); |
| 76 | + }); |
| 77 | +}); |
0 commit comments