Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test environment (npm run test:browser) behaves differently than real browser #2351

Open
aghArdeshir opened this issue Aug 27, 2024 · 1 comment · May be fixed by #2353
Open

Test environment (npm run test:browser) behaves differently than real browser #2351

aghArdeshir opened this issue Aug 27, 2024 · 1 comment · May be fixed by #2353

Comments

@aghArdeshir
Copy link
Contributor

aghArdeshir commented Aug 27, 2024

Expected behavior

I'm trying to write a test to expose the bug explained here: #2330
The bug is clearly reproducible when I try it manually.
But when I try to expose this bad behavior as an automated test, it starts acting correctly.

Actual Behavior

Take this reproduction link: https://studio.webcomponents.dev/edit/CdTHK8iuVNqVSuive1NH/src/index.js?p=README.md

It contains this code snippet:

import { LionButton } from "@lion/ui/button.js";

customElements.define("lion-button", LionButton);

["lion-button", "button"].forEach((tagName) => {
  let counter = 1;

  const theButton = document.createElement(tagName);
  theButton.textContent = `This is ${tagName}`;
  document.body.appendChild(theButton);

  theButton.addEventListener("click", () => {
    console.log(`Hello from ${tagName} - ${counter}`);
  });

  theButton.click();
  counter += 1;

  theButton.setAttribute("disabled", "true");

  theButton.click();
  counter += 1;

  theButton.removeAttribute("disabled");

  theButton.click();
  counter += 1;
});

After running this code snippet, the normal button logs to the console 2 times, but the lion-button logs to the console 3 times. The normal button is behaving correctly and not handling clicks when its disabled.

Now look at the new test in this PR: #2352

Additional context

I guess the right approach is to remove that extra line (which imports @webcomponents/scoped-custom-element-registry), and instead every test that needs it, should import it independently. Importing it for all tests affects tests behavior (which is bad). It gives us false confidence that our components is working fine, but in the real world, not everyone is importing @webcomponents/scoped-custom-element-registry)

I'm cooking an MR for this 🧑‍🍳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant