File size: 3,470 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 | import { addDays, subDays } from "date-fns";
import { defaultDateLib } from "../classes/DateLib";
import type {
DateAfter,
DateBefore,
DateInterval,
DateRange,
DayOfWeek,
} from "../types";
import { dateMatchModifiers } from "./dateMatchModifiers";
const testDay = new Date();
describe("when the matcher is a boolean", () => {
const matcher = true;
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return the boolean", () => {
expect(result).toBe(matcher);
});
});
describe("when matching the same day", () => {
const matcher = testDay;
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true", () => {
expect(result).toBe(true);
});
});
describe("when matching an array of dates including the day", () => {
const matcher = [
addDays(testDay, -1),
new Date(testDay),
addDays(testDay, 1),
];
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true", () => {
expect(result).toBe(true);
});
});
describe("when matching date range", () => {
const matcher: DateRange = {
from: testDay,
to: addDays(testDay, 1),
};
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true", () => {
expect(result).toBe(true);
});
});
describe("when matching the day of week", () => {
const matcher: DayOfWeek = {
dayOfWeek: [testDay.getDay()],
};
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true", () => {
expect(result).toBe(true);
});
});
describe("when matching date interval (closed)", () => {
const matcher: DateInterval = {
before: addDays(testDay, 5),
after: subDays(testDay, 3),
};
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true for the included day", () => {
expect(result).toBe(true);
});
});
describe("when matching date interval (open)", () => {
const matcher: DateInterval = {
before: subDays(testDay, 4),
after: addDays(testDay, 5),
};
test("should return false", () => {
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
expect(result).toBe(false);
});
test("should return true for the days before", () => {
const result = dateMatchModifiers(
subDays(testDay, 8),
[matcher],
defaultDateLib,
);
expect(result).toBe(true);
});
test("should return true for the days after", () => {
const result = dateMatchModifiers(
addDays(testDay, 8),
[matcher],
defaultDateLib,
);
expect(result).toBe(true);
});
});
describe("when matching the date after", () => {
const matcher: DateAfter = { after: addDays(testDay, -1) };
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true", () => {
expect(result).toBe(true);
});
});
describe("when matching the date before", () => {
const matcher: DateBefore = { before: addDays(testDay, +1) };
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return true", () => {
expect(result).toBe(true);
});
});
describe("when the matcher is a function", () => {
const matcher = () => true;
const result = dateMatchModifiers(testDay, [matcher], defaultDateLib);
test("should return the result of the function", () => {
expect(result).toBe(true);
});
});
|