@@ -123,6 +123,74 @@ describe('Element', () => {
123
123
expect ( $el . getAttribute ( 'foo' ) ) . to . equal ( 'bar' ) ;
124
124
} ) ;
125
125
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
+
126
194
it ( 'should call removeAttribute' , ( ) => {
127
195
const { tagName, CustomElementClass} = getObservedAttributesTestElement ( [
128
196
'foo' ,
@@ -135,6 +203,29 @@ describe('Element', () => {
135
203
expect ( $el . hasAttribute ( 'foo' ) ) . to . be . false ;
136
204
} ) ;
137
205
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
+
138
229
it ( 'should call toggleAttribute' , ( ) => {
139
230
const { tagName, CustomElementClass} = getObservedAttributesTestElement ( [
140
231
'foo' ,
@@ -151,5 +242,49 @@ describe('Element', () => {
151
242
152
243
expect ( $el . hasAttribute ( 'foo' ) ) . to . be . true ;
153
244
} ) ;
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
+ } ) ;
154
289
} ) ;
155
290
} ) ;
0 commit comments