chore(labs): fix onReportValidity called twice with form.reportValidity

PiperOrigin-RevId: 597661314
This commit is contained in:
Elizabeth Mitchell 2024-01-11 14:53:05 -08:00 committed by Copybara-Service
parent 5c22b2baea
commit f2ebcdac79
2 changed files with 15 additions and 16 deletions

View File

@ -201,11 +201,10 @@ export function mixinOnReportValidity<
reportedInvalidEventFromForm = false; reportedInvalidEventFromForm = false;
this.addEventListener( this.addEventListener(
'invalid', 'invalid',
(invalidEvent) => { () => {
reportedInvalidEventFromForm = true; reportedInvalidEventFromForm = true;
if (!invalidEvent.defaultPrevented) { // Constructor's invalid listener will handle reporting invalid
this[onReportValidity](invalidEvent); // events.
}
}, },
{signal: formReportValidityCleanup.signal}, {signal: formReportValidityCleanup.signal},
); );

View File

@ -58,7 +58,7 @@ describe('mixinOnReportValidity()', () => {
control[onReportValidity] = jasmine.createSpy('onReportValidity'); control[onReportValidity] = jasmine.createSpy('onReportValidity');
control.reportValidity(); control.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
}); });
@ -69,7 +69,7 @@ describe('mixinOnReportValidity()', () => {
control.required = true; control.required = true;
control.reportValidity(); control.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith( expect(control[onReportValidity]).toHaveBeenCalledOnceWith(
jasmine.any(Event), jasmine.any(Event),
); );
}); });
@ -101,7 +101,7 @@ describe('mixinOnReportValidity()', () => {
control.checked = true; control.checked = true;
control.reportValidity(); control.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
}); });
@ -114,7 +114,7 @@ describe('mixinOnReportValidity()', () => {
form.appendChild(control); form.appendChild(control);
form.reportValidity(); form.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
it('should be called with null when form.requestSubmit() is called and it is valid', () => { it('should be called with null when form.requestSubmit() is called and it is valid', () => {
@ -134,7 +134,7 @@ describe('mixinOnReportValidity()', () => {
document.body.appendChild(form); document.body.appendChild(form);
form.requestSubmit(); form.requestSubmit();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
it('should be called with null when form submits declaratively and it is valid', () => { it('should be called with null when form submits declaratively and it is valid', () => {
@ -156,7 +156,7 @@ describe('mixinOnReportValidity()', () => {
document.body.appendChild(form); document.body.appendChild(form);
submitButton.click(); submitButton.click();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
}); });
@ -169,7 +169,7 @@ describe('mixinOnReportValidity()', () => {
control.required = true; control.required = true;
form.reportValidity(); form.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith( expect(control[onReportValidity]).toHaveBeenCalledOnceWith(
jasmine.any(Event), jasmine.any(Event),
); );
}); });
@ -209,7 +209,7 @@ describe('mixinOnReportValidity()', () => {
control.required = true; control.required = true;
form.requestSubmit(); form.requestSubmit();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith( expect(control[onReportValidity]).toHaveBeenCalledOnceWith(
jasmine.any(Event), jasmine.any(Event),
); );
}); });
@ -263,7 +263,7 @@ describe('mixinOnReportValidity()', () => {
document.body.appendChild(form); document.body.appendChild(form);
submitButton.click(); submitButton.click();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith( expect(control[onReportValidity]).toHaveBeenCalledOnceWith(
jasmine.any(Event), jasmine.any(Event),
); );
}); });
@ -315,7 +315,7 @@ describe('mixinOnReportValidity()', () => {
form.reportValidity(); form.reportValidity();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
it('should be called with null when form.requestSubmit() is called after fixing invalid', () => { it('should be called with null when form.requestSubmit() is called after fixing invalid', () => {
@ -345,7 +345,7 @@ describe('mixinOnReportValidity()', () => {
form.requestSubmit(); form.requestSubmit();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
it('should be called with null when form submits declaratively after fixing invalid', () => { it('should be called with null when form submits declaratively after fixing invalid', () => {
@ -377,7 +377,7 @@ describe('mixinOnReportValidity()', () => {
submitButton.click(); submitButton.click();
form.remove(); form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null); expect(control[onReportValidity]).toHaveBeenCalledOnceWith(null);
}); });
}); });