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 ad5cb26 commit f3b6675
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
64 changes: 64 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,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>

<test-fixture id="single">
Expand Down Expand Up @@ -471,6 +510,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 type="module">
import { Polymer } from '../../polymer-legacy.js';

Expand Down Expand Up @@ -534,6 +585,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
71 changes: 70 additions & 1 deletion test/unit/mixin-behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,48 @@
customElements.define('before-register-observers', klass);
</script>

<script type="module">
import { mixinBehaviors } from '../../lib/legacy/class.js';
import { PolymerElement } from '../../polymer-element.js';

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

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

</script>

<script type="module">
import { mixinBehaviors } from '../../lib/legacy/class.js';
import { PolymerElement } from '../../polymer-element.js';


class Base extends mixinBehaviors([{
properties: {b1: String} }], PolymerElement) {

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

class El extends mixinBehaviors([{
properties: {b2: String}}], Base) {

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

}

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

</script>

<test-fixture id="single">
<template>
<single-behavior></single-behavior>
Expand Down Expand Up @@ -436,6 +478,20 @@
<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 type="module">
import { Polymer } from '../../polymer-legacy.js';
import { mixinBehaviors } from '../../lib/legacy/class.js';
Expand Down Expand Up @@ -508,7 +564,12 @@
var el = fixture('before-register-observers');
el.foo = 1;
assert.equal(el.__fooChangedCalled, 1);
});
});

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

});

Expand Down Expand Up @@ -657,6 +718,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 f3b6675

Please sign in to comment.