react-day-picker 8.4.0 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/index.d.ts +123 -70
  2. package/dist/index.esm.js +14 -50
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.js +122 -158
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.min.js +2 -0
  7. package/dist/index.min.js.map +1 -0
  8. package/dist/style.css +2 -1
  9. package/dist/style.css.d.ts +38 -37
  10. package/dist/style.css.map +1 -0
  11. package/dist/style.module.css +0 -1
  12. package/dist/style.module.css.d.ts +38 -37
  13. package/package.json +35 -32
  14. package/src/DayPicker.tsx +1 -1
  15. package/src/components/Caption/Caption.test.tsx +2 -2
  16. package/src/components/CaptionDropdowns/CaptionDropdowns.test.tsx +7 -6
  17. package/src/components/CaptionLabel/CaptionLabel.test.tsx +1 -1
  18. package/src/components/CaptionLabel/CaptionLabel.tsx +3 -3
  19. package/src/components/CaptionNavigation/CaptionNavigation.test.tsx +11 -10
  20. package/src/components/CaptionNavigation/CaptionNavigation.tsx +1 -1
  21. package/src/components/Day/Day.test.tsx +15 -16
  22. package/src/components/Day/Day.tsx +1 -1
  23. package/src/components/DayContent/DayContent.test.tsx +1 -1
  24. package/src/components/Footer/Footer.test.tsx +1 -1
  25. package/src/components/HeadRow/HeadRow.test.tsx +1 -20
  26. package/src/components/HeadRow/HeadRow.tsx +2 -6
  27. package/src/components/HeadRow/utils/getWeekdays.test.ts +1 -1
  28. package/src/components/HeadRow/utils/getWeekdays.ts +1 -5
  29. package/src/components/Month/Month.test.tsx +1 -1
  30. package/src/components/MonthsDropdown/MonthsDropdown.test.tsx +3 -2
  31. package/src/components/MonthsDropdown/MonthsDropdown.tsx +1 -3
  32. package/src/components/Navigation/Navigation.test.tsx +10 -9
  33. package/src/components/Root/Root.test.tsx +2 -3
  34. package/src/components/Row/Row.tsx +2 -1
  35. package/src/components/Table/Table.tsx +1 -1
  36. package/src/components/Table/__snapshots__/Table.test.tsx.snap +219 -210
  37. package/src/components/Table/utils/daysToMonthWeeks.ts +11 -10
  38. package/src/components/Table/utils/getMonthWeeks.ts +7 -6
  39. package/src/components/YearsDropdown/YearsDropdown.test.tsx +3 -2
  40. package/src/components/YearsDropdown/YearsDropdown.tsx +1 -3
  41. package/src/contexts/DayPicker/DayPickerContext.test.ts +55 -138
  42. package/src/contexts/DayPicker/defaultContextValues.ts +1 -1
  43. package/src/contexts/DayPicker/formatters/formatCaption.test.ts +1 -1
  44. package/src/contexts/DayPicker/formatters/formatCaption.ts +1 -2
  45. package/src/contexts/DayPicker/formatters/formatDay.ts +1 -1
  46. package/src/contexts/DayPicker/formatters/formatMonthCaption.test.ts +1 -1
  47. package/src/contexts/DayPicker/formatters/formatMonthCaption.ts +1 -1
  48. package/src/contexts/DayPicker/formatters/formatWeekdayName.test.ts +3 -3
  49. package/src/contexts/DayPicker/formatters/formatWeekdayName.ts +2 -2
  50. package/src/contexts/DayPicker/formatters/formatYearCaption.ts +1 -1
  51. package/src/contexts/DayPicker/labels/labelDay.ts +1 -1
  52. package/src/contexts/DayPicker/labels/labelWeekday.test.ts +1 -1
  53. package/src/contexts/DayPicker/labels/labelWeekday.ts +1 -1
  54. package/src/contexts/DayPicker/utils/parseFromToProps.ts +1 -3
  55. package/src/contexts/Focus/FocusContext.test.ts +35 -62
  56. package/src/contexts/Focus/FocusContext.tsx +1 -1
  57. package/src/contexts/Focus/utils/getInitialFocusTarget.ts +1 -3
  58. package/src/contexts/Focus/utils/getNextFocus.ts +12 -10
  59. package/src/contexts/Modifiers/ModifiersContext.test.ts +8 -14
  60. package/src/contexts/Modifiers/utils/getActiveModifiers.ts +1 -1
  61. package/src/contexts/Modifiers/utils/isDateInRange.ts +1 -2
  62. package/src/contexts/Modifiers/utils/isMatch.ts +1 -4
  63. package/src/contexts/Navigation/NavigationContext.test.ts +12 -17
  64. package/src/contexts/Navigation/NavigationContext.tsx +1 -3
  65. package/src/contexts/Navigation/useNavigationState.test.ts +9 -16
  66. package/src/contexts/Navigation/useNavigationState.ts +6 -4
  67. package/src/contexts/Navigation/utils/getDisplayMonths.ts +1 -3
  68. package/src/contexts/Navigation/utils/getInitialMonth.ts +1 -3
  69. package/src/contexts/Navigation/utils/getNextMonth.ts +1 -3
  70. package/src/contexts/Navigation/utils/getPreviousMonth.ts +1 -3
  71. package/src/contexts/SelectMultiple/SelectMultipleContext.test.ts +15 -20
  72. package/src/contexts/SelectMultiple/SelectMultipleContext.tsx +1 -1
  73. package/src/contexts/SelectRange/SelectRangeContext.test.ts +23 -29
  74. package/src/contexts/SelectRange/SelectRangeContext.tsx +6 -4
  75. package/src/contexts/SelectRange/utils/addToRange.ts +1 -3
  76. package/src/contexts/SelectSingle/SelectSingleContext.test.ts +18 -15
  77. package/src/hooks/useActiveModifiers/useActiveModifiers.test.tsx +9 -16
  78. package/src/hooks/useControlledValue/useControlledValue.test.ts +16 -39
  79. package/src/hooks/useDayEventHandlers/useDayEventHandlers.test.tsx +18 -78
  80. package/src/hooks/useDayRender/useDayRender.test.tsx +68 -95
  81. package/src/hooks/useDayRender/useDayRender.tsx +5 -8
  82. package/src/hooks/useInput/useInput.ts +2 -4
  83. package/src/hooks/useSelectedDays/useSelectedDays.test.ts +15 -45
  84. package/src/index.ts +1 -0
  85. package/src/style.css +0 -1
  86. package/src/style.css.d.ts +37 -36
  87. package/src/types/Labels.ts +1 -0
  88. package/dist/index.esm.d.ts +0 -1175
  89. package/dist/react-day-picker.min.js +0 -1
