addToRange.test.ts
3.31 KB
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
import { addDays, subDays } from 'date-fns';
import { DateRange } from 'types/Matchers';
import { addToRange } from './addToRange';
describe('when no "from" is the range', () => {
const range = { from: undefined };
const day = new Date();
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set "from" as the given day', () => {
expect(result).toEqual({ from: day, to: undefined });
});
});
describe('when no "to" is the range', () => {
const day = new Date();
const range = { from: day, to: undefined };
describe('and the day is the same as the "from" day', () => {
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should return it in the range', () => {
expect(result).toEqual({ from: day, to: day });
});
});
describe('and the day is before "from" day', () => {
const day = subDays(range.from, 1);
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set the day as the "from" range', () => {
expect(result).toEqual({ from: day, to: range.from });
});
});
describe('and the day is after the "from" day', () => {
const day = addDays(range.from, 1);
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set the day as the "to" date', () => {
expect(result).toEqual({ from: range.from, to: day });
});
});
});
describe('when "from", "to" and "day" are the same', () => {
const day = new Date();
const range = { from: day, to: day };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should return an undefined range (reset)', () => {
expect(result).toBeUndefined();
});
});
describe('when "to" and "day" are the same', () => {
const from = new Date();
const to = addDays(from, 4);
const day = to;
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set "to" to undefined', () => {
expect(result).toEqual({ from: to, to: undefined });
});
});
describe('when "from" and "day" are the same', () => {
const from = new Date();
const to = addDays(from, 4);
const day = from;
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should return an undefined range (reset)', () => {
expect(result).toBeUndefined();
});
});
describe('when "from" is after "day"', () => {
const day = new Date();
const from = addDays(day, 1);
const to = addDays(from, 4);
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set the day as "from"', () => {
expect(result).toEqual({ from: day, to: range.to });
});
});
describe('when "from" is before "day"', () => {
const day = new Date();
const from = subDays(day, 1);
const to = addDays(from, 4);
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set the day as "to"', () => {
expect(result).toEqual({ from: range.from, to: day });
});
});