|
| 1 | +// SRC: https://jsdate.wtf/ |
| 2 | +describe('Date', () => { |
| 3 | + let oDate, dateString, dateValue, milliseconds |
| 4 | + |
| 5 | + // 1 of 28 |
| 6 | + describe('new Date("0")', () => { |
| 7 | + beforeEach(() => { |
| 8 | + oDate = new Date('0') |
| 9 | + dateString = oDate.toISOString() |
| 10 | + }) |
| 11 | + |
| 12 | + it('does NOT equal "1970-01-01T00:00:00.000Z"', () => { |
| 13 | + expect(dateString).not.toEqual('1970-01-01T00:00:00.000Z') |
| 14 | + }) |
| 15 | + |
| 16 | + // FIXME? it matches in CI, but not in my local. |
| 17 | + // Is it because I am on GMT-6? |
| 18 | + // The string "0" is interpreted as the year 2000, not as a timestamp! |
| 19 | + xit('interprets "0" as the year 2000, not as a timestamp!', () => { |
| 20 | + expect(dateString).not.toEqual('2000-01-01T00:00:00.000Z') // According to https://jsdate.wtf |
| 21 | + expect(dateString).toEqual('2000-01-01T06:00:00.000Z') |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + // 2 of 28 |
| 26 | + describe('new Date(0)', () => { |
| 27 | + beforeEach(() => { |
| 28 | + oDate = new Date(0) |
| 29 | + dateString = oDate.toISOString() |
| 30 | + }) |
| 31 | + |
| 32 | + it('does NOT equal "2000-01-01T00:00:00.000Z"', () => { |
| 33 | + expect(dateString).not.toEqual('2000-01-01T00:00:00.000Z') |
| 34 | + }) |
| 35 | + |
| 36 | + // The number 0, as opposed to the string "0", |
| 37 | + // is interpreted as milliseconds since the Unix epoch (Jan 1, 1970). |
| 38 | + it('interprets 0 as a timestamp', () => { |
| 39 | + expect(dateString).toEqual('1970-01-01T00:00:00.000Z') |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + // 3 of 28 |
| 44 | + describe('Date.parse(0) === Date.parse("0")', () => { |
| 45 | + // Both parse to 946684800000 milliseconds! |
| 46 | + // Date.parse only operates on strings, |
| 47 | + // so 0 is coerced to the string "0". |
| 48 | + it('is true', () => { |
| 49 | + const firstDate = Date.parse(0) |
| 50 | + const secondDate = Date.parse('0') |
| 51 | + |
| 52 | + expect(firstDate === secondDate).not.toBe(false) |
| 53 | + expect(firstDate === secondDate).toBe(true) |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + // 4 of 28 |
| 58 | + describe('new Date("not a date")', () => { |
| 59 | + beforeEach(() => { |
| 60 | + oDate = new Date('not a date') |
| 61 | + dateValue = oDate.valueOf() |
| 62 | + }) |
| 63 | + |
| 64 | + it('is NOT null', () => { |
| 65 | + expect(oDate).not.toBe(null) |
| 66 | + }) |
| 67 | + |
| 68 | + it('is NOT undefined', () => { |
| 69 | + expect(oDate).not.toBe(undefined) |
| 70 | + }) |
| 71 | + |
| 72 | + // Invalid Date is still a Date object! |
| 73 | + // It's not null or an error. |
| 74 | + it('is Invalid Date', () => { |
| 75 | + expect(Number.isNaN(dateValue)).not.toBe(false) |
| 76 | + expect(Number.isNaN(dateValue)).toBe(true) |
| 77 | + expect(dateValue).toBeNaN() |
| 78 | + }) |
| 79 | + |
| 80 | + // 5 of 28 |
| 81 | + describe('.getTime()', () => { |
| 82 | + beforeEach(() => { |
| 83 | + milliseconds = oDate.getTime() |
| 84 | + }) |
| 85 | + |
| 86 | + it('is NOT null', () => { |
| 87 | + expect(milliseconds).not.toBe(null) |
| 88 | + }) |
| 89 | + |
| 90 | + it('is NOT 0', () => { |
| 91 | + expect(milliseconds).not.toBe(0) |
| 92 | + }) |
| 93 | + |
| 94 | + // Invalid Date objects return NaN for getTime(). |
| 95 | + // This function returns the number of milliseconds since the Unix epoch for valid dates. |
| 96 | + it('is NaN', () => { |
| 97 | + expect(Number.isNaN(milliseconds)).not.toBe(false) |
| 98 | + expect(Number.isNaN(milliseconds)).toBe(true) |
| 99 | + expect(milliseconds).toBeNaN() |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + // 6 of 28 |
| 104 | + describe('.toISOString()', () => { |
| 105 | + // toISOString() throws a RangeError on Invalid Date objects. |
| 106 | + it('throws an error', () => { |
| 107 | + expect(() => oDate.toISOString()).toThrow() |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + // 7 of 28 |
| 112 | + describe('.toTimeString()', () => { |
| 113 | + beforeEach(() => { |
| 114 | + dateString = oDate.toTimeString() |
| 115 | + }) |
| 116 | + |
| 117 | + it('does NOT equal ""', () => { |
| 118 | + expect(dateString).not.toEqual('') |
| 119 | + }) |
| 120 | + |
| 121 | + it('is NOT null', () => { |
| 122 | + expect(dateString).not.toBe(null) |
| 123 | + }) |
| 124 | + |
| 125 | + // toTimeString() returns the string "Invalid Date" for invalid dates. 🫠 |
| 126 | + it('equals "Invalid Date"', () => { |
| 127 | + expect(dateString).toEqual('Invalid Date') |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
| 131 | +}) |
0 commit comments