@@ -1,4 +1,4 @@
1
- import { act, RenderResult } from '@testing-library/react-hooks';
1
+ import { act } from '@testing-library/react';
2
2
  import {
3
3
  addDays,
4
4
  addMonths,
@@ -8,19 +8,16 @@ import {
8
8
  startOfWeek
9
9
  } from 'date-fns';
10
10
 
11
- import { customRenderHook } from 'test/render';
11
+ import { renderDayPickerHook, RenderHookResult } from 'test/render';
12
12
  import { freezeBeforeAll } from 'test/utils';
13
13
 
14
14
  import { FocusContextValue, useFocusContext } from 'contexts/Focus';
15
15
 
16
- let renderResult: RenderResult<FocusContextValue>;
17
-
18
16
  const today = new Date(2021, 11, 8); // make sure is in the middle of the week for the complete test
19
17
  freezeBeforeAll(today);
20
18
 
21
- function setup() {
22
- const { result } = customRenderHook(() => useFocusContext());
23
- renderResult = result;
19
+ function renderHook() {
20
+ return renderDayPickerHook<FocusContextValue>(useFocusContext);
24
21
  }
25
22
 
26
23
  type HookFunction =
@@ -35,12 +32,9 @@ type HookFunction =
35
32
  | 'focusStartOfWeek'
36
33
  | 'focusEndOfWeek';
37
34
 
38
- beforeEach(() => {
39
- setup();
40
- });
41
-
42
35
  test('`focusedDay` should be undefined', () => {
43
- expect(renderResult.current.focusedDay).toBeUndefined();
36
+ const result = renderHook();
37
+ expect(result.current.focusedDay).toBeUndefined();
44
38
  });
45
39
 
46
40
  const tests: Array<HookFunction> = [
@@ -56,128 +50,107 @@ const tests: Array<HookFunction> = [
56
50
  'focusEndOfWeek'
57
51
  ];
58
52
  describe.each(tests)('when calling %s', (fn: HookFunction) => {
59
- beforeEach(() => {
60
- renderResult.current[fn];
61
- });
62
53
  test('`focusedDay` should be undefined', () => {
63
- expect(renderResult.current.focusedDay).toBeUndefined();
54
+ const result = renderHook();
55
+ result.current[fn];
56
+ expect(result.current.focusedDay).toBeUndefined();
64
57
  });
65
58
  });
66
59
 
67
60
  describe('when a day is focused', () => {
68
61
  const day = today;
62
+ let result: RenderHookResult<FocusContextValue>;
69
63
  beforeEach(() => {
70
- act(() => renderResult.current.focus(day));
64
+ result = renderHook();
65
+ act(() => result.current.focus(day));
71
66
  });
72
67
  test('should set the focused day', () => {
73
- expect(renderResult.current.focusedDay).toEqual(day);
68
+ expect(result.current.focusedDay).toEqual(day);
74
69
  });
75
70
  describe('when "focusDayBefore" is called', () => {
76
71
  const dayBefore = addDays(day, -1);
77
- beforeEach(() => {
78
- act(() => renderResult.current.focusDayBefore());
79
- });
72
+ beforeEach(() => act(() => result.current.focusDayBefore()));
80
73
  test('should focus the day before', () => {
81
- expect(renderResult.current.focusedDay).toEqual(dayBefore);
74
+ expect(result.current.focusedDay).toEqual(dayBefore);
82
75
  });
83
76
  test.todo('should call the navigation goToDate');
84
77
  });
85
78
  describe('when "focusDayAfter" is called', () => {
86
- beforeEach(() => {
87
- act(() => renderResult.current.focusDayAfter());
88
- });
79
+ beforeEach(() => act(() => result.current.focusDayAfter()));
89
80
  test('should focus the day after', () => {
90
81
  const dayAfter = addDays(day, 1);
91
- expect(renderResult.current.focusedDay).toEqual(dayAfter);
82
+ expect(result.current.focusedDay).toEqual(dayAfter);
92
83
  });
93
84
  test.todo('should call the navigation goToDate');
94
85
  });
95
86
  describe('when "focusWeekBefore" is called', () => {
96
- beforeEach(() => {
97
- act(() => renderResult.current.focusWeekBefore());
98
- });
87
+ beforeEach(() => act(() => result.current.focusWeekBefore()));
99
88
  test('should focus the day in the previous week', () => {
100
89
  const prevWeek = addWeeks(day, -1);
101
- expect(renderResult.current.focusedDay).toEqual(prevWeek);
90
+ expect(result.current.focusedDay).toEqual(prevWeek);
102
91
  });
103
92
  test.todo('should call the navigation goToDate');
104
93
  });
105
94
  describe('when "focusWeekAfter" is called', () => {
106
- beforeEach(() => {
107
- act(() => renderResult.current.focusWeekAfter());
108
- });
95
+ beforeEach(() => act(() => result.current.focusWeekAfter()));
109
96
  test('should focus the day in the next week', () => {
110
97
  const nextWeek = addWeeks(day, 1);
111
- expect(renderResult.current.focusedDay).toEqual(nextWeek);
98
+ expect(result.current.focusedDay).toEqual(nextWeek);
112
99
  });
113
100
  test.todo('should call the navigation goToDate');
114
101
  });
115
102
  describe('when "focusStartOfWeek" is called', () => {
116
- beforeEach(() => {
117
- act(() => renderResult.current.focusStartOfWeek());
118
- });
103
+ beforeEach(() => act(() => result.current.focusStartOfWeek()));
119
104
  test('should focus the first day of the week', () => {
120
105
  const firstDayOfWeek = startOfWeek(day);
121
- expect(renderResult.current.focusedDay).toEqual(firstDayOfWeek);
106
+ expect(result.current.focusedDay).toEqual(firstDayOfWeek);
122
107
  });
123
108
  test.todo('should call the navigation goToDate');
124
109
  });
125
110
  describe('when "focusEndOfWeek" is called', () => {
126
- beforeEach(() => {
127
- act(() => renderResult.current.focusEndOfWeek());
128
- });
111
+ beforeEach(() => act(() => result.current.focusEndOfWeek()));
129
112
  test('should focus the last day of the week', () => {
130
113
  const lastDayOfWeek = endOfWeek(day);
131
- expect(renderResult.current.focusedDay).toEqual(lastDayOfWeek);
114
+ expect(result.current.focusedDay).toEqual(lastDayOfWeek);
132
115
  });
133
116
  test.todo('should call the navigation goToDate');
134
117
  });
135
118
  describe('when "focusMonthBefore" is called', () => {
136
- beforeEach(() => {
137
- act(() => renderResult.current.focusMonthBefore());
138
- });
119
+ beforeEach(() => act(() => result.current.focusMonthBefore()));
139
120
  test('should focus the day in the month before', () => {
140
121
  const monthBefore = addMonths(day, -1);
141
- expect(renderResult.current.focusedDay).toEqual(monthBefore);
122
+ expect(result.current.focusedDay).toEqual(monthBefore);
142
123
  });
143
124
  test.todo('should call the navigation goToDate');
144
125
  });
145
126
  describe('when "focusMonthAfter" is called', () => {
146
- beforeEach(() => {
147
- act(() => renderResult.current.focusMonthAfter());
148
- });
127
+ beforeEach(() => act(() => result.current.focusMonthAfter()));
149
128
  test('should focus the day in the month after', () => {
150
129
  const monthAfter = addMonths(day, 1);
151
- expect(renderResult.current.focusedDay).toEqual(monthAfter);
130
+ expect(result.current.focusedDay).toEqual(monthAfter);
152
131
  });
153
132
  test.todo('should call the navigation goToDate');
154
133
  });
155
134
  describe('when "focusYearBefore" is called', () => {
156
- beforeEach(() => {
157
- act(() => renderResult.current.focusYearBefore());
158
- });
135
+ beforeEach(() => act(() => result.current.focusYearBefore()));
159
136
  test('should focus the day in the year before', () => {
160
137
  const prevYear = addYears(day, -1);
161
- expect(renderResult.current.focusedDay).toEqual(prevYear);
138
+ expect(result.current.focusedDay).toEqual(prevYear);
162
139
  });
163
140
  test.todo('should call the navigation goToDate');
164
141
  });
165
142
  describe('when "focusYearAfter" is called', () => {
166
- beforeEach(() => {
167
- act(() => renderResult.current.focusYearAfter());
168
- });
143
+ beforeEach(() => act(() => result.current.focusYearAfter()));
169
144
  test('should focus the day in the year after', () => {
170
145
  const nextYear = addYears(day, 1);
171
- expect(renderResult.current.focusedDay).toEqual(nextYear);
146
+ expect(result.current.focusedDay).toEqual(nextYear);
172
147
  });
173
148
  test.todo('should call the navigation goToDate');
174
149
  });
175
150
  describe('when blur is called', () => {
176
- beforeEach(() => {
177
- act(() => renderResult.current.blur());
178
- });
151
+ beforeEach(() => act(() => result.current.blur()));
179
152
  test('`focusedDay` should be undefined', () => {
180
- expect(renderResult.current.focusedDay).toBeUndefined();
153
+ expect(result.current.focusedDay).toBeUndefined();
181
154
  });
182
155
  });
183
156
  });
@@ -1,6 +1,6 @@
1
1
  import React, { createContext, ReactNode, useContext, useState } from 'react';
2
2
 
3
- import isSameDay from 'date-fns/isSameDay';
3
+ import { isSameDay } from 'date-fns';
4
4
 
5
5
  import { useDayPicker } from 'contexts/DayPicker';
6
6
 
@@ -1,6 +1,4 @@
1
- import addDays from 'date-fns/addDays';
2
- import endOfMonth from 'date-fns/endOfMonth';
3
- import startOfMonth from 'date-fns/startOfMonth';
1
+ import { addDays, endOfMonth, startOfMonth } from 'date-fns';
4
2
 
5
3
  import { getActiveModifiers } from 'contexts/Modifiers';
6
4
  import { Modifiers } from 'types/Modifiers';
@@ -1,13 +1,15 @@
1
- import addDays from 'date-fns/addDays';
2
- import addMonths from 'date-fns/addMonths';
3
- import addWeeks from 'date-fns/addWeeks';
4
- import addYears from 'date-fns/addYears';
5
- import endOfISOWeek from 'date-fns/endOfISOWeek';
6
- import endOfWeek from 'date-fns/endOfWeek';
7
- import max from 'date-fns/max';
8
- import min from 'date-fns/min';
9
- import startOfISOWeek from 'date-fns/startOfISOWeek';
10
- import startOfWeek from 'date-fns/startOfWeek';
1
+ import {
2
+ addDays,
3
+ addMonths,
4
+ addWeeks,
5
+ addYears,
6
+ endOfISOWeek,
7
+ endOfWeek,
8
+ max,
9
+ min,
10
+ startOfISOWeek,
11
+ startOfWeek
12
+ } from 'date-fns';
11
13
 
12
14
  import { DayPickerContextValue } from 'contexts/DayPicker';
13
15
  import { getActiveModifiers } from 'contexts/Modifiers';
@@ -1,20 +1,16 @@
1
- import { RenderResult } from '@testing-library/react-hooks';
2
1
  import { DayPickerProps } from 'DayPicker';
3
2
 
4
- import { customRenderHook } from 'test/render';
3
+ import { renderDayPickerHook } from 'test/render';
5
4
 
6
5
  import { useModifiers } from 'contexts/Modifiers';
7
6
  import { DayModifiers, InternalModifier, Modifiers } from 'types/Modifiers';
8
7
 
9
- let renderResult: RenderResult<Modifiers>;
8
+ const internalModifiers = Object.values(InternalModifier);
10
9
 
11
- function setup(dayPickerProps: DayPickerProps) {
12
- const { result } = customRenderHook(() => useModifiers(), dayPickerProps);
13
- renderResult = result;
10
+ function renderHook(dayPickerProps: Partial<DayPickerProps> = {}) {
11
+ return renderDayPickerHook<Modifiers>(useModifiers, dayPickerProps);
14
12
  }
15
13
 
16
- const internalModifiers = Object.values(InternalModifier);
17
-
18
14
  describe('when rendered with custom modifiers', () => {
19
15
  const modifier = new Date(2018, 11, 12);
20
16
  const dayModifiers: DayModifiers = {
@@ -28,17 +24,15 @@ describe('when rendered with custom modifiers', () => {
28
24
  range_end: modifier,
29
25
  range_middle: modifier
30
26
  };
31
- beforeEach(() => {
32
- setup({ modifiers: dayModifiers });
33
- });
34
-
35
27
  test('should return the custom modifiers', () => {
36
- expect(renderResult.current.foo).toEqual([dayModifiers.foo]);
28
+ const result = renderHook({ modifiers: dayModifiers });
29
+ expect(result.current.foo).toEqual([dayModifiers.foo]);
37
30
  });
38
31
  test.each(internalModifiers)(
39
32
  'should override the %s internal modifier',
40
33
  (internalModifier) => {
41
- expect(renderResult.current[internalModifier]).toEqual([
34
+ const result = renderHook({ modifiers: dayModifiers });
35
+ expect(result.current[internalModifier]).toEqual([
42
36
  dayModifiers[internalModifier]
43
37
  ]);
44
38
  }
@@ -1,4 +1,4 @@
1
- import isSameMonth from 'date-fns/isSameMonth';
1
+ import { isSameMonth } from 'date-fns';
2
2
 
3
3
  import { ActiveModifiers, Modifiers } from 'types/Modifiers';
4
4
 
@@ -1,5 +1,4 @@
1
- import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
2
- import isSameDay from 'date-fns/isSameDay';
1
+ import { differenceInCalendarDays, isSameDay } from 'date-fns';
3
2
 
4
3
  import { DateRange } from 'types/Matchers';
5
4
 
@@ -1,7 +1,4 @@
1
- import { isAfter } from 'date-fns';
2
- import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
3
- import isDate from 'date-fns/isDate';
4
- import isSameDay from 'date-fns/isSameDay';
1
+ import { differenceInCalendarDays, isAfter, isDate, isSameDay } from 'date-fns';
5
2
 
6
3
  import {
7
4
  isDateAfterType,
@@ -1,28 +1,24 @@
1
- import { act, RenderResult } from '@testing-library/react-hooks';
1
+ import { act } from '@testing-library/react';
2
2
  import { addMonths, startOfMonth, subMonths } from 'date-fns';
3
+ import { DayPickerProps } from 'DayPicker';
3
4
 
4
- import { customRenderHook } from 'test/render/customRenderHook';
5
+ import { renderDayPickerHook, RenderHookResult } from 'test/render';
5
6
  import { freezeBeforeAll } from 'test/utils';
6
7
 
7
- import { DayPickerBase } from 'types/DayPickerBase';
8
-
9
8
  import { NavigationContextValue, useNavigation } from './NavigationContext';
10
9
 
11
10
  const today = new Date(2021, 11, 8);
12
11
  const todaysMonth = startOfMonth(today);
13
12
  freezeBeforeAll(today);
14
13
 
15
- let result: RenderResult<NavigationContextValue>;
16
-
17
- function setup(dayPickerProps?: DayPickerBase) {
18
- const view = customRenderHook(() => useNavigation(), dayPickerProps);
19
- result = view.result;
20
- return result;
14
+ function renderHook(props: Partial<DayPickerProps> = {}) {
15
+ return renderDayPickerHook<NavigationContextValue>(useNavigation, props);
21
16
  }
22
17
 
18
+ let result: RenderHookResult<NavigationContextValue>;
23
19
  describe('when rendered', () => {
24
20
  beforeEach(() => {
25
- setup();
21
+ result = renderHook();
26
22
  });
27
23
  test('the current month should be the today`s month', () => {
28
24
  expect(result.current.currentMonth).toEqual(todaysMonth);
@@ -39,9 +35,8 @@ describe('when rendered', () => {
39
35
  describe('when goToMonth is called', () => {
40
36
  const newMonth = addMonths(todaysMonth, 10);
41
37
  beforeEach(() => {
42
- act(() => {
43
- result.current.goToMonth(newMonth);
44
- });
38
+ result = renderHook();
39
+ act(() => result.current.goToMonth(newMonth));
45
40
  });
46
41
  test('should go to the specified month', () => {
47
42
  expect(result.current.currentMonth).toEqual(newMonth);
@@ -60,7 +55,7 @@ describe('when rendered', () => {
60
55
  const newDate = addMonths(today, 10);
61
56
  const onMonthChange = jest.fn();
62
57
  beforeEach(() => {
63
- setup({ onMonthChange });
58
+ result = renderHook({ onMonthChange });
64
59
  act(() => result.current.goToDate(newDate));
65
60
  });
66
61
  test('should go to the specified month', () => {
@@ -86,7 +81,7 @@ describe('when rendered', () => {
86
81
  const numberOfMonths = 2;
87
82
  describe('when the number of months is ${numberOfMonths}', () => {
88
83
  beforeEach(() => {
89
- setup({ numberOfMonths: 2 });
84
+ result = renderHook({ numberOfMonths: 2 });
90
85
  });
91
86
  test('the current month should be the today`s month', () => {
92
87
  expect(result.current.currentMonth).toEqual(todaysMonth);
@@ -107,7 +102,7 @@ describe('when the number of months is ${numberOfMonths}', () => {
107
102
 
108
103
  describe(`when the number of months is ${numberOfMonths} and the navigation is paged`, () => {
109
104
  beforeEach(() => {
110
- setup({ numberOfMonths, pagedNavigation: true });
105
+ result = renderHook({ numberOfMonths, pagedNavigation: true });
111
106
  });
112
107
  test('the current month should be the today`s month', () => {
113
108
  expect(result.current.currentMonth).toEqual(todaysMonth);
@@ -1,8 +1,6 @@
1
1
  import React, { createContext, ReactNode, useContext } from 'react';
2
2
 
3
- import addMonths from 'date-fns/addMonths';
4
- import isBefore from 'date-fns/isBefore';
5
- import isSameMonth from 'date-fns/isSameMonth';
3
+ import { addMonths, isBefore, isSameMonth } from 'date-fns';
6
4
 
7
5
  import { useDayPicker } from '../DayPicker';
8
6
  import { useNavigationState } from './useNavigationState';
@@ -1,39 +1,32 @@
1
- import { act } from '@testing-library/react-hooks';
1
+ import { act } from '@testing-library/react';
2
2
  import { addMonths, startOfMonth } from 'date-fns';
3
+ import { DayPickerProps } from 'DayPicker';
3
4
 
4
- import { customRenderHook } from 'test/render/customRenderHook';
5
+ import { renderDayPickerHook } from 'test/render';
5
6
  import { freezeBeforeAll } from 'test/utils';
6
7
 
7
- import { DayPickerBase } from 'types/DayPickerBase';
8
-
9
- import { useNavigationState } from './useNavigationState';
8
+ import { NavigationState, useNavigationState } from './useNavigationState';
10
9
 
11
10
  const today = new Date(2021, 11, 8);
12
11
  freezeBeforeAll(today);
13
12
 
14
- function setup(dayPickerProps?: DayPickerBase) {
15
- const { result } = customRenderHook(
16
- () => useNavigationState(),
17
- dayPickerProps
18
- );
19
- return result;
13
+ function renderHook(props: Partial<DayPickerProps> = {}) {
14
+ return renderDayPickerHook<NavigationState>(useNavigationState, props);
20
15
  }
21
16
 
22
17
  describe('when goToMonth is called', () => {
23
18
  test('should set the month in state', () => {
24
19
  const onMonthChange = jest.fn();
25
- const result = setup({ onMonthChange });
20
+ const result = renderHook({ onMonthChange });
26
21
  const month = addMonths(today, 2);
27
- act(() => {
28
- result.current[1](month);
29
- });
22
+ act(() => result.current[1](month));
30
23
  expect(result.current[0]).toEqual(startOfMonth(month));
31
24
  expect(onMonthChange).toHaveBeenCalledWith(startOfMonth(month));
32
25
  });
33
26
  describe('when navigation is disabled', () => {
34
27
  test('should not set the month in state', () => {
35
28
  const onMonthChange = jest.fn();
36
- const result = setup({ disableNavigation: true, onMonthChange });
29
+ const result = renderHook({ disableNavigation: true, onMonthChange });
37
30
  const month = addMonths(today, 2);
38
31
  result.current[1](month);
39
32
  expect(result.current[0]).toEqual(startOfMonth(today));
@@ -1,17 +1,19 @@
1
- import startOfMonth from 'date-fns/startOfMonth';
1
+ import { startOfMonth } from 'date-fns';
2
2
 
3
3
  import { useDayPicker } from 'contexts/DayPicker';
4
4
  import { useControlledValue } from 'hooks/useControlledValue';
5
5
 
6
6
  import { getInitialMonth } from './utils/getInitialMonth';
7
7
 
8
- /** Controls the navigation state. */
9
- export function useNavigationState(): [
8
+ export type NavigationState = [
10
9
  /** The month DayPicker is navigating at */
11
10
  month: Date,
12
11
  /** Go to the specified month. */
13
12
  goToMonth: (month: Date) => void
14
- ] {
13
+ ];
14
+
15
+ /** Controls the navigation state. */
16
+ export function useNavigationState(): NavigationState {
15
17
  const context = useDayPicker();
16
18
  const initialMonth = getInitialMonth(context);
17
19
  const [month, setMonth] = useControlledValue(initialMonth, context.month);
@@ -1,6 +1,4 @@
1
- import addMonths from 'date-fns/addMonths';
2
- import differenceInCalendarMonths from 'date-fns/differenceInCalendarMonths';
3
- import startOfMonth from 'date-fns/startOfMonth';
1
+ import { addMonths, differenceInCalendarMonths, startOfMonth } from 'date-fns';
4
2
 
5
3
  /**
6
4
  * Return the months to display in the component according to the number of
@@ -1,6 +1,4 @@
1
- import addMonths from 'date-fns/addMonths';
2
- import differenceInCalendarMonths from 'date-fns/differenceInCalendarMonths';
3
- import startOfMonth from 'date-fns/startOfMonth';
1
+ import { addMonths, differenceInCalendarMonths, startOfMonth } from 'date-fns';
4
2
 
5
3
  import { DayPickerContextValue } from 'contexts/DayPicker';
6
4
 
@@ -1,6 +1,4 @@
1
- import addMonths from 'date-fns/addMonths';
2
- import differenceInCalendarMonths from 'date-fns/differenceInCalendarMonths';
3
- import startOfMonth from 'date-fns/startOfMonth';
1
+ import { addMonths, differenceInCalendarMonths, startOfMonth } from 'date-fns';
4
2
 
5
3
  /**
6
4
  * Returns the next month the user can navigate to according to the given
@@ -1,6 +1,4 @@
1
- import addMonths from 'date-fns/addMonths';
2
- import differenceInCalendarMonths from 'date-fns/differenceInCalendarMonths';
3
- import startOfMonth from 'date-fns/startOfMonth';
1
+ import { addMonths, differenceInCalendarMonths, startOfMonth } from 'date-fns';
4
2
 
5
3
  /**
6
4
  * Returns the next previous the user can navigate to, according to the given
@@ -1,7 +1,7 @@
1
- import { RenderResult } from '@testing-library/react-hooks';
2
1
  import { addDays, addMonths } from 'date-fns';
2
+ import { DayPickerProps } from 'DayPicker';
3
3
 
4
- import { customRenderHook } from 'test/render/customRenderHook';
4
+ import { renderDayPickerHook } from 'test/render';
5
5
  import { freezeBeforeAll } from 'test/utils';
6
6
 
7
7
  import { isMatch } from 'contexts/Modifiers/utils/isMatch';
@@ -16,17 +16,15 @@ import {
16
16
  const today = new Date(2021, 11, 8);
17
17
  freezeBeforeAll(today);
18
18
 
19
- let result: RenderResult<SelectMultipleContextValue>;
20
-
21
- function setup(dayPickerProps?: DayPickerMultipleProps) {
22
- const view = customRenderHook(useSelectMultiple, dayPickerProps);
23
- result = view.result;
19
+ function renderHook(props?: Partial<DayPickerProps>) {
20
+ return renderDayPickerHook<SelectMultipleContextValue>(
21
+ useSelectMultiple,
22
+ props
23
+ );
24
24
  }
25
25
 
26
26
  describe('when is not a multiple select DayPicker', () => {
27
- beforeAll(() => {
28
- setup();
29
- });
27
+ const result = renderHook();
30
28
  test('the selected day should be undefined', () => {
31
29
  expect(result.current.selected).toBeUndefined();
32
30
  });
@@ -52,10 +50,8 @@ describe('when days are selected', () => {
52
50
  selected
53
51
  };
54
52
 
55
- beforeAll(() => {
56
- setup(dayPickerProps);
57
- });
58
53
  test('it should return the days as selected', () => {
54
+ const result = renderHook(dayPickerProps);
59
55
  expect(result.current.selected).toStrictEqual(selected);
60
56
  });
61
57
  describe('when `onDayClick` is called with a not selected day', () => {
@@ -63,6 +59,7 @@ describe('when days are selected', () => {
63
59
  const activeModifiers = {};
64
60
  const event = {} as React.MouseEvent;
65
61
  beforeAll(() => {
62
+ const result = renderHook(dayPickerProps);
66
63
  result.current.onDayClick?.(clickedDay, activeModifiers, event);
67
64
  });
68
65
  afterAll(() => {
@@ -89,6 +86,7 @@ describe('when days are selected', () => {
89
86
  const clickedDay = selectedDay1;
90
87
  const activeModifiers: ActiveModifiers = { selected: true };
91
88
  beforeAll(() => {
89
+ const result = renderHook(dayPickerProps);
92
90
  result.current.onDayClick?.(clickedDay, activeModifiers, event);
93
91
  });
94
92
  afterAll(() => {
@@ -121,17 +119,15 @@ describe('when the maximum number of days are selected', () => {
121
119
  selected,
122
120
  max: selected.length
123
121
  };
124
- beforeAll(() => {
125
- setup(dayPickerProps);
126
- });
127
-
128
122
  test('the selected days should not be disabled', () => {
123
+ const result = renderHook(dayPickerProps);
129
124
  const { disabled } = result.current.modifiers;
130
125
  expect(isMatch(selectedDay1, disabled)).toBe(false);
131
126
  expect(isMatch(selectedDay2, disabled)).toBe(false);
132
127
  expect(isMatch(selectedDay3, disabled)).toBe(false);
133
128
  });
134
129
  test('the other days should be disabled', () => {
130
+ const result = renderHook(dayPickerProps);
135
131
  const { disabled } = result.current.modifiers;
136
132
  expect(isMatch(addMonths(selectedDay1, 1), disabled)).toBe(true);
137
133
  expect(isMatch(addMonths(selectedDay2, 1), disabled)).toBe(true);
@@ -140,6 +136,7 @@ describe('when the maximum number of days are selected', () => {
140
136
  const clickedDay = addMonths(selectedDay1, 1);
141
137
  const activeModifiers: ActiveModifiers = {};
142
138
  beforeAll(() => {
139
+ const result = renderHook(dayPickerProps);
143
140
  result.current.onDayClick?.(clickedDay, activeModifiers, event);
144
141
  });
145
142
  afterAll(() => {
@@ -166,13 +163,11 @@ describe('when the minimum number of days are selected', () => {
166
163
  selected,
167
164
  min: selected.length
168
165
  };
169
- beforeAll(() => {
170
- setup(dayPickerProps);
171
- });
172
166
  describe('when `onDayClick` is called with one of the selected days', () => {
173
167
  const clickedDay = selected[0];
174
168
  const activeModifiers: ActiveModifiers = { selected: true };
175
169
  beforeAll(() => {
170
+ const result = renderHook(dayPickerProps);
176
171
  result.current.onDayClick?.(clickedDay, activeModifiers, event);
177
172
  });
178
173
  afterAll(() => {
@@ -1,6 +1,6 @@
1
1
  import React, { createContext, ReactNode, useContext } from 'react';
2
2
 
3
- import isSameDay from 'date-fns/isSameDay';
3
+ import { isSameDay } from 'date-fns';
4
4
 
5
5
  import { DayPickerBase } from 'types/DayPickerBase';
6
6
  import {