Skip to content

Commit

Permalink
test: add test to confirm observedAttributes behavior (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson authored Aug 4, 2022
1 parent fda8d6f commit 4f97aeb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createElement } from 'lwc';
import Observes from 'x/observes';

const SUPPORTS_CUSTOM_ELEMENTS = !process.env.COMPAT && 'customElements' in window;

if (SUPPORTS_CUSTOM_ELEMENTS) {
describe('observed attributes', () => {
const constructorMethods = {
CustomElementConstructor: () => {
customElements.define(
'x-observes-as-custom-element',
Observes.CustomElementConstructor
);
const elm = document.createElement('x-observes-as-custom-element');
document.body.appendChild(elm);
return elm;
},
createElement: () => {
const elm = createElement('x-observes', { is: Observes });
document.body.appendChild(elm);
return elm;
},
};

Object.entries(constructorMethods).forEach(([name, makeElement]) => {
// TODO [#2972]: LWC components do not respect observedAttributes/attributeChangedCallback
it(`${name} - should not observe attributes`, () => {
const elm = makeElement();

elm.setAttribute('foo', 'bar');
elm.removeAttribute('foo');

expect(elm.changes).toEqual([]);
});
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LightningElement, api } from 'lwc';

export default class extends LightningElement {
static observedAttributes = ['foo'];

@api changes = [];

attributeChangedCallback(name, oldValue, newValue) {
this.changes.push({ name, oldValue, newValue });
}
}

0 comments on commit 4f97aeb

Please sign in to comment.