|
| 1 | +import { TZDate } from "@date-fns/tz"; |
| 2 | + |
| 3 | +import type { DateRange, Matcher } from "../types/index.js"; |
| 4 | + |
| 5 | +import { convertMatchersToTimeZone } from "./convertMatchersToTimeZone.js"; |
| 6 | + |
| 7 | +const timeZone = "Pacific/Honolulu"; |
| 8 | + |
| 9 | +describe("convertMatchersToTimeZone", () => { |
| 10 | + describe("when argument is a date", () => { |
| 11 | + const date = new Date("2024-09-26T00:00:00.000Z"); |
| 12 | + let convertedDate: Date; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + convertedDate = convertMatchersToTimeZone(date, timeZone) as Date; |
| 16 | + }); |
| 17 | + |
| 18 | + test("returns a TZDate instance", () => { |
| 19 | + expect(convertedDate).toBeInstanceOf(TZDate); |
| 20 | + }); |
| 21 | + |
| 22 | + test("uses the provided time zone offset", () => { |
| 23 | + expect(convertedDate.toISOString()).toEqual( |
| 24 | + new TZDate(date, timeZone).toISOString(), |
| 25 | + ); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe("when argument is a date range", () => { |
| 30 | + const range: DateRange = { |
| 31 | + from: new Date("2024-09-24T00:00:00.000Z"), |
| 32 | + to: new Date("2024-09-26T00:00:00.000Z"), |
| 33 | + }; |
| 34 | + let convertedRange: DateRange; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + convertedRange = convertMatchersToTimeZone(range, timeZone) as DateRange; |
| 38 | + }); |
| 39 | + |
| 40 | + test("converts the from date", () => { |
| 41 | + expect(convertedRange.from).toBeInstanceOf(TZDate); |
| 42 | + }); |
| 43 | + |
| 44 | + test("converts the to date", () => { |
| 45 | + expect(convertedRange.to).toBeInstanceOf(TZDate); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("when argument is a date interval", () => { |
| 50 | + const interval = { |
| 51 | + before: new Date("2024-09-20T00:00:00.000Z"), |
| 52 | + after: new Date("2024-09-10T00:00:00.000Z"), |
| 53 | + }; |
| 54 | + let convertedInterval: typeof interval; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + convertedInterval = convertMatchersToTimeZone( |
| 58 | + interval, |
| 59 | + timeZone, |
| 60 | + ) as typeof interval; |
| 61 | + }); |
| 62 | + |
| 63 | + test("converts the before date", () => { |
| 64 | + expect(convertedInterval.before).toBeInstanceOf(TZDate); |
| 65 | + }); |
| 66 | + |
| 67 | + test("converts the after date", () => { |
| 68 | + expect(convertedInterval.after).toBeInstanceOf(TZDate); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("when argument is a before matcher", () => { |
| 73 | + const before = { before: new Date("2024-08-31T00:00:00.000Z") }; |
| 74 | + let convertedBefore: typeof before; |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + convertedBefore = convertMatchersToTimeZone( |
| 78 | + before, |
| 79 | + timeZone, |
| 80 | + ) as typeof before; |
| 81 | + }); |
| 82 | + |
| 83 | + test("converts the before value", () => { |
| 84 | + expect(convertedBefore.before).toBeInstanceOf(TZDate); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("when argument is an after matcher", () => { |
| 89 | + const after = { after: new Date("2024-10-01T00:00:00.000Z") }; |
| 90 | + let convertedAfter: typeof after; |
| 91 | + |
| 92 | + beforeEach(() => { |
| 93 | + convertedAfter = convertMatchersToTimeZone( |
| 94 | + after, |
| 95 | + timeZone, |
| 96 | + ) as typeof after; |
| 97 | + }); |
| 98 | + |
| 99 | + test("converts the after value", () => { |
| 100 | + expect(convertedAfter.after).toBeInstanceOf(TZDate); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe("when argument is an array of matchers", () => { |
| 105 | + const range: DateRange = { |
| 106 | + from: new Date("2024-09-24T00:00:00.000Z"), |
| 107 | + to: new Date("2024-09-26T00:00:00.000Z"), |
| 108 | + }; |
| 109 | + const after = { after: new Date("2024-10-01T00:00:00.000Z") }; |
| 110 | + let convertedRange: Matcher; |
| 111 | + let convertedAfter: Matcher; |
| 112 | + |
| 113 | + beforeEach(() => { |
| 114 | + [convertedRange, convertedAfter] = convertMatchersToTimeZone( |
| 115 | + [range, after], |
| 116 | + timeZone, |
| 117 | + ) as Matcher[]; |
| 118 | + }); |
| 119 | + |
| 120 | + test("converts each range entry", () => { |
| 121 | + expect((convertedRange as DateRange).from).toBeInstanceOf(TZDate); |
| 122 | + }); |
| 123 | + |
| 124 | + test("converts each after entry", () => { |
| 125 | + expect((convertedAfter as typeof after).after).toBeInstanceOf(TZDate); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe("when argument is undefined", () => { |
| 130 | + test("returns undefined", () => { |
| 131 | + expect(convertMatchersToTimeZone(undefined, timeZone)).toBeUndefined(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe("when argument is a boolean", () => { |
| 136 | + test("returns the same boolean value", () => { |
| 137 | + expect(convertMatchersToTimeZone(true, timeZone)).toBe(true); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("when argument is a matcher function", () => { |
| 142 | + const matcher = (date: Date) => date.getDay() === 0; |
| 143 | + |
| 144 | + test("returns the same function reference", () => { |
| 145 | + expect(convertMatchersToTimeZone(matcher, timeZone)).toBe(matcher); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments