import { expect, fixture, html, oneEvent, waitUntil } from '@open-wc/testing'; import { sendKeys } from '@web/test-runner-commands'; import sinon from 'sinon'; import type SlRadioGroup from '~/components/radio-group/radio-group'; import type SlRadioButton from './radio-button'; describe('', () => { it('should be disabled with the disabled attribute', async () => { const el = await fixture(html` `); expect(el.input.disabled).to.be.true; }); it('should be valid by default', async () => { const el = await fixture(html` `); expect(el.invalid).to.be.false; }); it('should fire sl-change when clicked', async () => { const el = await fixture(html` `); setTimeout(() => el.input.click()); const event = await oneEvent(el, 'sl-change'); expect(event.target).to.equal(el); expect(el.checked).to.be.true; }); it('should fire sl-change when toggled via keyboard - space', async () => { const el = await fixture(html` `); el.input.focus(); setTimeout(() => sendKeys({ press: ' ' })); const event = await oneEvent(el, 'sl-change'); expect(event.target).to.equal(el); expect(el.checked).to.be.true; }); it('should fire sl-change when toggled via keyboard - arrow key', async () => { const radioGroup = await fixture(html` `); const radio1 = radioGroup.querySelector('#radio-1')!; const radio2 = radioGroup.querySelector('#radio-2')!; radio1.input.focus(); setTimeout(() => sendKeys({ press: 'ArrowRight' })); const event = await oneEvent(radio2, 'sl-change'); expect(event.target).to.equal(radio2); expect(radio2.checked).to.be.true; }); it('should not get checked when disabled', async () => { const radioGroup = await fixture(html` `); const radio1 = radioGroup.querySelector('sl-radio-button[checked]')!; const radio2 = radioGroup.querySelector('sl-radio-button[disabled]')!; radio2.click(); await Promise.all([radio1.updateComplete, radio2.updateComplete]); expect(radio1.checked).to.be.true; expect(radio2.checked).to.be.false; }); describe('when submitting a form', () => { it('should submit the correct value', async () => { const form = await fixture(html`
Submit
`); const button = form.querySelector('sl-button')!; const radio = form.querySelectorAll('sl-radio-button')[1]!; const submitHandler = sinon.spy((event: SubmitEvent) => { formData = new FormData(form); event.preventDefault(); }); let formData: FormData; form.addEventListener('submit', submitHandler); radio.click(); button.click(); await waitUntil(() => submitHandler.calledOnce); expect(formData!.get('a')).to.equal('2'); }); }); });