Skip to content

Commit

Permalink
Add test for add & remove
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Apr 15, 2017
1 parent 53053eb commit 4c87e1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/lib/template/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,18 @@
// only perform attachment if the element was previously detached.
if (this.__isDetached) {
this.__isDetached = false;
var parent = Polymer.dom(Polymer.dom(this).parentNode);
var refNode;
var parentNode = Polymer.dom(this).parentNode;
// Affordance for 2.x hybrid mode
if (parentNode.localName == this.is) {
refNode = parentNode;
parentNode = Polymer.dom(parentNode).parentNode;
} else {
refNode = this;
}
var parent = Polymer.dom(parentNode);
for (var i=0; i<this._instances.length; i++) {
this._attachInstance(i, parent);
this._attachInstance(i, parent, refNode);
}
}
},
Expand Down Expand Up @@ -666,10 +675,10 @@
}
},

_attachInstance: function(idx, parent) {
_attachInstance: function(idx, parent, refNode) {
var inst = this._instances[idx];
if (!inst.isPlaceholder) {
parent.insertBefore(inst.root, this);
parent.insertBefore(inst.root, refNode);
}
},

Expand Down
17 changes: 15 additions & 2 deletions test/unit/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ <h4>x-repeat-chunked</h4>
</div>

<!-- 2.x hybrid test -->
<dom-repeat>
<dom-repeat id="hybridDomRepeatWrapper">
<template is="dom-repeat" id="hybridDomRepeat" items='[{"prop":"a"},{"prop":"b"},{"prop":"c"}]'>
<div hybrid-stamped>[[item.prop]]</div>
</template>
</dom-repeat>

<script>
/* global unconfigured1 unconfigured primitive limited inDocumentRepeater configured inDocumentContainer inDocumentContainerOrig unconfigured2 primitiveLarge simple:true model:true stamped:true chunked hybridDomRepeat*/
/* global unconfigured1 unconfigured primitive limited inDocumentRepeater configured inDocumentContainer inDocumentContainerOrig unconfigured2 primitiveLarge simple:true model:true stamped:true chunked hybridDomRepeat hybridDomRepeatWrapper*/

/*
Expected:
Expand Down Expand Up @@ -3903,6 +3903,19 @@ <h4>x-repeat-chunked</h4>
});
});

test('remove & re-add works correctly', function() {
var wrapper = hybridDomRepeatWrapper;
Polymer.dom(document.body).removeChild(wrapper);
Polymer.dom(document.body).appendChild(wrapper);
CustomElements.takeRecords();
hybridDomRepeat.render();
var stamped = Array.from(document.querySelectorAll('[hybrid-stamped]'));
assert.equal(stamped.length, 3);
stamped.forEach(function(el) {
assert.equal(el.parentNode, document.body);
});
});

test('children removed correctly', function() {
hybridDomRepeat.items = null;
hybridDomRepeat.render();
Expand Down

0 comments on commit 4c87e1d

Please sign in to comment.