Skip to content

Commit e44f6a5

Browse files
asynclizcopybara-github
authored andcommitted
test(labs): re-organize report validity tests for unhandled use cases
PiperOrigin-RevId: 597596342
1 parent 73725be commit e44f6a5

File tree

1 file changed

+128
-118
lines changed

1 file changed

+128
-118
lines changed

labs/behaviors/on-report-validity_test.ts

Lines changed: 128 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -52,137 +52,147 @@ describe('mixinOnReportValidity()', () => {
5252
}
5353

5454
describe('[onReportValidity]', () => {
55-
it('should be called with event when reportValidity() is called and it is invalid', () => {
56-
const control = new TestOnReportValidity();
57-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
58-
59-
control.required = true;
60-
control.reportValidity();
61-
expect(control[onReportValidity]).toHaveBeenCalledWith(
62-
jasmine.any(Event),
63-
);
64-
});
65-
66-
it('should NOT be called when reportValidity() is called and invalid but default prevented', () => {
67-
const control = new TestOnReportValidity();
68-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
55+
describe('for valid controls', () => {
56+
it('should be called with null when reportValidity() is called and it is valid', () => {
57+
const control = new TestOnReportValidity();
58+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
6959

70-
control.required = true;
71-
control.addEventListener('invalid', (event) => {
72-
event.preventDefault();
60+
control.reportValidity();
61+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
7362
});
74-
75-
control.reportValidity();
76-
expect(control[onReportValidity]).not.toHaveBeenCalled();
7763
});
7864

79-
it('should be called with null when reportValidity() is called and it is valid', () => {
80-
const control = new TestOnReportValidity();
81-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
82-
83-
control.reportValidity();
84-
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
85-
});
65+
describe('for invalid controls', () => {
66+
it('should be called with event when reportValidity() is called and it is invalid', () => {
67+
const control = new TestOnReportValidity();
68+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
8669

87-
it('should be called with event when form.reportValidity() is called and it is invalid', () => {
88-
const control = new TestOnReportValidity();
89-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
90-
const form = document.createElement('form');
91-
form.appendChild(control);
92-
93-
control.required = true;
94-
form.reportValidity();
95-
expect(control[onReportValidity]).toHaveBeenCalledWith(
96-
jasmine.any(Event),
97-
);
98-
});
99-
100-
it('should NOT be called when form.reportValidity() is called and invalid but default prevented', () => {
101-
const control = new TestOnReportValidity();
102-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
103-
const form = document.createElement('form');
104-
form.appendChild(control);
105-
106-
control.required = true;
107-
control.addEventListener('invalid', (event) => {
108-
event.preventDefault();
70+
control.required = true;
71+
control.reportValidity();
72+
expect(control[onReportValidity]).toHaveBeenCalledWith(
73+
jasmine.any(Event),
74+
);
10975
});
11076

111-
form.reportValidity();
112-
expect(control[onReportValidity]).not.toHaveBeenCalled();
113-
});
77+
it('should NOT be called when reportValidity() is called and invalid but default prevented', () => {
78+
const control = new TestOnReportValidity();
79+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
11480

115-
it('should be called with null when form.reportValidity() is called and it is valid', () => {
116-
const control = new TestOnReportValidity();
117-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
118-
const form = document.createElement('form');
119-
form.appendChild(control);
81+
control.required = true;
82+
control.addEventListener('invalid', (event) => {
83+
event.preventDefault();
84+
});
12085

121-
form.reportValidity();
122-
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
86+
control.reportValidity();
87+
expect(control[onReportValidity]).not.toHaveBeenCalled();
88+
});
12389
});
12490

125-
it('should be called with null when form submits', () => {
126-
const control = new TestOnReportValidity();
127-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
128-
const form = document.createElement('form');
129-
form.appendChild(control);
130-
form.addEventListener(
131-
'submit',
132-
(event) => {
133-
// Prevent the test page from actually reloading
134-
event.preventDefault();
135-
},
136-
{capture: true},
137-
);
138-
139-
document.body.appendChild(form);
140-
form.requestSubmit();
141-
form.remove();
142-
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
143-
});
91+
describe('with forms', () => {
92+
describe('for valid controls', () => {
93+
it('should be called with null when form.reportValidity() is called and it is valid', () => {
94+
const control = new TestOnReportValidity();
95+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
96+
const form = document.createElement('form');
97+
form.appendChild(control);
98+
99+
form.reportValidity();
100+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
101+
});
102+
103+
it('should be called with null when form.requestSubmit() is called and it is valid', () => {
104+
const control = new TestOnReportValidity();
105+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
106+
const form = document.createElement('form');
107+
form.appendChild(control);
108+
form.addEventListener(
109+
'submit',
110+
(event) => {
111+
// Prevent the test page from actually reloading
112+
event.preventDefault();
113+
},
114+
{capture: true},
115+
);
116+
117+
document.body.appendChild(form);
118+
form.requestSubmit();
119+
form.remove();
120+
expect(control[onReportValidity]).toHaveBeenCalledWith(null);
121+
});
122+
});
144123

145-
it('should be called with invalid event when invalid form tries to submit', () => {
146-
const control = new TestOnReportValidity();
147-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
148-
const form = document.createElement('form');
149-
form.appendChild(control);
150-
form.addEventListener(
151-
'submit',
152-
(event) => {
153-
// Prevent the test page from actually reloading. This shouldn't
154-
// happen, but we add it just in case the control fails and reports
155-
// as valid and the form tries to submit.
156-
event.preventDefault();
157-
},
158-
{capture: true},
159-
);
160-
161-
document.body.appendChild(form);
162-
control.required = true;
163-
form.requestSubmit();
164-
form.remove();
165-
expect(control[onReportValidity]).toHaveBeenCalledWith(
166-
jasmine.any(Event),
167-
);
168-
});
124+
describe('for valid to invalid controls', () => {
125+
it('should be called with event when form.reportValidity() is called and it is invalid', () => {
126+
const control = new TestOnReportValidity();
127+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
128+
const form = document.createElement('form');
129+
form.appendChild(control);
130+
131+
control.required = true;
132+
form.reportValidity();
133+
expect(control[onReportValidity]).toHaveBeenCalledWith(
134+
jasmine.any(Event),
135+
);
136+
});
137+
138+
it('should NOT be called when form.reportValidity() is called and invalid but default prevented', () => {
139+
const control = new TestOnReportValidity();
140+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
141+
const form = document.createElement('form');
142+
form.appendChild(control);
143+
144+
control.required = true;
145+
control.addEventListener('invalid', (event) => {
146+
event.preventDefault();
147+
});
148+
149+
form.reportValidity();
150+
expect(control[onReportValidity]).not.toHaveBeenCalled();
151+
});
152+
153+
it('should be called with event when form.requestSubmit() is called and it is invalid', () => {
154+
const control = new TestOnReportValidity();
155+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
156+
const form = document.createElement('form');
157+
form.appendChild(control);
158+
form.addEventListener(
159+
'submit',
160+
(event) => {
161+
// Prevent the test page from actually reloading. This shouldn't
162+
// happen, but we add it just in case the control fails and reports
163+
// as valid and the form tries to submit.
164+
event.preventDefault();
165+
},
166+
{capture: true},
167+
);
168+
169+
document.body.appendChild(form);
170+
control.required = true;
171+
form.requestSubmit();
172+
form.remove();
173+
expect(control[onReportValidity]).toHaveBeenCalledWith(
174+
jasmine.any(Event),
175+
);
176+
});
177+
});
169178

170-
it('should clean up when form is unassociated and not call when non-parent form.reportValidity() is called', () => {
171-
const control = new TestOnReportValidity();
172-
control[onReportValidity] = jasmine.createSpy('onReportValidity');
173-
const form = document.createElement('form');
174-
form.appendChild(control);
175-
176-
form.reportValidity();
177-
expect(control[onReportValidity])
178-
.withContext('onReportValidity is called once while attached to form')
179-
.toHaveBeenCalledTimes(1);
180-
181-
form.removeChild(control);
182-
form.reportValidity();
183-
expect(control[onReportValidity])
184-
.withContext('onReportValidity is not called a second time')
185-
.toHaveBeenCalledTimes(1);
179+
it('should clean up when form is unassociated and not call when non-parent form.reportValidity() is called', () => {
180+
const control = new TestOnReportValidity();
181+
control[onReportValidity] = jasmine.createSpy('onReportValidity');
182+
const form = document.createElement('form');
183+
form.appendChild(control);
184+
185+
form.reportValidity();
186+
expect(control[onReportValidity])
187+
.withContext('onReportValidity is called once while attached to form')
188+
.toHaveBeenCalledTimes(1);
189+
190+
form.removeChild(control);
191+
form.reportValidity();
192+
expect(control[onReportValidity])
193+
.withContext('onReportValidity is not called a second time')
194+
.toHaveBeenCalledTimes(1);
195+
});
186196
});
187197
});
188198
});

0 commit comments

Comments
 (0)