Skip to content

Commit e858c54

Browse files
committed
Add attribute tests for scoped-custom-element-registry
1 parent 90cb97f commit e858c54

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

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

+135
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,74 @@ describe('Element', () => {
123123
expect($el.getAttribute('foo')).to.equal('bar');
124124
});
125125

126+
it('setAttribute should trigger attributeChangedCallback', () => {
127+
const {tagName, CustomElementClass} = getObservedAttributesTestElement([
128+
'foo',
129+
'bar',
130+
]);
131+
customElements.define(tagName, CustomElementClass);
132+
133+
const $el = getHTML(`<${tagName} bar="value 1"></${tagName}>`);
134+
expect($el.changedAttributes[0]).to.deep.equal({
135+
name: 'bar',
136+
oldValue: null,
137+
newValue: 'value 1',
138+
});
139+
140+
$el.setAttribute('foo', '');
141+
expect($el.changedAttributes[1]).to.deep.equal({
142+
name: 'foo',
143+
oldValue: null,
144+
newValue: '',
145+
});
146+
147+
$el.setAttribute('foo', '');
148+
expect($el.changedAttributes[2]).to.deep.equal({
149+
name: 'foo',
150+
oldValue: '',
151+
newValue: '',
152+
});
153+
154+
$el.setAttribute('foo', 'value 1');
155+
expect($el.changedAttributes[3]).to.deep.equal({
156+
name: 'foo',
157+
oldValue: '',
158+
newValue: 'value 1',
159+
});
160+
161+
/* Setting an attribute just set programmatically with the same key and
162+
* value should still trigger the callback. */
163+
$el.setAttribute('foo', 'value 1');
164+
expect($el.changedAttributes[4]).to.deep.equal({
165+
name: 'foo',
166+
oldValue: 'value 1',
167+
newValue: 'value 1',
168+
});
169+
170+
$el.setAttribute('foo', 'value 2');
171+
expect($el.changedAttributes[5]).to.deep.equal({
172+
name: 'foo',
173+
oldValue: 'value 1',
174+
newValue: 'value 2',
175+
});
176+
177+
/* Setting an attribute that was already present in the HTML with the same
178+
* value should still trigger the callback. */
179+
$el.setAttribute('bar', 'value 1');
180+
expect($el.changedAttributes[6]).to.deep.equal({
181+
name: 'bar',
182+
oldValue: 'value 1',
183+
newValue: 'value 1',
184+
});
185+
186+
$el.setAttribute('bar', 'value 2');
187+
expect($el.changedAttributes[7]).to.deep.equal({
188+
name: 'bar',
189+
oldValue: 'value 1',
190+
newValue: 'value 2',
191+
});
192+
});
193+
126194
it('should call removeAttribute', () => {
127195
const {tagName, CustomElementClass} = getObservedAttributesTestElement([
128196
'foo',
@@ -135,6 +203,29 @@ describe('Element', () => {
135203
expect($el.hasAttribute('foo')).to.be.false;
136204
});
137205

206+
it('removeAttribute should trigger attributeChangedCallback', () => {
207+
const {tagName, CustomElementClass} = getObservedAttributesTestElement([
208+
'foo',
209+
]);
210+
customElements.define(tagName, CustomElementClass);
211+
const $el = getHTML(`<${tagName} foo></${tagName}>`);
212+
213+
$el.removeAttribute('foo');
214+
expect($el.changedAttributes[1]).to.deep.equal({
215+
name: 'foo',
216+
oldValue: '',
217+
newValue: null,
218+
});
219+
220+
$el.setAttribute('foo', 'value');
221+
$el.removeAttribute('foo');
222+
expect($el.changedAttributes[3]).to.deep.equal({
223+
name: 'foo',
224+
oldValue: 'value',
225+
newValue: null,
226+
});
227+
});
228+
138229
it('should call toggleAttribute', () => {
139230
const {tagName, CustomElementClass} = getObservedAttributesTestElement([
140231
'foo',
@@ -151,5 +242,49 @@ describe('Element', () => {
151242

152243
expect($el.hasAttribute('foo')).to.be.true;
153244
});
245+
246+
it('toggleAttribute should trigger attributeChangedCallback', () => {
247+
const {tagName, CustomElementClass} = getObservedAttributesTestElement([
248+
'foo',
249+
]);
250+
customElements.define(tagName, CustomElementClass);
251+
const $el = getHTML(`<${tagName} foo></${tagName}>`);
252+
253+
/* Forcefully toggling an already present attribute on shouldn't trigger
254+
* a change. */
255+
$el.toggleAttribute('foo', true);
256+
expect($el.changedAttributes).to.have.length(1);
257+
258+
/* Forcefully toggling a present attribute off. */
259+
$el.toggleAttribute('foo', false);
260+
expect($el.changedAttributes).to.have.length(2);
261+
expect($el.changedAttributes[1]).to.deep.equal({
262+
name: 'foo',
263+
oldValue: '',
264+
newValue: null,
265+
});
266+
267+
/* Forcefully toggling a non-present attribute off shouldn't trigger a
268+
* change. */
269+
$el.toggleAttribute('foo', false);
270+
expect($el.changedAttributes).to.have.length(2);
271+
272+
/* Forcefully toggling an absent attribute off. */
273+
$el.toggleAttribute('foo', true);
274+
expect($el.changedAttributes).to.have.length(3);
275+
expect($el.changedAttributes[2]).to.deep.equal({
276+
name: 'foo',
277+
oldValue: null,
278+
newValue: '',
279+
});
280+
281+
/* Non-forcefully toggling attributes off and on. */
282+
$el.toggleAttribute('foo');
283+
$el.toggleAttribute('foo');
284+
expect($el.changedAttributes.slice(3)).to.deep.equal([
285+
{name: 'foo', oldValue: '', newValue: null},
286+
{name: 'foo', oldValue: null, newValue: ''},
287+
]);
288+
});
154289
});
155290
});

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

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ export const getObservedAttributesTestElement = (
4343
static get observedAttributes() {
4444
return observedAttributeNames;
4545
}
46+
47+
/** @type {{ name: string, oldValue: string|null, newValue: string|null}[]} */
48+
changedAttributes = [];
49+
50+
attributeChangedCallback(name, oldValue, newValue) {
51+
this.changedAttributes.push({name, oldValue, newValue});
52+
}
4653
},
4754
});
4855

0 commit comments

Comments
 (0)