mirror of
https://github.com/material-components/material-web.git
synced 2024-09-11 13:46:10 +03:00
test(labs): re-organize report validity tests for unhandled use cases
PiperOrigin-RevId: 597596342
This commit is contained in:
parent
73725be670
commit
e44f6a5a7f
@ -52,137 +52,147 @@ describe('mixinOnReportValidity()', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('[onReportValidity]', () => {
|
describe('[onReportValidity]', () => {
|
||||||
it('should be called with event when reportValidity() is called and it is invalid', () => {
|
describe('for valid controls', () => {
|
||||||
const control = new TestOnReportValidity();
|
it('should be called with null when reportValidity() is called and it is valid', () => {
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
const control = new TestOnReportValidity();
|
||||||
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
|
|
||||||
control.required = true;
|
control.reportValidity();
|
||||||
control.reportValidity();
|
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
|
||||||
expect(control[onReportValidity]).toHaveBeenCalledWith(
|
});
|
||||||
jasmine.any(Event),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT be called when reportValidity() is called and invalid but default prevented', () => {
|
describe('for invalid controls', () => {
|
||||||
const control = new TestOnReportValidity();
|
it('should be called with event when reportValidity() is called and it is invalid', () => {
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
const control = new TestOnReportValidity();
|
||||||
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
|
|
||||||
control.required = true;
|
control.required = true;
|
||||||
control.addEventListener('invalid', (event) => {
|
control.reportValidity();
|
||||||
event.preventDefault();
|
expect(control[onReportValidity]).toHaveBeenCalledWith(
|
||||||
|
jasmine.any(Event),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
control.reportValidity();
|
it('should NOT be called when reportValidity() is called and invalid but default prevented', () => {
|
||||||
expect(control[onReportValidity]).not.toHaveBeenCalled();
|
const control = new TestOnReportValidity();
|
||||||
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
|
|
||||||
|
control.required = true;
|
||||||
|
control.addEventListener('invalid', (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
});
|
||||||
|
|
||||||
|
control.reportValidity();
|
||||||
|
expect(control[onReportValidity]).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be called with null when reportValidity() is called and it is valid', () => {
|
describe('with forms', () => {
|
||||||
const control = new TestOnReportValidity();
|
describe('for valid controls', () => {
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
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);
|
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be called with event when form.reportValidity() is called and it is invalid', () => {
|
it('should be called with null when form.requestSubmit() is called and it is valid', () => {
|
||||||
const control = new TestOnReportValidity();
|
const control = new TestOnReportValidity();
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
const form = document.createElement('form');
|
const form = document.createElement('form');
|
||||||
form.appendChild(control);
|
form.appendChild(control);
|
||||||
|
form.addEventListener(
|
||||||
|
'submit',
|
||||||
|
(event) => {
|
||||||
|
// Prevent the test page from actually reloading
|
||||||
|
event.preventDefault();
|
||||||
|
},
|
||||||
|
{capture: true},
|
||||||
|
);
|
||||||
|
|
||||||
control.required = true;
|
document.body.appendChild(form);
|
||||||
form.reportValidity();
|
form.requestSubmit();
|
||||||
expect(control[onReportValidity]).toHaveBeenCalledWith(
|
form.remove();
|
||||||
jasmine.any(Event),
|
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
|
||||||
);
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('should NOT be called when form.reportValidity() is called and invalid but default prevented', () => {
|
|
||||||
const control = new TestOnReportValidity();
|
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
|
||||||
const form = document.createElement('form');
|
|
||||||
form.appendChild(control);
|
|
||||||
|
|
||||||
control.required = true;
|
|
||||||
control.addEventListener('invalid', (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
form.reportValidity();
|
describe('for valid to invalid controls', () => {
|
||||||
expect(control[onReportValidity]).not.toHaveBeenCalled();
|
it('should be called with event when form.reportValidity() is called and it is invalid', () => {
|
||||||
});
|
const control = new TestOnReportValidity();
|
||||||
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
|
const form = document.createElement('form');
|
||||||
|
form.appendChild(control);
|
||||||
|
|
||||||
it('should be called with null when form.reportValidity() is called and it is valid', () => {
|
control.required = true;
|
||||||
const control = new TestOnReportValidity();
|
form.reportValidity();
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
expect(control[onReportValidity]).toHaveBeenCalledWith(
|
||||||
const form = document.createElement('form');
|
jasmine.any(Event),
|
||||||
form.appendChild(control);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
form.reportValidity();
|
it('should NOT be called when form.reportValidity() is called and invalid but default prevented', () => {
|
||||||
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
|
const control = new TestOnReportValidity();
|
||||||
});
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
|
const form = document.createElement('form');
|
||||||
|
form.appendChild(control);
|
||||||
|
|
||||||
it('should be called with null when form submits', () => {
|
control.required = true;
|
||||||
const control = new TestOnReportValidity();
|
control.addEventListener('invalid', (event) => {
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
event.preventDefault();
|
||||||
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.reportValidity();
|
||||||
form.requestSubmit();
|
expect(control[onReportValidity]).not.toHaveBeenCalled();
|
||||||
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();
|
const control = new TestOnReportValidity();
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
const form = document.createElement('form');
|
const form = document.createElement('form');
|
||||||
form.appendChild(control);
|
form.appendChild(control);
|
||||||
form.addEventListener(
|
form.addEventListener(
|
||||||
'submit',
|
'submit',
|
||||||
(event) => {
|
(event) => {
|
||||||
// Prevent the test page from actually reloading. This shouldn't
|
// Prevent the test page from actually reloading. This shouldn't
|
||||||
// happen, but we add it just in case the control fails and reports
|
// happen, but we add it just in case the control fails and reports
|
||||||
// as valid and the form tries to submit.
|
// as valid and the form tries to submit.
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
},
|
},
|
||||||
{capture: true},
|
{capture: true},
|
||||||
);
|
);
|
||||||
|
|
||||||
document.body.appendChild(form);
|
document.body.appendChild(form);
|
||||||
control.required = true;
|
control.required = true;
|
||||||
form.requestSubmit();
|
form.requestSubmit();
|
||||||
form.remove();
|
form.remove();
|
||||||
expect(control[onReportValidity]).toHaveBeenCalledWith(
|
expect(control[onReportValidity]).toHaveBeenCalledWith(
|
||||||
jasmine.any(Event),
|
jasmine.any(Event),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should clean up when form is unassociated and not call when non-parent form.reportValidity() is called', () => {
|
it('should clean up when form is unassociated and not call when non-parent form.reportValidity() is called', () => {
|
||||||
const control = new TestOnReportValidity();
|
const control = new TestOnReportValidity();
|
||||||
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
control[onReportValidity] = jasmine.createSpy('onReportValidity');
|
||||||
const form = document.createElement('form');
|
const form = document.createElement('form');
|
||||||
form.appendChild(control);
|
form.appendChild(control);
|
||||||
|
|
||||||
form.reportValidity();
|
form.reportValidity();
|
||||||
expect(control[onReportValidity])
|
expect(control[onReportValidity])
|
||||||
.withContext('onReportValidity is called once while attached to form')
|
.withContext('onReportValidity is called once while attached to form')
|
||||||
.toHaveBeenCalledTimes(1);
|
.toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
form.removeChild(control);
|
form.removeChild(control);
|
||||||
form.reportValidity();
|
form.reportValidity();
|
||||||
expect(control[onReportValidity])
|
expect(control[onReportValidity])
|
||||||
.withContext('onReportValidity is not called a second time')
|
.withContext('onReportValidity is not called a second time')
|
||||||
.toHaveBeenCalledTimes(1);
|
.toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user