-
-
Notifications
You must be signed in to change notification settings - Fork 696
/
SelectRangeContext.test.ts
286 lines (266 loc) · 8.21 KB
/
SelectRangeContext.test.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { RenderResult } from '@testing-library/react-hooks';
import {
addDays,
addMonths,
differenceInCalendarDays,
subDays
} from 'date-fns';
import { customRenderHook } from 'test/render/customRenderHook';
import { freezeBeforeAll } from 'test/utils';
import { isMatch } from 'contexts/Modifiers/utils/isMatch';
import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerRangeProps } from 'types/DayPickerRange';
import { ActiveModifiers } from 'types/Modifiers';
import { SelectRangeContextValue, useSelectRange } from './SelectRangeContext';
const today = new Date(2021, 11, 8);
freezeBeforeAll(today);
let result: RenderResult<SelectRangeContextValue>;
function setup(dayPickerProps?: DayPickerBase) {
const view = customRenderHook(useSelectRange, dayPickerProps);
result = view.result;
}
describe('when is not a multiple select DayPicker', () => {
beforeAll(() => {
setup();
});
test('the selected day should be undefined', () => {
expect(result.current.selected).toBeUndefined();
});
});
const initialProps: DayPickerRangeProps = {
mode: 'range',
onDayClick: jest.fn(),
onSelect: jest.fn()
};
const from = today;
const to = addDays(today, 6);
const stubEvent = {} as React.MouseEvent;
describe('when no days are selected', () => {
beforeAll(() => {
setup(initialProps);
});
test('the selected days should be undefined', () => {
expect(result.current.selected).toBeUndefined();
});
describe('when "onDayClick" is called', () => {
const day = from;
const activeModifiers = {};
beforeAll(() => {
result.current.onDayClick?.(day, activeModifiers, stubEvent);
});
afterAll(() => {
jest.resetAllMocks();
});
test('should call the "onDayClick" from the DayPicker props', () => {
expect(initialProps.onDayClick).toHaveBeenCalledWith(
day,
activeModifiers,
stubEvent
);
});
test('should call "onSelect" with the clicked day as the "from" prop', () => {
expect(initialProps.onSelect).toHaveBeenCalledWith(
{ from: day, to: undefined },
day,
activeModifiers,
stubEvent
);
});
});
});
describe('when only the "from" day is selected', () => {
const selected = { from, to: undefined };
const dayPickerProps: DayPickerRangeProps = {
...initialProps,
selected
};
beforeAll(() => {
setup(dayPickerProps);
});
test('should return the "range_start" modifiers with the "from" day', () => {
expect(result.current.modifiers.range_start).toEqual([from]);
});
test('should return the "range_end" modifiers with the "from" day', () => {
expect(result.current.modifiers.range_end).toEqual([from]);
});
test('should not return any "range_middle" modifiers', () => {
expect(result.current.modifiers.range_middle).toEqual([]);
});
});
describe('when a complete range of days is selected', () => {
const selected = { from, to };
const dayPickerProps: DayPickerRangeProps = {
...initialProps,
selected
};
beforeAll(() => {
setup(dayPickerProps);
});
test('should return the "range_start" modifiers with the "from" day', () => {
expect(result.current.modifiers.range_start).toEqual([from]);
});
test('should return the "range_end" modifiers with the "to" day', () => {
expect(result.current.modifiers.range_end).toEqual([to]);
});
test('should return the "range_middle" range modifiers', () => {
expect(result.current.modifiers.range_middle).toEqual([
{ after: from, before: to }
]);
});
describe('when "onDayClick" is called with the day before the from day', () => {
const day = addDays(from, -1);
const activeModifiers = {};
beforeAll(() => {
result.current.onDayClick?.(day, activeModifiers, stubEvent);
});
afterAll(() => {
jest.resetAllMocks();
});
test('should call the "onDayClick" from the DayPicker props', () => {
expect(dayPickerProps.onDayClick).toHaveBeenCalledWith(
day,
activeModifiers,
stubEvent
);
});
test('should call "onSelect" with the day selected', () => {
expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
{ from: day, to },
day,
activeModifiers,
stubEvent
);
});
});
});
describe('when "from" and "to" are the same', () => {
const date = new Date();
const selected = { from: date, to: date };
const dayPickerProps: DayPickerRangeProps = {
...initialProps,
selected
};
beforeAll(() => {
setup(dayPickerProps);
});
test('should return the "range_start" modifier with the date', () => {
expect(result.current.modifiers.range_start).toEqual([date]);
});
test('should return the "range_end" modifier with the date', () => {
expect(result.current.modifiers.range_end).toEqual([date]);
});
test('should return an empty "range_middle"', () => {
expect(result.current.modifiers.range_middle).toEqual([]);
});
});
describe('when the max number of the selected days is reached', () => {
const from = today;
const to = addDays(today, 6);
const selected = { from, to };
const dayPickerProps: DayPickerRangeProps = {
...initialProps,
selected,
max: 7
};
beforeAll(() => {
setup(dayPickerProps);
});
test('the days in the range should not be disabled', () => {
const { disabled } = result.current.modifiers;
expect(isMatch(from, disabled)).toBe(false);
expect(isMatch(to, disabled)).toBe(false);
});
test('the other days should be disabled', () => {
const { disabled } = result.current.modifiers;
expect(isMatch(addMonths(from, 1), disabled)).toBe(true);
});
describe('when "onDayClick" is called with a new day', () => {
const day = addMonths(from, 1);
const activeModifiers: ActiveModifiers = {};
beforeAll(() => {
result.current.onDayClick?.(day, activeModifiers, stubEvent);
});
afterAll(() => {
jest.resetAllMocks();
});
test('should call the "onDayClick" from the DayPicker props', () => {
expect(dayPickerProps.onDayClick).toHaveBeenCalledWith(
day,
activeModifiers,
stubEvent
);
});
});
});
describe('when the minimum number of days are selected', () => {
const selected = { from, to };
const dayPickerProps: DayPickerRangeProps = {
...initialProps,
selected,
min: Math.abs(differenceInCalendarDays(to, from))
};
beforeAll(() => {
setup(dayPickerProps);
});
describe('when "onDayClick" is called with a day before "from"', () => {
const day = subDays(from, 1);
const activeModifiers: ActiveModifiers = { selected: true };
beforeAll(() => {
result.current.onDayClick?.(day, activeModifiers, stubEvent);
});
afterAll(() => {
jest.resetAllMocks();
});
test('should call "onSelect" with the day included in the range', () => {
expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
{ from: day, to },
day,
activeModifiers,
stubEvent
);
});
});
describe('when "onDayClick" is called with the "from" day', () => {
const day = from;
const activeModifiers: ActiveModifiers = { selected: true };
beforeAll(() => {
result.current.onDayClick?.(day, activeModifiers, stubEvent);
});
afterAll(() => {
jest.resetAllMocks();
});
test('should call the "onDayClick" from the DayPicker props', () => {
expect(dayPickerProps.onDayClick).toHaveBeenCalledWith(
day,
activeModifiers,
stubEvent
);
});
test('should call "onSelect" with an undefined range', () => {
expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
undefined,
day,
activeModifiers,
stubEvent
);
});
});
describe('when "onDayClick" is called with the "to" day', () => {
const day = to;
const activeModifiers: ActiveModifiers = { selected: true };
beforeAll(() => {
result.current.onDayClick?.(day, activeModifiers, stubEvent);
});
afterAll(() => {
jest.resetAllMocks();
});
test('should call "onSelect" without the "to" in the range', () => {
expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
{ from: day, to: undefined },
day,
activeModifiers,
stubEvent
);
});
});
});