Skip to content

Commit 1e903a9

Browse files
author
Steven Orvell
committed
Add tests for setting custom attribute name
1 parent 74fb515 commit 1e903a9

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

lib/mixins/property-effects.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@
20512051
* @protected
20522052
*/
20532053
_createReflectedProperty(property) {
2054-
let attr = CaseMap.camelToDashCase(property);
2054+
let attr = this._attributeForProperty(property);
20552055
if (attr[0] === '-') {
20562056
console.warn('Property ' + property + ' cannot be reflected to attribute ' +
20572057
attr + ' because "-" is not a valid starting attribute name. Use a lowercase first letter for the property instead.');

test/unit/polymer.element.html

+20-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040
computed: '_compute(computedPropDep)'
4141
},
4242
accessor: String,
43-
noStomp: String
43+
noStomp: String,
44+
customAttr: {
45+
type: String,
46+
attribute: 'foo',
47+
reflectToAttribute: true
48+
}
4449
};
4550
}
4651

@@ -323,7 +328,7 @@ <h1>Sub template</h1>
323328

324329
<test-fixture id="my-element-attr">
325330
<template>
326-
<my-element prop="attr"></my-element>
331+
<my-element prop="attr" foo="foo"></my-element>
327332
</template>
328333
</test-fixture>
329334

@@ -409,10 +414,22 @@ <h1>Sub template</h1>
409414
test('attributes', function() {
410415
const fixtureEl = fixture('my-element-attr');
411416
assert.equal(fixtureEl.prop, 'attr');
412-
assert.equal(fixtureEl._callAttributeChangedCallback, 1);
417+
assert.equal(fixtureEl.customAttr, 'foo');
418+
assert.equal(fixtureEl._callAttributeChangedCallback, 3);
413419
assert.isTrue(fixtureEl.hasAttribute('tabindex'));
414420
});
415421

422+
test('reflecting attributes', function() {
423+
const fixtureEl = fixture('my-element-attr');
424+
fixtureEl.prop = 'propValue';
425+
// without reflect effect
426+
fixtureEl._propertyToAttribute('prop');
427+
assert.equal(fixtureEl.getAttribute('prop'), 'propValue');
428+
// with reflect effect
429+
fixtureEl.customAttr = 'customAttrValue';
430+
assert.equal(fixtureEl.getAttribute('foo'), 'customAttrValue');
431+
});
432+
416433
});
417434

418435
suite('subclass', function() {

test/unit/polymer.properties-element.html

+18-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
static get properties() {
2626
return {
2727
prop: String,
28-
noStomp: String
28+
noStomp: String,
29+
customAttr: {
30+
type: String,
31+
attribute: 'foo'
32+
}
2933
};
3034
}
3135

@@ -186,7 +190,7 @@
186190

187191
<test-fixture id="my-element-attr">
188192
<template>
189-
<my-element prop="attr"></my-element>
193+
<my-element prop="attr" foo="foo"></my-element>
190194
</template>
191195
</test-fixture>
192196

@@ -244,7 +248,18 @@
244248
test('attributes', function() {
245249
const fixtureEl = fixture('my-element-attr');
246250
assert.equal(fixtureEl.prop, 'attr');
247-
assert.equal(fixtureEl._callAttributeChangedCallback, 1);
251+
assert.equal(fixtureEl.customAttr, 'foo');
252+
assert.equal(fixtureEl._callAttributeChangedCallback, 2);
253+
});
254+
255+
test('reflecting attributes', function() {
256+
const fixtureEl = fixture('my-element-attr');
257+
fixtureEl.prop = 'propValue';
258+
fixtureEl._propertyToAttribute('prop');
259+
assert.equal(fixtureEl.getAttribute('prop'), 'propValue');
260+
fixtureEl.customAttr = 'customAttrValue';
261+
fixtureEl._propertyToAttribute('customAttr');
262+
assert.equal(fixtureEl.getAttribute('foo'), 'customAttrValue');
248263
});
249264

250265
});

0 commit comments

Comments
 (0)