Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
* When extended, a behavior `registered` is always called on sub-most prototype rather than the prototype on which `registered` was defined.
* behavior property default values are overwritten by later behaviors and elements
* readOnly properties ignored when a previous behavior observes the property
* observedAttributes when extended
  • Loading branch information
Steven Orvell committed Nov 14, 2018
1 parent bbf1acc commit db4dbf8
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,45 @@
zot: 'zot'
});

Polymer({
is: 'override-default-value',
behaviors: [
{
properties: {
foo: { value: true },
bar: { value: true}
}
},
{
properties: {
foo: { value: true },
bar: String,
zot: {value: true}
}
},

],

properties: {
foo: String,
zot: String
}
});

Polymer({
is: 'property-observer-readonly',
behaviors: [
{
observers: ['_changed(bar)'],
_changed() {}
}
],

properties: {
bar: {readOnly: true}
}
});

});
</script>

Expand Down Expand Up @@ -470,6 +509,18 @@
</template>
</test-fixture>

<test-fixture id="override-default-value">
<template>
<override-default-value></override-default-value>
</template>
</test-fixture>

<test-fixture id="property-observer-readonly">
<template>
<property-observer-readonly></property-observer-readonly>
</template>
</test-fixture>

<script>

suite('single behavior element', function() {
Expand Down Expand Up @@ -532,6 +583,19 @@
assert.equal(el.nug, 'nug');
});

test('behavior default values can be overridden', function() {
const el = fixture('override-default-value');
assert.notOk(el.foo);
assert.notOk(el.bar);
assert.notOk(el.zot);
});

test('readOnly not applied when property was previously observed', function() {
const el = fixture('property-observer-readonly');
el.bar = 5;
assert.equal(el.bar, 5);
});

});

suite('behavior.registered', function() {
Expand Down
57 changes: 57 additions & 0 deletions test/unit/mixin-behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,37 @@
], Polymer.Element);

customElements.define('before-register-observers', klass);

const Base = Polymer.mixinBehaviors([{
registered() {
this.usedExtendedProto = this.canUseExtendedProto;
}
}], Polymer.Element);

customElements.define('registered-proto', Polymer.mixinBehaviors([
{canUseExtendedProto: true}
], Base));


class Base2 extends Polymer.mixinBehaviors([{
properties: {b1: String} }], Polymer.Element) {

static get properties() {
return { e1: String};
}
}

class El extends Polymer.mixinBehaviors([{
properties: {b2: String}}], Base2) {

static get properties() {
return { e2: String};
}

}

customElements.define('extended-observed-attributes', El);

});
</script>

Expand Down Expand Up @@ -436,6 +467,19 @@
<behavior-registered-ext></behavior-registered-ext>
</template>
</test-fixture>

<test-fixture id="registered-proto">
<template>
<registered-proto></registered-proto>
</template>
</test-fixture>

<test-fixture id="extended-observed-attributes">
<template>
<extended-observed-attributes b1="b1" e1="e1" b2="b2" e2="e2"></extended-observed-attributes>
</template>
</test-fixture>

<script>

suite('single behavior element', function() {
Expand Down Expand Up @@ -507,6 +551,11 @@
assert.equal(el.__fooChangedCalled, 1);
});

test('registered called on class prototype when extended', function() {
var el = fixture('registered-proto');
assert.isTrue(el.usedExtendedProto);
});

});

suite('behavior lifecycle', function() {
Expand Down Expand Up @@ -654,6 +703,14 @@
assert.deepEqual(el.__createdList, ['1', '2', 'sup', '3', '4', 'sub'], 'created list wrong');
});

test('observedAttributes when extended', function() {
const el = fixture('extended-observed-attributes');
assert.equal(el.b1, 'b1');
assert.equal(el.e1, 'e1');
assert.equal(el.b2, 'b2');
assert.equal(el.e2, 'e2');
});

});

</script>
Expand Down

0 comments on commit db4dbf8

Please sign in to comment.