Skip to content

Commit 5c22b2b

Browse files
asyncLizcopybara-github
authored andcommitted
test(labs): add more report validity tests
PiperOrigin-RevId: 597598077
1 parent e44f6a5 commit 5c22b2b

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

labs/behaviors/on-report-validity_test.ts

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ describe('mixinOnReportValidity()', () => {
8888
});
8989
});
9090

91+
describe('for invalid to valid controls', () => {
92+
it('should be called with null when reportValidity() is called after fixing invalid', () => {
93+
const control = new TestOnReportValidity();
94+
const onReportValiditySpy = jasmine.createSpy('onReportValidity');
95+
control[onReportValidity] = onReportValiditySpy;
96+
control.required = true;
97+
control.reportValidity();
98+
onReportValiditySpy.calls.reset();
99+
100+
// Fix invalid
101+
control.checked = true;
102+
control.reportValidity();
103+
104+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
105+
});
106+
});
107+
91108
describe('with forms', () => {
92109
describe('for valid controls', () => {
93110
it('should be called with null when form.reportValidity() is called and it is valid', () => {
@@ -119,6 +136,28 @@ describe('mixinOnReportValidity()', () => {
119136
form.remove();
120137
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
121138
});
139+
140+
it('should be called with null when form submits declaratively and it is valid', () => {
141+
const control = new TestOnReportValidity();
142+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
143+
const form = document.createElement('form');
144+
form.appendChild(control);
145+
form.addEventListener(
146+
'submit',
147+
(event) => {
148+
// Prevent the test page from actually reloading
149+
event.preventDefault();
150+
},
151+
{capture: true},
152+
);
153+
154+
const submitButton = document.createElement('button');
155+
form.appendChild(submitButton);
156+
document.body.appendChild(form);
157+
submitButton.click();
158+
form.remove();
159+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
160+
});
122161
});
123162

124163
describe('for valid to invalid controls', () => {
@@ -174,6 +213,172 @@ describe('mixinOnReportValidity()', () => {
174213
jasmine.any(Event),
175214
);
176215
});
216+
217+
it('should NOT be called when form.requestSubmit() is called and invalid but default prevented', () => {
218+
const control = new TestOnReportValidity();
219+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
220+
const form = document.createElement('form');
221+
form.appendChild(control);
222+
form.addEventListener(
223+
'submit',
224+
(event) => {
225+
// Prevent the test page from actually reloading. This shouldn't
226+
// happen, but we add it just in case the control fails and reports
227+
// as valid and the form tries to submit.
228+
event.preventDefault();
229+
},
230+
{capture: true},
231+
);
232+
233+
control.required = true;
234+
control.addEventListener('invalid', (event) => {
235+
event.preventDefault();
236+
});
237+
238+
document.body.appendChild(form);
239+
form.requestSubmit();
240+
form.remove();
241+
expect(control[onReportValidity]).not.toHaveBeenCalled();
242+
});
243+
244+
it('should be called with event when form submits declaratively and it is invalid', () => {
245+
const control = new TestOnReportValidity();
246+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
247+
const form = document.createElement('form');
248+
form.appendChild(control);
249+
form.addEventListener(
250+
'submit',
251+
(event) => {
252+
// Prevent the test page from actually reloading. This shouldn't
253+
// happen, but we add it just in case the control fails and reports
254+
// as valid and the form tries to submit.
255+
event.preventDefault();
256+
},
257+
{capture: true},
258+
);
259+
260+
control.required = true;
261+
const submitButton = document.createElement('button');
262+
form.appendChild(submitButton);
263+
document.body.appendChild(form);
264+
submitButton.click();
265+
form.remove();
266+
expect(control[onReportValidity]).toHaveBeenCalledWith(
267+
jasmine.any(Event),
268+
);
269+
});
270+
271+
it('should NOT be called when form submits declaratively and invalid but default prevented', () => {
272+
const control = new TestOnReportValidity();
273+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
274+
const form = document.createElement('form');
275+
form.appendChild(control);
276+
form.addEventListener(
277+
'submit',
278+
(event) => {
279+
// Prevent the test page from actually reloading. This shouldn't
280+
// happen, but we add it just in case the control fails and reports
281+
// as valid and the form tries to submit.
282+
event.preventDefault();
283+
},
284+
{capture: true},
285+
);
286+
287+
control.required = true;
288+
control.addEventListener('invalid', (event) => {
289+
event.preventDefault();
290+
});
291+
292+
document.body.appendChild(form);
293+
form.requestSubmit();
294+
form.remove();
295+
expect(control[onReportValidity]).not.toHaveBeenCalled();
296+
});
297+
});
298+
299+
describe('invalid to valid', () => {
300+
it('should be called with null when form.reportValidity() is called after fixing invalid', () => {
301+
const control = new TestOnReportValidity();
302+
const onReportValiditySpy = jasmine.createSpy('onReportValidity');
303+
control[onReportValidity] = onReportValiditySpy;
304+
const form = document.createElement('form');
305+
form.appendChild(control);
306+
document.body.appendChild(form);
307+
308+
control.required = true;
309+
form.reportValidity();
310+
onReportValiditySpy.calls.reset();
311+
312+
// Fix invalid
313+
control.checked = true;
314+
315+
form.reportValidity();
316+
form.remove();
317+
318+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
319+
});
320+
321+
it('should be called with null when form.requestSubmit() is called after fixing invalid', () => {
322+
const control = new TestOnReportValidity();
323+
const onReportValiditySpy = jasmine.createSpy('onReportValidity');
324+
control[onReportValidity] = onReportValiditySpy;
325+
const form = document.createElement('form');
326+
form.appendChild(control);
327+
document.body.appendChild(form);
328+
form.addEventListener(
329+
'submit',
330+
(event) => {
331+
// Prevent the test page from actually reloading.
332+
event.preventDefault();
333+
},
334+
{capture: true},
335+
);
336+
337+
control.required = true;
338+
form.reportValidity();
339+
onReportValiditySpy.calls.reset();
340+
341+
// Fix invalid
342+
control.checked = true;
343+
344+
// Submit imperatively
345+
form.requestSubmit();
346+
form.remove();
347+
348+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
349+
});
350+
351+
it('should be called with null when form submits declaratively after fixing invalid', () => {
352+
const control = new TestOnReportValidity();
353+
const onReportValiditySpy = jasmine.createSpy('onReportValidity');
354+
control[onReportValidity] = onReportValiditySpy;
355+
const form = document.createElement('form');
356+
form.appendChild(control);
357+
const submitButton = document.createElement('button');
358+
form.appendChild(submitButton);
359+
document.body.appendChild(form);
360+
form.addEventListener(
361+
'submit',
362+
(event) => {
363+
// Prevent the test page from actually reloading.
364+
event.preventDefault();
365+
},
366+
{capture: true},
367+
);
368+
369+
control.required = true;
370+
form.reportValidity();
371+
onReportValiditySpy.calls.reset();
372+
373+
// Fix invalid
374+
control.checked = true;
375+
376+
// Submit declaratively
377+
submitButton.click();
378+
form.remove();
379+
380+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
381+
});
177382
});
178383

179384
it('should clean up when form is unassociated and not call when non-parent form.reportValidity() is called', () => {

0 commit comments

Comments
 (0)