import { render } from '@stencil/vitest';
describe('ontario-checkbox', () => {
it('renders', async () => {
const page = await render(``);
expect(page.root).toEqualHtml(`
`);
});
it('should keep the host value in sync with checked checkbox options', async () => {
const page = await render(
``,
);
expect(page.root?.value).toEqual(['checkbox-option-1', 'checkbox-option-2']);
});
it('should apply the provided value over checked option flags', async () => {
const page = await render(
``,
);
const checkboxOne = page.root?.shadowRoot?.querySelector('#checkbox-1') as HTMLInputElement;
const checkboxTwo = page.root?.shadowRoot?.querySelector('#checkbox-2') as HTMLInputElement;
expect(page.root?.value).toEqual(['checkbox-option-2']);
expect(checkboxOne.checked).toBe(false);
expect(checkboxTwo.checked).toBe(true);
});
it('should emit a host change event with the current value in detail', async () => {
const page = await render(
``,
);
const onChange = vi.fn();
page.root?.addEventListener('change', onChange);
const checkbox = page.root?.shadowRoot?.querySelector('#checkbox-2') as HTMLInputElement;
checkbox.checked = true;
checkbox.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
await page.waitForChanges();
expect(onChange).toHaveBeenCalledTimes(1);
expect(page.root?.value).toEqual(['checkbox-option-2']);
expect(onChange.mock.calls[0][0].detail.value).toEqual(['checkbox-option-2']);
});
it('should preserve the checkboxOnChange custom event detail', async () => {
const page = await render(
``,
);
const onCheckboxChange = vi.fn();
document.addEventListener('checkboxOnChange', onCheckboxChange);
const checkbox = page.root?.shadowRoot?.querySelector('#checkbox-2') as HTMLInputElement;
checkbox.checked = true;
checkbox.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
await page.waitForChanges();
expect(onCheckboxChange).toHaveBeenCalledTimes(1);
expect(onCheckboxChange.mock.calls[0][0].detail).toEqual({
checked: true,
id: 'checkbox-2',
value: 'checkbox-option-2',
});
});
it('should update the host value across multiple checkbox toggles', async () => {
const page = await render(
``,
);
const checkboxOne = page.root?.shadowRoot?.querySelector('#checkbox-1') as HTMLInputElement;
const checkboxTwo = page.root?.shadowRoot?.querySelector('#checkbox-2') as HTMLInputElement;
checkboxOne.checked = true;
checkboxOne.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
await page.waitForChanges();
expect(page.root?.value).toEqual(['checkbox-option-1']);
checkboxTwo.checked = true;
checkboxTwo.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
await page.waitForChanges();
expect(page.root?.value).toEqual(['checkbox-option-1', 'checkbox-option-2']);
checkboxOne.checked = false;
checkboxOne.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
await page.waitForChanges();
expect(page.root?.value).toEqual(['checkbox-option-2']);
});
it('should warn and ignore provided values that do not match an option', async () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
const page = await render(
``,
);
expect(page.root?.value).toEqual(['checkbox-option-2']);
expect(warnSpy).toHaveBeenCalled();
warnSpy.mockRestore();
});
it('should reflect external value updates in the rendered checkboxes', async () => {
const page = await render(
``,
);
const checkboxOne = page.root?.shadowRoot?.querySelector('#checkbox-1') as HTMLInputElement;
const checkboxTwo = page.root?.shadowRoot?.querySelector('#checkbox-2') as HTMLInputElement;
expect(checkboxOne.checked).toBe(true);
expect(checkboxTwo.checked).toBe(false);
(page.root as HTMLOntarioCheckboxesElement).value = ['checkbox-option-2'];
await page.waitForChanges();
expect(page.root?.value).toEqual(['checkbox-option-2']);
expect(checkboxOne.checked).toBe(false);
expect(checkboxTwo.checked).toBe(true);
});
});