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