UNPKG

3.28 kBPlain TextView Raw
1import { addDays, subDays } from "date-fns";
2
3import { dateLib } from "../lib";
4import {
5 DateAfter,
6 DateBefore,
7 DateInterval,
8 DateRange,
9 DayOfWeek
10} from "../types";
11
12import { dateMatchModifiers } from "./dateMatchModifiers";
13
14const testDay = new Date();
15
16describe("when the matcher is a boolean", () => {
17 const matcher = true;
18 const result = dateMatchModifiers(testDay, [matcher], dateLib);
19 test("should return the boolean", () => {
20 expect(result).toBe(matcher);
21 });
22});
23describe("when matching the same day", () => {
24 const matcher = testDay;
25 const result = dateMatchModifiers(testDay, [matcher], dateLib);
26 test("should return true", () => {
27 expect(result).toBe(true);
28 });
29});
30
31describe("when matching an array of dates including the day", () => {
32 const matcher = [addDays(testDay, -1), testDay, addDays(testDay, 1)];
33 const result = dateMatchModifiers(testDay, [matcher], dateLib);
34 test("should return true", () => {
35 expect(result).toBe(true);
36 });
37});
38
39describe("when matching date range", () => {
40 const matcher: DateRange = {
41 from: testDay,
42 to: addDays(testDay, 1)
43 };
44 const result = dateMatchModifiers(testDay, [matcher], dateLib);
45 test("should return true", () => {
46 expect(result).toBe(true);
47 });
48});
49
50describe("when matching the day of week", () => {
51 const matcher: DayOfWeek = {
52 dayOfWeek: [testDay.getDay()]
53 };
54 const result = dateMatchModifiers(testDay, [matcher], dateLib);
55 test("should return true", () => {
56 expect(result).toBe(true);
57 });
58});
59
60describe("when matching date interval (closed)", () => {
61 const matcher: DateInterval = {
62 before: addDays(testDay, 5),
63 after: subDays(testDay, 3)
64 };
65 const result = dateMatchModifiers(testDay, [matcher], dateLib);
66 test("should return true for the included day", () => {
67 expect(result).toBe(true);
68 });
69});
70
71describe("when matching date interval (open)", () => {
72 const matcher: DateInterval = {
73 before: subDays(testDay, 4),
74 after: addDays(testDay, 5)
75 };
76 test("should return false", () => {
77 const result = dateMatchModifiers(testDay, [matcher], dateLib);
78 expect(result).toBe(false);
79 });
80 test("should return true for the days before", () => {
81 const result = dateMatchModifiers(subDays(testDay, 8), [matcher], dateLib);
82 expect(result).toBe(true);
83 });
84 test("should return true for the days after", () => {
85 const result = dateMatchModifiers(addDays(testDay, 8), [matcher], dateLib);
86 expect(result).toBe(true);
87 });
88});
89
90describe("when matching the date after", () => {
91 const matcher: DateAfter = { after: addDays(testDay, -1) };
92 const result = dateMatchModifiers(testDay, [matcher], dateLib);
93 test("should return true", () => {
94 expect(result).toBe(true);
95 });
96});
97
98describe("when matching the date before", () => {
99 const matcher: DateBefore = { before: addDays(testDay, +1) };
100 const result = dateMatchModifiers(testDay, [matcher], dateLib);
101 test("should return true", () => {
102 expect(result).toBe(true);
103 });
104});
105
106describe("when the matcher is a function", () => {
107 const matcher = () => true;
108 const result = dateMatchModifiers(testDay, [matcher], dateLib);
109 test("should return the result of the function", () => {
110 expect(result).toBe(true);
111 });
112});