-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdate-picker-form.spec.ts
More file actions
366 lines (293 loc) · 10.9 KB
/
date-picker-form.spec.ts
File metadata and controls
366 lines (293 loc) · 10.9 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { elementUpdated, expect, fixture, html } from '@open-wc/testing';
import { CalendarDay, toCalendarDay } from '../calendar/model.js';
import { type DateRangeDescriptor, DateRangeType } from '../calendar/types.js';
import { defineComponents } from '../common/definitions/defineComponents.js';
import { equal } from '../common/util.js';
import {
type ValidationContainerTestsParams,
createFormAssociatedTestBed,
runValidationContainerTests,
simulatePointerDown,
} from '../common/utils.spec.js';
import IgcDateTimeInputComponent from '../date-time-input/date-time-input.js';
import IgcDatePickerComponent from './date-picker.js';
describe('igc-datepicker form integration', () => {
before(() => defineComponents(IgcDatePickerComponent));
function checkDatesEqual(a: CalendarDay | Date, b: CalendarDay | Date) {
expect(equal(toCalendarDay(a), toCalendarDay(b))).to.be.true;
}
describe('Initial validation', () => {
it('should not enter in invalid state when clicking the calendar toggle part', async () => {
const picker = await fixture<IgcDatePickerComponent>(
html`<igc-date-picker required></igc-date-picker>`
);
const dateTimeInput = picker.renderRoot.querySelector(
IgcDateTimeInputComponent.tagName
)!;
const icon = picker.renderRoot.querySelector('[name="today"]')!;
expect(picker.invalid).to.be.false;
expect(dateTimeInput.invalid).to.be.false;
simulatePointerDown(icon);
await elementUpdated(picker);
expect(picker.invalid).to.be.false;
expect(dateTimeInput.invalid).to.be.false;
});
});
describe('Form integration', () => {
const today = CalendarDay.today;
const spec = createFormAssociatedTestBed<IgcDatePickerComponent>(
html`<igc-date-picker name="datePicker"></igc-date-picker>`
);
beforeEach(async () => {
await spec.setup(IgcDatePickerComponent.tagName);
});
it('should be form associated', () => {
expect(spec.element.form).to.equal(spec.form);
});
it('should not participate in form submission if the value is empty/invalid', () => {
spec.assertSubmitHasValue(null);
});
it('should participate in form submission if there is a value and the value adheres to the validation constraints', () => {
spec.setProperties({ value: today.native });
spec.assertSubmitHasValue(spec.element.value?.toISOString());
});
it('should reset to its default value state on form reset', () => {
spec.setProperties({ value: today.native });
spec.reset();
expect(spec.element.value).to.be.null;
});
it('should reset to the new default value after setAttribute() call', () => {
spec.setAttributes({ value: today.native.toISOString() });
spec.setProperties({ value: today.add('day', 180).native });
spec.reset();
checkDatesEqual(spec.element.value!, today);
spec.assertSubmitHasValue(today.native.toISOString());
});
it('should reflect disabled ancestor state (fieldset/form)', () => {
spec.setAncestorDisabledState(true);
expect(spec.element.disabled).to.be.true;
spec.setAncestorDisabledState(false);
expect(spec.element.disabled).to.be.false;
});
it('should enforce required constraint', () => {
spec.setProperties({ required: true });
spec.assertSubmitFails();
spec.setProperties({ value: today.native });
spec.assertSubmitPasses();
});
it('should enforce min value constraint', () => {
// No value - submit passes
spec.setProperties({ min: new Date(2026, 0, 1) });
spec.assertSubmitPasses();
// Invalid min constraint
spec.setProperties({ value: new Date(2022, 0, 1) });
spec.assertSubmitFails();
// Valid value
spec.setProperties({ value: new Date(2026, 0, 2) });
spec.assertSubmitPasses();
});
it('should enforce max value constraint', () => {
// No value - submit passes
spec.setProperties({ max: new Date(2020, 0, 1) });
spec.assertSubmitPasses();
// Invalid max constraint
spec.setProperties({ value: today.native });
spec.assertSubmitFails();
// Valid value
spec.setProperties({ value: new Date(2020, 0, 1) });
spec.assertSubmitPasses();
});
it('should enforce min value constraint with string property', () => {
// No value - submit passes
spec.setProperties({ min: new Date(2026, 0, 1).toISOString() });
spec.assertSubmitPasses();
// Invalid min constraint
spec.setProperties({ value: new Date(2022, 0, 1).toISOString() });
spec.assertSubmitFails();
// Valid value
spec.setProperties({ value: new Date(2026, 0, 2).toISOString() });
spec.assertSubmitPasses();
});
it('should enforce max value constraint with string property', () => {
// No value - submit passes
spec.setProperties({ max: new Date(2020, 0, 1).toISOString() });
spec.assertSubmitPasses();
// Invalid max constraint
spec.setProperties({ value: today.native });
spec.assertSubmitFails();
// Valid value
spec.setProperties({ value: new Date(2020, 0, 1).toISOString() });
spec.assertSubmitPasses();
});
it('should invalidate the component if a disabled date is typed in the input', () => {
const minDate = new Date(2024, 1, 1);
const maxDate = new Date(2024, 1, 28);
const disabledDates: DateRangeDescriptor[] = [
{
type: DateRangeType.Between,
dateRange: [minDate, maxDate],
},
];
spec.setProperties({ disabledDates, value: new Date(2024, 1, 26) });
expect(spec.element.invalid).to.be.true;
spec.assertSubmitFails();
});
it('should enforce custom constraint', () => {
spec.element.setCustomValidity('invalid');
spec.assertSubmitFails();
spec.element.setCustomValidity('');
spec.assertSubmitPasses();
});
it('synchronous form validation', () => {
spec.setProperties({ required: true }, false);
expect(spec.form.checkValidity()).to.be.false;
spec.assertSubmitFails();
spec.reset();
spec.setProperties({ value: today.native }, false);
expect(spec.form.checkValidity()).to.be.true;
spec.assertSubmitPasses();
});
});
describe('defaultValue', () => {
const today = CalendarDay.today;
describe('Form integration', () => {
const spec = createFormAssociatedTestBed<IgcDatePickerComponent>(html`
<igc-date-picker
name="datePicker"
.defaultValue=${today.native}
></igc-date-picker>
`);
beforeEach(async () => {
await spec.setup(IgcDatePickerComponent.tagName);
});
it('correct initial state', () => {
spec.assertIsPristine();
checkDatesEqual(spec.element.value!, today);
});
it('is correctly submitted', () => {
spec.assertSubmitHasValue(today.native.toISOString());
});
it('is correctly reset', () => {
spec.setProperties({ value: today.add('day', 1).native });
spec.reset();
checkDatesEqual(spec.element.value!, today);
});
});
describe('Validation', () => {
const spec = createFormAssociatedTestBed<IgcDatePickerComponent>(html`
<igc-date-picker
name="datePicker"
.defaultValue=${null}
></igc-date-picker>
`);
beforeEach(async () => {
await spec.setup(IgcDatePickerComponent.tagName);
});
it('fails required validation', () => {
spec.setProperties({ required: true });
spec.assertIsPristine();
spec.assertSubmitFails();
});
it('passes required validation when updating defaultValue', () => {
spec.setProperties({ required: true, defaultValue: today.native });
spec.assertIsPristine();
spec.assertSubmitPasses();
});
it('fails min validation', () => {
spec.setProperties({
min: today.native,
defaultValue: today.add('day', -1).native,
});
spec.assertIsPristine();
spec.assertSubmitFails();
});
it('passes min validation', () => {
spec.setProperties({ min: today.native, defaultValue: today.native });
spec.assertIsPristine();
spec.assertSubmitPasses();
});
it('fails max validation', () => {
spec.setProperties({
max: today.native,
defaultValue: today.add('day', 1).native,
});
spec.assertIsPristine();
spec.assertSubmitFails();
});
it('passes max validation', () => {
spec.setProperties({
max: today.native,
defaultValue: today.native,
});
spec.assertIsPristine();
spec.assertSubmitPasses();
});
it('fails for range constraints', () => {
const minDate = new Date(2024, 1, 1);
const maxDate = new Date(2024, 1, 28);
const disabledDates: DateRangeDescriptor[] = [
{
type: DateRangeType.Between,
dateRange: [minDate, maxDate],
},
];
spec.setProperties({
disabledDates,
defaultValue: new Date(2024, 1, 28),
});
spec.assertIsPristine();
spec.assertSubmitFails();
});
it('passes for range constraints', () => {
const minDate = new Date(2024, 1, 1);
const maxDate = new Date(2024, 1, 28);
const disabledDates: DateRangeDescriptor[] = [
{
type: DateRangeType.Between,
dateRange: [minDate, maxDate],
},
];
spec.setProperties({
disabledDates,
defaultValue: new Date(2024, 1, 29),
});
spec.assertIsPristine();
spec.assertSubmitPasses();
});
});
});
describe('Validation message slots', () => {
it('', () => {
const now = CalendarDay.today;
const tomorrow = now.add('day', 1);
const yesterday = now.add('day', -1);
const testParameters: ValidationContainerTestsParams<IgcDatePickerComponent>[] =
[
{ slots: ['valueMissing'], props: { required: true } }, // value-missing slot
{
slots: ['rangeOverflow'],
props: { value: now.native, max: yesterday.native }, // range-overflow slot
},
{
slots: ['rangeUnderflow'],
props: { value: now.native, min: tomorrow.native }, // range-underflow slot
},
{
slots: ['badInput'],
props: {
value: now.native,
disabledDates: [
{
type: DateRangeType.Between,
dateRange: [yesterday.native, tomorrow.native], // bad-input slot
},
],
},
},
{ slots: ['customError'] }, // custom-error slot
{ slots: ['invalid'], props: { required: true } }, // invalid slot
];
runValidationContainerTests(IgcDatePickerComponent, testParameters);
});
});
});