Skip to content

Commit 6ee1982

Browse files
committed
Add tests for checkbox/radio constraint validation rendering
1 parent 36d3838 commit 6ee1982

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

test/rendering.test.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,151 @@ suite('Rendering', async () => {
263263
assert.equal(true, errorElement.hasAttribute(`data-preserve`))
264264
assert.equal("The preserved message", errorElement.textContent)
265265
})
266+
})
267+
268+
suite(`renderConstraintValidationMessageForElement: different input types`, async () => {
269+
test(`radio: required`, async () => {
270+
const container = await fixture(html`
271+
<div>
272+
<fieldset>
273+
<input type="radio" name="terms" id="terms-field-privacy" value="privacy" aria-describedby="terms-field-errors" required>
274+
<input type="radio" name="terms" id="terms-field-service" value="service" aria-describedby="terms-field-errors">
275+
<section id="terms-field-errors" data-error-container>
276+
<ul>
277+
<li data-visible data-error-type="error_1">An ad-hoc error from the initial load</li>
278+
<li data-visible data-error-type="error_2">Another ad-hoc error from the initial load</li>
279+
</ul>
280+
</section>
281+
</fieldset>
282+
283+
<template id="pf-error-list-item-template">
284+
<li><span>‼️</span> <span data-error-message></span></li>
285+
</template>
286+
</div>
287+
`);
288+
289+
assert.equal(2, document.querySelectorAll(`#terms-field-errors li`).length)
290+
291+
const privacy = document.getElementById(`terms-field-privacy`)
292+
const service = document.getElementById(`terms-field-service`)
293+
294+
assert.equal(false, privacy.validity.valid)
295+
assert.equal(false, service.validity.valid)
296+
297+
Rendering.renderConstraintValidationMessageForElement(privacy)
298+
Rendering.renderConstraintValidationMessageForElement(service)
299+
300+
assert.equal(1, document.querySelectorAll(`#terms-field-errors li`).length)
301+
302+
const errorElement = document.querySelector(`#terms-field-errors li[data-visible][data-error-type="valueMissing"]`)
303+
304+
assert.isNotNull(errorElement)
305+
assert.equal("‼️ Please select one of these options.", errorElement.textContent)
306+
307+
privacy.checked = true
308+
service.checked = true
309+
310+
Rendering.renderConstraintValidationMessageForElement(privacy)
311+
Rendering.renderConstraintValidationMessageForElement(service)
312+
assert.equal(0, document.querySelectorAll(`#terms-field-errors li`).length)
313+
})
314+
315+
test(`checkbox: multiple checkboxes, all required, all pointing to same error container`, async () => {
316+
const container = await fixture(html`
317+
<div>
318+
<fieldset>
319+
<input type="checkbox" name="terms" id="terms-field-privacy" value="privacy" aria-describedby="terms-field-errors" required>
320+
<input type="checkbox" name="terms" id="terms-field-service" value="service" aria-describedby="terms-field-errors" required>
321+
<section id="terms-field-errors" data-error-container>
322+
<ul>
323+
<li data-visible data-error-type="error_1">An ad-hoc error from the initial load</li>
324+
<li data-visible data-error-type="error_2">Another ad-hoc error from the initial load</li>
325+
</ul>
326+
</section>
327+
</fieldset>
328+
329+
<template id="pf-error-list-item-template">
330+
<li><span>‼️</span> <span data-error-message></span></li>
331+
</template>
332+
</div>
333+
`);
334+
335+
assert.equal(2, document.querySelectorAll(`#terms-field-errors li`).length)
336+
337+
const privacy = document.getElementById(`terms-field-privacy`)
338+
const service = document.getElementById(`terms-field-service`)
339+
340+
assert.equal(false, privacy.validity.valid)
341+
assert.equal(false, service.validity.valid)
342+
343+
Rendering.renderConstraintValidationMessageForElement(privacy)
344+
Rendering.renderConstraintValidationMessageForElement(service)
345+
346+
assert.equal(1, document.querySelectorAll(`#terms-field-errors li`).length)
347+
348+
const errorElement = document.querySelector(`#terms-field-errors li[data-visible][data-error-type="valueMissing"]`)
349+
350+
assert.isNotNull(errorElement)
351+
assert.equal("‼️ Please check this box if you want to proceed.", errorElement.textContent)
352+
353+
privacy.checked = true
354+
service.checked = true
355+
356+
Rendering.renderConstraintValidationMessageForElement(privacy)
357+
Rendering.renderConstraintValidationMessageForElement(service)
358+
assert.equal(0, document.querySelectorAll(`#terms-field-errors li`).length)
359+
})
360+
361+
test(`checkbox: multiple checkboxes, all required, pointing to different error container`, async () => {
362+
const container = await fixture(html`
363+
<div>
364+
<fieldset>
365+
<input type="checkbox" name="terms" id="terms-field-privacy" value="privacy" aria-describedby="terms-privacy-errors" required>
366+
<input type="checkbox" name="terms" id="terms-field-service" value="service" aria-describedby="terms-service-errors" required>
367+
<section id="terms-privacy-errors" data-error-container>
368+
<ul>
369+
<li data-visible data-error-type="error_1">An ad-hoc error from the initial load</li>
370+
<li data-visible data-error-type="error_2">Another ad-hoc error from the initial load</li>
371+
</ul>
372+
</section>
373+
374+
<section id="terms-service-errors" data-error-container>
375+
<ul>
376+
<li data-visible data-error-type="error_1">An ad-hoc error from the initial load</li>
377+
<li data-visible data-error-type="error_2">Another ad-hoc error from the initial load</li>
378+
</ul>
379+
</section>
380+
</fieldset>
381+
382+
<template id="pf-error-list-item-template">
383+
<li><span>‼️</span> <span data-error-message></span></li>
384+
</template>
385+
</div>
386+
`);
387+
388+
assert.equal(2, document.querySelectorAll(`#terms-privacy-errors li`).length)
389+
assert.equal(2, document.querySelectorAll(`#terms-service-errors li`).length)
390+
391+
const privacy = document.getElementById(`terms-field-privacy`)
392+
const service = document.getElementById(`terms-field-service`)
393+
394+
assert.equal(false, privacy.validity.valid)
395+
assert.equal(false, service.validity.valid)
396+
397+
Rendering.renderConstraintValidationMessageForElement(privacy)
398+
399+
assert.equal(1, document.querySelectorAll(`#terms-privacy-errors li`).length)
400+
assert.equal(2, document.querySelectorAll(`#terms-service-errors li`).length)
401+
402+
const errorElement = document.querySelector(`#terms-privacy-errors li[data-visible][data-error-type="valueMissing"]`)
403+
404+
assert.isNotNull(errorElement)
405+
assert.equal("‼️ Please check this box if you want to proceed.", errorElement.textContent)
406+
407+
privacy.checked = true
408+
409+
Rendering.renderConstraintValidationMessageForElement(privacy)
410+
Rendering.renderConstraintValidationMessageForElement(service)
411+
assert.equal(0, document.querySelectorAll(`#terms-privacy-errors li`).length)
412+
})
266413
})

0 commit comments

Comments
 (0)