bc518174
王天杨
提交两个项目文件
|
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
|
/* eslint-disable testing-library/render-result-naming-convention */
import { es } from 'date-fns/locale';
import { DayPickerProps } from 'DayPicker';
import { renderDayPickerHook } from 'test/render';
import { freezeBeforeAll } from 'test/utils';
import { CaptionLayout } from 'components/Caption';
import { DayPickerContextValue, useDayPicker } from 'contexts/DayPicker';
import {
DefaultContextProps,
getDefaultContextValues
} from 'contexts/DayPicker/defaultContextValues';
import { DaySelectionMode } from 'types/DayPickerBase';
import { Formatters } from 'types/Formatters';
import { Labels } from 'types/Labels';
import { DayModifiers, ModifiersClassNames } from 'types/Modifiers';
import { ClassNames, Styles } from 'types/Styles';
const today = new Date(2022, 5, 13);
const defaults = getDefaultContextValues();
freezeBeforeAll(today);
function renderHook(props?: DayPickerProps) {
return renderDayPickerHook<DayPickerContextValue>(useDayPicker, props);
}
describe('when rendered without props', () => {
const testPropNames = Object.keys(defaults).filter(
(key) => key !== 'today'
) as DefaultContextProps[];
test.each(testPropNames)('should use the %s default value', (propName) => {
const result = renderHook();
expect(result.current[propName]).toEqual(defaults[propName]);
});
});
describe('when passing "locale" from props', () => {
const locale = es;
test('should return the custom locale', () => {
const result = renderHook({ locale });
expect(result.current.locale).toBe(locale);
});
});
describe('when passing "numberOfMonths" from props', () => {
const numberOfMonths = 4;
test('should return the custom numberOfMonths', () => {
const result = renderHook({ numberOfMonths });
expect(result.current.numberOfMonths).toBe(4);
});
});
describe('when passing "today" from props', () => {
const today = new Date(2010, 9, 11);
test('should return the custom "today"', () => {
const result = renderHook({ today });
expect(result.current.today).toBe(today);
});
});
describe('when passing "captionLayout" from props', () => {
const captionLayout: CaptionLayout = 'dropdown';
const fromYear = 2000;
const toYear = 2010;
const dayPickerProps: DayPickerProps = { captionLayout, fromYear, toYear };
test('should return the custom "captionLayout"', () => {
const result = renderHook(dayPickerProps);
expect(result.current.captionLayout).toBe(captionLayout);
});
});
describe('when "fromDate" and "toDate" are undefined', () => {
const fromDate = undefined;
const toDate = undefined;
describe('when using "dropdown" as "captionLayout"', () => {
const captionLayout: CaptionLayout = 'dropdown';
test('should return "buttons" as "captionLayout"', () => {
const result = renderHook({
fromDate,
toDate,
captionLayout
});
expect(result.current.captionLayout).toBe('buttons');
});
});
});
describe('when "fromDate" is undefined, but not "toDate"', () => {
const fromDate = undefined;
const toDate = new Date();
describe('when using "dropdown" as "captionLayout"', () => {
const captionLayout: CaptionLayout = 'dropdown';
test('should return "buttons" as "captionLayout"', () => {
const result = renderHook({
fromDate,
toDate,
captionLayout
});
expect(result.current.captionLayout).toBe('buttons');
});
});
});
describe('when "toDate" is undefined, but not "fromDate"', () => {
const fromDate = new Date();
const toDate = undefined;
describe('when using "dropdown" as "captionLayout"', () => {
const captionLayout: CaptionLayout = 'dropdown';
test('should return "buttons" as "captionLayout"', () => {
const result = renderHook({
fromDate,
toDate,
captionLayout
});
expect(result.current.captionLayout).toBe('buttons');
});
});
});
describe('when using "dropdown" as "captionLayout"', () => {
const captionLayout: CaptionLayout = 'dropdown';
const fromYear = 2000;
const toYear = 2010;
test('should return the custom "captionLayout"', () => {
const result = renderHook({ captionLayout, fromYear, toYear });
expect(result.current.captionLayout).toBe(captionLayout);
});
});
describe('when passing "modifiers" from props', () => {
const modifiers: DayModifiers = { foo: new Date() };
test('should return the custom "modifiers"', () => {
const result = renderHook({ modifiers });
expect(result.current.modifiers).toStrictEqual(modifiers);
});
});
describe('when passing "modifiersClassNames" from props', () => {
const modifiersClassNames: ModifiersClassNames = { foo: 'bar' };
test('should return the custom "modifiersClassNames"', () => {
const result = renderHook({ modifiersClassNames });
expect(result.current.modifiersClassNames).toStrictEqual(
modifiersClassNames
);
});
});
describe('when passing "styles" from props', () => {
const styles: Styles = { caption: { color: 'red ' } };
test('should include the custom "styles"', () => {
const result = renderHook({ styles });
expect(result.current.styles).toStrictEqual({
...defaults.styles,
...styles
});
});
});
describe('when passing "classNames" from props', () => {
const classNames: ClassNames = { caption: 'foo' };
test('should include the custom "classNames"', () => {
const result = renderHook({ classNames });
expect(result.current.classNames).toStrictEqual({
...defaults.classNames,
...classNames
});
});
});
describe('when passing "formatters" from props', () => {
const formatters: Partial<Formatters> = { formatCaption: jest.fn() };
test('should include the custom "formatters"', () => {
const result = renderHook({ formatters });
expect(result.current.formatters).toStrictEqual({
...defaults.formatters,
...formatters
});
});
});
describe('when passing "labels" from props', () => {
const labels: Partial<Labels> = { labelDay: jest.fn() };
test('should include the custom "labels"', () => {
const result = renderHook({ labels });
expect(result.current.labels).toStrictEqual({
...defaults.labels,
...labels
});
});
});
describe('when passing an "id" from props', () => {
test('should return the id', () => {
const result = renderHook({ id: 'foo' });
expect(result.current.id).toBe('foo');
});
});
describe('when in selection mode', () => {
const mode: DaySelectionMode = 'multiple';
const onSelect = jest.fn();
test('should return the "onSelect" event handler', () => {
const result = renderHook({ mode, onSelect });
expect(result.current.onSelect).toBe(onSelect);
});
});
|