import { newE2EPage } from '@stencil/core/testing';
const isHTML = (tagOrHTML) => tagOrHTML.trim().startsWith('<');
/** Returns element tag from given string */
const getSelector = (tagOrHTML) => {
  const closingTag = /[>\s]/;
  return isHTML(tagOrHTML) ? tagOrHTML.trim().substring(1, tagOrHTML.search(closingTag)) : tagOrHTML;
};
/** Tests if the component mounts into the page and displays as defaulted */
const renders = async (componentTagOrHTML, options = {}) => {
  var _a, _b;
  const html = isHTML(componentTagOrHTML) ? componentTagOrHTML : `<${componentTagOrHTML}></${componentTagOrHTML}>`;
  const page = await newE2EPage({ html });
  await page.waitForChanges();
  const element = await page.find(getSelector(componentTagOrHTML));
  expect(element).toHaveClass('hydrated');
  expect(await element.isVisible()).toBe((_a = options.visible) !== null && _a !== void 0 ? _a : true);
  expect((await element.getComputedStyle()).display).toBe((_b = options.display) !== null && _b !== void 0 ? _b : 'inline-flex');
};
const inputFormSubmit = async (options, componentName = 'abds-input') => {
  const page = await newE2EPage();
  const { checked = false, customValue = 'a', disabled = false, name = 'name', readonly = false, required = false, value = '', } = options;
  await page.setContent(`
    <form>
      <${componentName} ${checked ? 'checked' : ''} ${disabled ? 'disabled' : ''} name='${name}' ${readonly ? 'readonly' : ''} ${required ? 'required' : ''} value=${value ? value : ''}>label</${componentName}>
      <input type="submit" value="Submit">
    </form>
  `);
  if (componentName === 'abds-input') {
    const input = await page.find(componentName);
    await input.click();
    await page.keyboard.type(customValue);
  }
  const submit = await page.find('[type="submit"]');
  await submit.click();
  await page.waitForChanges();
  const urlAfterSubmit = page.$$eval('body', () => window.location.href);
  await page.waitForChanges();
  if (componentName === 'abds-switch' || componentName === 'abds-checkbox' || componentName === 'abds-radio') {
    if (checked) {
      expect(await urlAfterSubmit).toContain(`${name}=${value}`);
    }
    else {
      expect(await urlAfterSubmit).not.toContain(`${name}=${value}`);
    }
    return;
  }
  if (readonly && value) {
    expect(await urlAfterSubmit).toContain(`${name}=${value}`);
    return;
  }
  if (disabled) {
    expect(await urlAfterSubmit).not.toContain(`${name}=${customValue}`);
    return;
  }
  expect(await urlAfterSubmit).toContain(`${name}=${customValue}`);
};
export { renders, inputFormSubmit };
//# sourceMappingURL=common-tests.js.map