import type { InitialTemplateAttributes } from '@microsoft/fast-test-harness/fixtures/csr-fixture.js'; import { expect, test } from '../../test/playwright/index.js'; import { tagName } from './button.options.js'; test.describe('Button', () => { test.use({ tagName, innerHTML: 'Button', }); test('should create with document.createElement()', async ({ page, fastPage }) => { await fastPage.setTemplate(); let hasError = false; page.on('pageerror', () => { hasError = true; }); await page.evaluate(tagName => { document.createElement(tagName); }, tagName); expect(hasError).toBe(false); }); test('should NOT submit the parent form when clicked and `type` attribute is not set', async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName}>Button
`); await element.click(); await expect(page).not.toHaveURL(/foo/); }); test('should NOT submit the parent form when clicked and `type` attribute is set to "button"', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="button">Button
`); await element.click(); await expect(page).not.toHaveURL(/foo/); }); test('should NOT submit the parent form when clicked and `type` attribute is set to "reset"', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="reset">Button
`); await element.click(); await expect(page).not.toHaveURL(/foo/); }); test("should submit the form with the submit button's name and value when clicked", async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" name="bar" value="baz">Button
`); await element.click(); await expect(page).toHaveURL(/foo\?bar=baz$/); }); test('should NOT submit a value when the `name` attribute is NOT set and the `value` attribute is set', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" value="baz">Button
`); await element.click(); await expect(page).toHaveURL(/foo\?$/); }); test('should be focusable by default', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate(); await element.focus(); await expect(element).toBeFocused(); }); test('should NOT be focusable when the `disabled` attribute is present', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate({ attributes: { disabled: true } }); await element.focus(); await expect(element).not.toBeFocused(); }); test('should apply transparency correctly when the `disabled` attribute is present', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate({ attributes: { disabled: true } }); const transparent = 'rgba(0, 0, 0, 0)'; await expect(element).not.toHaveCSS('border-color', transparent); await expect(element).not.toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'primary')); await expect(element).toHaveCSS('border-color', transparent); await expect(element).not.toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'secondary')); await expect(element).not.toHaveCSS('border-color', transparent); await expect(element).not.toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'outline')); await expect(element).not.toHaveCSS('border-color', transparent); await expect(element).toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'subtle')); await expect(element).toHaveCSS('border-color', transparent); await expect(element).toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'transparent')); await expect(element).toHaveCSS('border-color', transparent); await expect(element).toHaveCSS('background-color', transparent); }); test('should be focusable when the `disabled-focusable` attribute is present', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate({ attributes: { 'disabled-focusable': true } }); await element.focus(); await expect(element).toBeFocused(); }); test('should apply transparency correctly when the `disabled-focusable` attribute is present', async ({ fastPage, }) => { const { element } = fastPage; await fastPage.setTemplate({ attributes: { 'disabled-focusable': true } }); const transparent = 'rgba(0, 0, 0, 0)'; await expect(element).not.toHaveCSS('border-color', transparent); await expect(element).not.toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'primary')); await expect(element).toHaveCSS('border-color', transparent); await expect(element).not.toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'secondary')); await expect(element).not.toHaveCSS('border-color', transparent); await expect(element).not.toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'outline')); await expect(element).not.toHaveCSS('border-color', transparent); await expect(element).toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'subtle')); await expect(element).toHaveCSS('border-color', transparent); await expect(element).toHaveCSS('background-color', transparent); await element.evaluate(node => node.setAttribute('appearance', 'transparent')); await expect(element).toHaveCSS('border-color', transparent); await expect(element).toHaveCSS('background-color', transparent); }); test('should NOT be clickable when the `disabled` attribute is present', async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate({ attributes: { disabled: true } }); const wasNotClicked = await page.evaluate(el => { const event = new KeyboardEvent('click', { bubbles: true, cancelable: true, view: window, }); // The return value of dispatchEvent will be false if any event listener called preventDefault, or true otherwise. return el?.dispatchEvent(event); }, await element.elementHandle()); expect(wasNotClicked).toEqual(true); }); for (const key of ['Enter', ' ']) { test(`should NOT be actionable with \`${key}\` keypress when the \`disabled-focusable\` attribute is present`, async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate({ attributes: { 'disabled-focusable': true } }); await element.focus(); const isActionable = await page.evaluate( ([el, key]) => { const event = new KeyboardEvent('keypress', { key: key as string, bubbles: true, cancelable: true, view: window, }); // The return value of dispatchEvent will be false if any event listener called preventDefault, or true otherwise. return (el as HTMLElement).dispatchEvent(event); }, [await element.elementHandle(), key], ); expect(isActionable).toBe(false); }); } test('should NOT receive focus when the `tabindex` is manually set to -1', async ({ fastPage, page }) => { const { element } = fastPage; const focusable = element.nth(0); const notFocusable = element.nth(1); await fastPage.setTemplate(/* html */ ` <${tagName}>Recieves Focus <${tagName} tabindex="-1">Not Focusable `); await focusable.focus(); await expect(focusable).toBeFocused(); await focusable.press('Tab'); await expect(notFocusable).not.toBeFocused(); }); test('should focus the element when the `autofocus` attribute is present', async ({ fastPage, ssr }) => { const { element } = fastPage; const attributes: InitialTemplateAttributes = { autofocus: true }; if (ssr) { // the host element needs to be focusable for autofocus to work on the server, so we need to set tabindex="0" attributes.tabindex = '0'; } await fastPage.setTemplate({ attributes }); await expect(element).toBeFocused(); }); test('should submit the parent form when clicked and `type` attribute is set to "submit"', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit">Submit Button
`); await element.click(); await expect(fastPage.page).toHaveURL(/foo/); }); test('should reset the parent form when clicked and `type` attribute is set to "reset"', async ({ fastPage, page, }) => { const { element } = fastPage; const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
<${tagName} type="reset">Reset Button
`); await expect(input).toHaveValue(''); await input.fill('foo'); await expect(input).toHaveValue('foo'); await element.click(); await expect(input).toHaveValue(''); }); test('should NOT reset the parent form when the `type` attribute is set to "reset" and the `disabled` attribute is present', async ({ fastPage, page, }) => { const { element } = fastPage; const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
<${tagName} type="reset" disabled>Reset Button
`); await input.fill('foo'); await element.click(); await expect(input).toHaveValue('foo'); }); test('should do nothing when clicked while not in a form and the `type` attribute is set to "submit"', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
Unrelated Form
<${tagName} type="submit">Submit Button `); await element.click(); await expect(page).not.toHaveURL(/foo/); }); test('should do nothing when clicked while not in a form and the `type` attribute is set to "reset"', async ({ fastPage, page, }) => { const { element } = fastPage; const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
Unrelated Form
<${tagName} type="reset">Submit Button `); await expect(input).toHaveValue(''); await input.fill('foo'); await expect(input).toHaveValue('foo'); await element.click(); await expect(input).toHaveValue('foo'); await expect(page).not.toHaveURL(/foo/); }); test('should NOT submit the parent form when clicked and `disabled` attribute is present', async ({ fastPage }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" disabled>Submit Button
`); await element.click(); await expect(fastPage.page).not.toHaveURL(/foo/); }); test('should submit the parent form when clicked and the `form` attribute is provided', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="testform">Submit Button `); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo/); }); test('should override the form action when the `formaction` attribute is provided', async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" formaction="bar">Submit Button
`); await element.click(); await expect(page).not.toHaveURL(/foo/); await expect(page).toHaveURL(/bar/); }); test('should override the action of the referenced form when the `formaction` and `form` attributes are provided', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="testform" formaction="bar">Submit Button `); await element.click(); await expect(page).not.toHaveURL(/foo/); await expect(page).toHaveURL(/bar/); }); test('should override the form method when the `formmethod` attribute is provided', async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" formmethod="post">Submit Button
`); await expect(page).not.toHaveURL(/foo/); const method = page.waitForRequest(request => request.method() === 'POST'); await element.click(); await expect(method).resolves.toBeTruthy(); await expect(page).toHaveURL(/foo/); }); test('should override the method of the referenced form when the `formmethod` and `form` attributes are provided', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="testform" formmethod="post">Submit Button `); await expect(page).not.toHaveURL(/foo/); const method = page.waitForRequest(request => request.method() === 'POST'); await element.click(); await expect(method).resolves.toBeTruthy(); await expect(page).toHaveURL(/foo/); }); test('should override the form encoding when the `formenctype` attribute is provided', async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" formenctype="plain/text">Submit Button
`); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo\?testinput=hello\+world$/); }); test('should override the encoding of the referenced form when the `formenctype` and `form` attributes are provided', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="testform" formenctype="plain/text">Submit Button `); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo\?testinput=hello\+world$/); }); test('should override the form target when the `formtarget` attribute is provided', async ({ fastPage, page }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" formtarget="_self">Submit Button
`); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo/); }); test('should override the target of the referenced form when the `formtarget` and `form` attributes are provided', async ({ fastPage, page, }) => { const { element } = fastPage; await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="testform" formtarget="_self">Submit Button `); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo/); }); test('should submit the parent form when form validation errors are present and the `formnovalidate` attribute is present', async ({ fastPage, page, }) => { const form = page.locator('#test-form'); const { element } = fastPage; const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" formnovalidate>Button
`); await input.fill('foo'); const validity = await form.evaluate((node: HTMLFormElement) => node.checkValidity()); expect(validity).toBe(false); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo/); }); test('should submit the referenced form when form validation errors are present and the `formnovalidate` and `form` attributes are present', async ({ fastPage, page, }) => { const form = page.locator('#test-form'); const { element } = fastPage; const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="test-form" formnovalidate>Button `); await input.fill('foo'); expect(await form.evaluate((node: HTMLFormElement) => node.checkValidity())).toBeFalsy(); await expect(page).not.toHaveURL(/foo/); await element.click(); await expect(page).toHaveURL(/foo/); }); test('should NOT submit the parent form when form validation errors are present and the `formnovalidate` is NOT present', async ({ fastPage, page, }) => { const button = page.locator(tagName); const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
<${tagName} type="submit">Button
`); await input.fill('foo'); const wasNotSubmitted = input.evaluate( node => new Promise(resolve => { node.addEventListener('invalid', () => resolve(true)); }), ); await button.click(); await expect(wasNotSubmitted).resolves.toBeTruthy(); }); test('should NOT submit the referenced form when form validation errors are present and the `formnovalidate` attribute is NOT present and the `form` attribute is present', async ({ fastPage, page, }) => { const button = page.locator(tagName); const input = page.locator('#text-input'); await fastPage.setTemplate(/* html */ `
<${tagName} type="submit" form="test-form">Button `); await input.fill('foo'); const wasInvalid = input.evaluate( node => new Promise(resolve => { node.addEventListener('invalid', () => resolve(true)); }), ); await button.click(); await expect(wasInvalid).resolves.toBeTruthy(); }); });