-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test to confirm observedAttributes behavior (#2973)
- Loading branch information
1 parent
fda8d6f
commit 4f97aeb
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/@lwc/integration-karma/test/component/observed-attributes/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); | ||
}); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/@lwc/integration-karma/test/component/observed-attributes/x/observes/observes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |