test(labs): re-organize report validity tests for unhandled use cases

PiperOrigin-RevId: 597596342
This commit is contained in:
Elizabeth Mitchell 2024-01-11 10:41:33 -08:00 committed by Copybara-Service
parent 73725be670
commit e44f6a5a7f

View File

@ -52,6 +52,17 @@ describe('mixinOnReportValidity()', () => {
}
describe('[onReportValidity]', () => {
describe('for valid controls', () => {
it('should be called with null when reportValidity() is called and it is valid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
control.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
});
});
describe('for invalid controls', () => {
it('should be called with event when reportValidity() is called and it is invalid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
@ -75,15 +86,42 @@ describe('mixinOnReportValidity()', () => {
control.reportValidity();
expect(control[onReportValidity]).not.toHaveBeenCalled();
});
});
it('should be called with null when reportValidity() is called and it is valid', () => {
describe('with forms', () => {
describe('for valid controls', () => {
it('should be called with null when form.reportValidity() is called and it is valid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
const form = document.createElement('form');
form.appendChild(control);
control.reportValidity();
form.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
});
it('should be called with null when form.requestSubmit() is called and it is valid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
const form = document.createElement('form');
form.appendChild(control);
form.addEventListener(
'submit',
(event) => {
// Prevent the test page from actually reloading
event.preventDefault();
},
{capture: true},
);
document.body.appendChild(form);
form.requestSubmit();
form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
});
});
describe('for valid to invalid controls', () => {
it('should be called with event when form.reportValidity() is called and it is invalid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
@ -112,37 +150,7 @@ describe('mixinOnReportValidity()', () => {
expect(control[onReportValidity]).not.toHaveBeenCalled();
});
it('should be called with null when form.reportValidity() is called and it is valid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
const form = document.createElement('form');
form.appendChild(control);
form.reportValidity();
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
});
it('should be called with null when form submits', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
const form = document.createElement('form');
form.appendChild(control);
form.addEventListener(
'submit',
(event) => {
// Prevent the test page from actually reloading
event.preventDefault();
},
{capture: true},
);
document.body.appendChild(form);
form.requestSubmit();
form.remove();
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
});
it('should be called with invalid event when invalid form tries to submit', () => {
it('should be called with event when form.requestSubmit() is called and it is invalid', () => {
const control = new TestOnReportValidity();
control[onReportValidity] = jasmine.createSpy('onReportValidity');
const form = document.createElement('form');
@ -166,6 +174,7 @@ describe('mixinOnReportValidity()', () => {
jasmine.any(Event),
);
});
});
it('should clean up when form is unassociated and not call when non-parent form.reportValidity() is called', () => {
const control = new TestOnReportValidity();
@ -186,3 +195,4 @@ describe('mixinOnReportValidity()', () => {
});
});
});
});