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

Custom element attributeChangedCallback is being called on any attribute change #41

Closed
ahmadigbaryia opened this issue May 20, 2020 · 2 comments

Comments

@ahmadigbaryia
Copy link

The callback attributeChangedCallback is being called on ANY attribute change, not only on the observed ones as expected.

here is a jest test to help you reproduce

import { Window } from "happy-dom";

class MyElem extends HTMLElement {
    attributeChangedCallback(attribute, oldValue, newValue) {}
    static get observedAttributes() {
        return ['my-attr'];
    }
}

const window = new Window();
const document = window.document;
let myElem;

window.customElements.define("my-elem", MyElem);

beforeEach(() => {
  document.body.innerHTML = "<my-elem id='my-elem'></my-elem>";
  myElem = document.querySelector("#my-elem");
});
describe("Make sure component is created", () => {
  //SUCCESS
  test("’Make sure <my-elem> is defined", () => {
    expect(myElem).not.toBeNull();
  });
  //SUCCESS
  test("’Make sure changing an observed attribute will triiger change callback", () => {
    const spy = jest.spyOn(myElem, 'attributeChangedCallback');
    myElem.setAttribute("my-attr", "css-class");
    expect(spy).toHaveBeenCalled();
  });
  //FAILS
  test("’Make sure changing non observed attribute will not triiger change callback", () => {
    const spy = jest.spyOn(myElem, 'attributeChangedCallback');
    myElem.setAttribute("class", "css-class");
    expect(spy).not.toHaveBeenCalled();
  });
  //FAILS
  test("’Make sure changing non observed attribute will not triiger change callback", () => {
    const spy = jest.spyOn(myElem, 'attributeChangedCallback');
    myElem.setAttribute("style", "display: block;");
    expect(spy).not.toHaveBeenCalled();
  });
  //FAILS
  test("’Make sure changing non observed attribute will not triiger change callback", () => {
    const spy = jest.spyOn(myElem, 'attributeChangedCallback');
    myElem.setAttribute("id", "other-id");
    expect(spy).not.toHaveBeenCalled();
  });
});
@capricorn86
Copy link
Owner

Thank you for reporting @ahmadigbaryia. I will try to fix it as soon as possible.

@capricorn86
Copy link
Owner

Hi @ahmadigbaryia!

Sorry that it has taken such a long time before I could release a fix for this issue.

I have worked on v1.0.0, which is now released. It has support for only triggering attributeChangedCallback() for observed attributes.

I will close this issue now.

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

No branches or pull requests

2 participants