import { expect, fixture, nextFrame, html } from '@open-wc/testing'; import { Required } from '@lion/validate'; import '@lion/radio/lion-radio.js'; import '../lion-radio-group.js'; describe('', () => { it('has a single checkedValue representing the currently checked radio value', async () => { const el = await fixture(html` `); await nextFrame(); expect(el.checkedValue).to.equal('female'); el.formElementsArray[0].checked = true; expect(el.checkedValue).to.equal('male'); el.formElementsArray[2].checked = true; expect(el.checkedValue).to.equal('alien'); }); it('can handle complex data via choiceValue', async () => { const date = new Date(2018, 11, 24, 10, 33, 30, 0); const el = await fixture(html` `); await nextFrame(); expect(el.checkedValue).to.equal(date); el.formElementsArray[0].checked = true; expect(el.checkedValue).to.deep.equal({ some: 'data' }); }); it('can handle 0 and empty string as valid values ', async () => { const el = await fixture(html` `); await nextFrame(); expect(el.checkedValue).to.equal(0); el.formElementsArray[1].checked = true; expect(el.checkedValue).to.equal(''); }); it('still has a full modelValue ', async () => { const el = await fixture(html` `); await nextFrame(); expect(el.modelValue).to.deep.equal({ 'gender[]': [ { value: 'male', checked: false }, { value: 'female', checked: true }, { value: 'alien', checked: false }, ], }); }); it('can check a radio by supplying an available checkedValue', async () => { const el = await fixture(html` `); await nextFrame(); expect(el.checkedValue).to.equal('female'); el.checkedValue = 'alien'; expect(el.formElementsArray[2].checked).to.be.true; }); it('fires checked-value-changed event only once per checked change', async () => { let counter = 0; const el = await fixture(html` { counter += 1; }} > `); await nextFrame(); expect(counter).to.equal(0); el.formElementsArray[0].checked = true; expect(counter).to.equal(1); // not changed values trigger no event el.formElementsArray[0].checked = true; expect(counter).to.equal(1); el.formElementsArray[2].checked = true; expect(counter).to.equal(2); // not found values trigger no event el.checkedValue = 'foo'; expect(counter).to.equal(2); el.checkedValue = 'male'; expect(counter).to.equal(3); }); it('expect child nodes to only fire one model-value-changed event per instance', async () => { let counter = 0; const el = await fixture(html` { counter += 1; }} > `); await nextFrame(); counter = 0; // reset after setup which may result in different results el.formElementsArray[0].checked = true; expect(counter).to.equal(2); // male becomes checked, female becomes unchecked // not changed values trigger no event el.formElementsArray[0].checked = true; expect(counter).to.equal(2); el.formElementsArray[2].checked = true; expect(counter).to.equal(4); // alien becomes checked, male becomes unchecked // not found values trigger no event el.checkedValue = 'foo'; expect(counter).to.equal(4); el.checkedValue = 'male'; expect(counter).to.equal(6); // male becomes checked, alien becomes unchecked }); it('allows selection of only one radio in a named group', async () => { const el = await fixture(html` `); await nextFrame(); const male = el.formElements['gender[]'][0]; const maleInput = male.querySelector('input'); const female = el.formElements['gender[]'][1]; const femaleInput = female.querySelector('input'); expect(male.checked).to.equal(false); expect(female.checked).to.equal(false); maleInput.focus(); maleInput.click(); expect(male.checked).to.equal(true); expect(female.checked).to.equal(false); femaleInput.focus(); femaleInput.click(); expect(male.checked).to.equal(false); expect(female.checked).to.equal(true); }); it('should have role = radiogroup', async () => { const el = await fixture(html` `); await nextFrame(); expect(el.getAttribute('role')).to.equal('radiogroup'); }); it('can be required', async () => { const el = await fixture(html` `); expect(el.hasFeedbackFor).to.include('error'); expect(el.validationStates).to.have.a.property('error'); expect(el.validationStates.error).to.have.a.property('Required'); el.formElements['gender[]'][0].checked = true; expect(el.hasFeedbackFor).not.to.include('error'); expect(el.validationStates).to.have.a.property('error'); expect(el.validationStates.error).not.to.have.a.property('Required'); el.formElements['gender[]'][1].checked = true; expect(el.hasFeedbackFor).not.to.include('error'); expect(el.validationStates).to.have.a.property('error'); expect(el.validationStates.error).not.to.have.a.property('Required'); }); it('returns serialized value', async () => { const el = await fixture(html` `); el.formElements['gender[]'][0].checked = true; expect(el.serializedValue).to.deep.equal({ checked: true, value: 'male' }); }); it('returns serialized value on unchecked state', async () => { const el = await fixture(html` `); await nextFrame(); expect(el.serializedValue).to.deep.equal(''); }); it(`becomes "touched" once a single element of the group changes`, async () => { const el = await fixture(html` `); await nextFrame(); el.children[1].focus(); expect(el.touched).to.equal(false, 'initially, touched state is false'); el.children[1].checked = true; expect(el.touched, `focused via a mouse click, group should be touched`).to.be.true; }); });