1 | import { addToRange } from "./addToRange";
|
2 |
|
3 | describe("addToRange", () => {
|
4 | test("add a date to an undefined range", () => {
|
5 | const date = new Date(2022, 0, 1);
|
6 | const range = addToRange(date, undefined);
|
7 | expect(range).toEqual({ from: date, to: date });
|
8 | });
|
9 |
|
10 | test("add a date to an empty range", () => {
|
11 | const date = new Date(2022, 0, 1);
|
12 | const range = addToRange(date, { from: undefined, to: undefined });
|
13 | expect(range).toEqual({ from: date, to: date });
|
14 | });
|
15 |
|
16 | test("add a date to an incomplete range with same start date", () => {
|
17 | const date = new Date(2022, 0, 1);
|
18 | const range = addToRange(date, { from: date, to: undefined });
|
19 | expect(range).toEqual(undefined);
|
20 | });
|
21 |
|
22 | test("add a date to an incomplete range with earlier date", () => {
|
23 | const from = new Date(2022, 0, 1);
|
24 | const earlierDate = new Date(2021, 11, 31);
|
25 | const range = addToRange(earlierDate, { from: from, to: undefined });
|
26 | expect(range).toEqual({ from: earlierDate, to: from });
|
27 | });
|
28 |
|
29 | test("add a date to an incomplete range with later date", () => {
|
30 | const from = new Date(2022, 0, 1);
|
31 | const date = new Date(2022, 0, 2);
|
32 | const range = addToRange(date, { from: from, to: undefined });
|
33 | expect(range).toEqual({ from: from, to: date });
|
34 | });
|
35 |
|
36 | test("add a date to a complete range with same start and end date", () => {
|
37 | const date = new Date(2022, 0, 1);
|
38 | const from = date;
|
39 | const to = date;
|
40 | const range = addToRange(date, { from, to }, 0, 0, false);
|
41 | expect(range).toEqual(undefined);
|
42 | });
|
43 |
|
44 | test("add a date to a complete range with same start date", () => {
|
45 | const date = new Date(2022, 0, 1);
|
46 | const to = new Date(2022, 0, 2);
|
47 | const range = addToRange(date, { from: date, to: to });
|
48 | expect(range).toEqual({ from: date, to: date });
|
49 | });
|
50 |
|
51 | test("add a date to a complete range with same end date", () => {
|
52 | const date = new Date(2022, 0, 2);
|
53 | const from = new Date(2022, 0, 1);
|
54 | const range = addToRange(date, { from: from, to: date });
|
55 | expect(range).toEqual({ from: date, to: date });
|
56 | });
|
57 |
|
58 | test("add a date when inside the range", () => {
|
59 | const date = new Date(2022, 0, 1);
|
60 | const from = new Date(2021, 11, 31);
|
61 | const to = new Date(2022, 0, 2);
|
62 | const range = addToRange(date, { from, to });
|
63 | expect(range).toEqual({ from, to: date });
|
64 | });
|
65 |
|
66 | test("add an earlier date to a complete range", () => {
|
67 | const from = new Date(2022, 0, 1);
|
68 | const to = new Date(2022, 0, 2);
|
69 | const date = new Date(2021, 11, 31);
|
70 | const range = addToRange(date, { from, to });
|
71 | expect(range).toEqual({ from: date, to: to });
|
72 | });
|
73 |
|
74 | test("add a later date to a complete range", () => {
|
75 | const date = new Date(2022, 0, 2);
|
76 | const from = new Date(2021, 11, 31);
|
77 | const to = new Date(2022, 0, 1);
|
78 | const range = addToRange(date, { from, to });
|
79 | expect(range).toEqual({ from: from, to: date });
|
80 | });
|
81 |
|
82 | test("add a date with min > 0", () => {
|
83 | const date = new Date(2022, 0, 1);
|
84 | const range = addToRange(date, undefined, 1, 0, false);
|
85 | expect(range).toEqual({ from: date, to: undefined });
|
86 | });
|
87 |
|
88 | test("add a date with max > 0", () => {
|
89 | const date = new Date(2022, 0, 1);
|
90 | const range = addToRange(date, undefined, 0, 1, false);
|
91 | expect(range).toEqual({ from: date, to: date });
|
92 | });
|
93 |
|
94 | test("add a date with required set to true", () => {
|
95 | const date = new Date(2022, 0, 1);
|
96 | const range = addToRange(date, undefined, 0, 0, true);
|
97 | expect(range).toEqual({ from: date, to: date });
|
98 | });
|
99 |
|
100 | test("when exceeding max, set the start of the range", () => {
|
101 | const from = new Date(2022, 0, 1);
|
102 | const to = new Date(2022, 0, 2);
|
103 | const date = new Date(2022, 0, 4);
|
104 | const max = 2;
|
105 | const range = addToRange(date, { from, to }, 0, max, false);
|
106 | expect(range).toEqual({ from: date, to: undefined });
|
107 | });
|
108 |
|
109 | test("when below min, set the start of the range", () => {
|
110 | const from = new Date(2021, 11, 20);
|
111 | const to = new Date(2022, 0, 2);
|
112 | const date = new Date(2021, 11, 21);
|
113 | const min = 5;
|
114 | const range = addToRange(date, { from, to }, min, 0, false);
|
115 | expect(range).toEqual({ from: date, to: undefined });
|
116 | });
|
117 | });
|