Skip to content

Commit 623e9c8

Browse files
committed
tests: add inital tests for element attributes
1 parent b513a5a commit 623e9c8

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

packages/scoped-custom-element-registry/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ upgrade it.
2222
Notes/limitations:
2323

2424
- In order to leverage native CE when available, `observedAttributes` handling
25-
must be simulated by patching `setAttribute`/`getAttribute` to call
25+
must be simulated by patching `setAttribute`/`getAttribute`/`toggleAttribute` to call
2626
`attributeChangedCallback` manually, since while we can delegate constructors,
2727
the `observedAttributes` respected by the browser are fixed at define time.
2828
This means that native reflecting properties are not observable when set via

packages/scoped-custom-element-registry/src/scoped-custom-element-registry.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (!ShadowRoot.prototype.createElement) {
5151
);
5252
}
5353
// Since observedAttributes can't change, we approximate it by patching
54-
// set/removeAttribute on the user's class
54+
// set/remove/toggleAttribute on the user's class
5555
const attributeChangedCallback =
5656
elementClass.prototype.attributeChangedCallback;
5757
const observedAttributes = new Set(elementClass.observedAttributes || []);
@@ -307,8 +307,8 @@ if (!ShadowRoot.prototype.createElement) {
307307
};
308308
};
309309

310-
// Helper to patch CE class setAttribute/getAttribute to implement
311-
// attributeChangedCallback
310+
// Helper to patch CE class setAttribute/getAttribute/toggleAttribute to
311+
// implement attributeChangedCallback
312312
const patchAttributes = (
313313
elementClass,
314314
observedAttributes,

packages/scoped-custom-element-registry/test/Element.test.html.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {expect} from '@open-wc/testing';
22

3-
import {getTestElement, getShadowRoot, getHTML} from './utils.js';
3+
import {
4+
getTestElement,
5+
getObservedAttributesTestElement,
6+
getShadowRoot,
7+
getHTML
8+
} from './utils.js';
49

510
describe('Element', () => {
611
describe('global registry', () => {
@@ -104,4 +109,41 @@ describe('Element', () => {
104109
expect($el.firstElementChild).to.not.be.instanceof(CustomElementClass);
105110
});
106111
});
112+
113+
describe('attributes', () => {
114+
it('should call setAttribute', () => {
115+
const {tagName, CustomElementClass} = getObservedAttributesTestElement(['foo']);
116+
customElements.define(tagName, CustomElementClass);
117+
const $el = getHTML('<div></div>');
118+
$el.innerHTML = `<${tagName}></${tagName}>`;
119+
120+
$el.firstElementChild.setAttribute('foo', 'bar');
121+
122+
expect($el.firstElementChild.getAttribute('foo')).to.equal('bar');
123+
});
124+
it('should call removeAttribute', () => {
125+
const {tagName, CustomElementClass} = getObservedAttributesTestElement(['foo']);
126+
customElements.define(tagName, CustomElementClass);
127+
const $el = getHTML('<div></div>');
128+
$el.innerHTML = `<${tagName} foo></${tagName}>`;
129+
130+
$el.firstElementChild.removeAttribute('foo');
131+
132+
expect($el.firstElementChild.hasAttribute('foo')).to.be.false;
133+
});
134+
it('should call toggleAttribute', () => {
135+
const {tagName, CustomElementClass} = getObservedAttributesTestElement(['foo']);
136+
customElements.define(tagName, CustomElementClass);
137+
const $el = getHTML('<div></div>');
138+
$el.innerHTML = `<${tagName}></${tagName}>`;
139+
140+
$el.firstElementChild.toggleAttribute('foo', false);
141+
142+
expect($el.firstElementChild.hasAttribute('foo')).to.be.false;
143+
144+
$el.firstElementChild.toggleAttribute('foo', true);
145+
146+
expect($el.firstElementChild.hasAttribute('foo')).to.be.true;
147+
});
148+
});
107149
});

packages/scoped-custom-element-registry/test/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ export const getTestElement = () => ({
3030
CustomElementClass: class extends HTMLElement {},
3131
});
3232

33+
/**
34+
*
35+
* @param {array<string>} observedAttributeNames the names of the attributes you want to observe
36+
* @returns {{Klass: typeof HTMLElement, tagName: string}}
37+
*/
38+
export const getObservedAttributesTestElement = (observedAttributeNames = []) => ({
39+
tagName: getTestTagName(),
40+
CustomElementClass: class extends HTMLElement {
41+
static get observedAttributes() {
42+
return observedAttributeNames;
43+
}
44+
},
45+
});
46+
3347
export const getFormAssociatedTestElement = () => ({
3448
tagName: getTestTagName(),
3549
CustomElementClass: class extends HTMLElement {

0 commit comments

Comments
 (0)