import { type E2EElement, type E2EPage, newE2EPage } from '@stencil/core/testing'; describe('ods-switch rendering', () => { let el: E2EElement; let page: E2EPage; let switchItemLabel: E2EElement; async function isInErrorState(): Promise { return await page.evaluate(() => { return document.querySelector('ods-switch')?.classList.contains('ods-switch--error'); }); } async function setup(content: string, customStyle?: string): Promise { page = await newE2EPage(); await page.setContent(content); await page.evaluate(() => document.body.style.setProperty('margin', '0')); await page.waitForChanges(); if (customStyle) { await page.addStyleTag({ content: customStyle }); } el = await page.find('ods-switch'); switchItemLabel = await page.find('ods-switch-item > .ods-switch-item__label'); } it('should render the web component', async() => { await setup(''); expect(el.shadowRoot).not.toBeNull(); }); describe('isDisabled', () => { it('should render with isDisabled', async() => { await setup(` label1 label2 label3 `); expect(switchItemLabel?.classList.contains('ods-switch-item__label--disabled')).toBe(true); }); it('should render without isDisabled', async() => { await setup(` label1 label2 label3 `); expect(switchItemLabel?.classList.contains('ods-switch-item__label--disabled')).toBe(false); }); }); describe('size', () => { it('should render with default size', async() => { await setup(` label1 label2 label3 `); expect(el?.classList.contains('ods-switch--md')).toBe(true); }); it('should render with size set', async() => { await setup(` label1 label2 label3 `); expect(el?.classList.contains('ods-switch--sm')).toBe(true); }); }); describe('error state', () => { it('should enforce the error state if has-error is set even on valid switch', async() => { await setup(`
label1 label2 label3
`); await page.waitForChanges(); expect(await isInErrorState()).toBe(true); await page.evaluate(() => { document.querySelector('form')?.requestSubmit(); }); await page.waitForChanges(); expect(await isInErrorState()).toBe(true); }); }); });