From c9a166857122a080231a22e74d1ba84365cc1bbb Mon Sep 17 00:00:00 2001 From: Max Franz Date: Thu, 8 Mar 2018 13:27:22 -0500 Subject: [PATCH] Backport 2.x : The `remove` event is emitted on already removed elements #2074 #2075 --- src/collection/index.js | 2 +- test/collection-graph-manipulation.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/collection/index.js b/src/collection/index.js index e0c33b1b54..95d7b7685f 100644 --- a/src/collection/index.js +++ b/src/collection/index.js @@ -521,7 +521,7 @@ elesfn.remove = function( notifyRenderer ){ function add( ele ){ var alreadyAdded = elesToRemoveIds[ ele.id() ]; - if( alreadyAdded ){ + if( ele.removed() || alreadyAdded ){ return; } else { elesToRemoveIds[ ele.id() ] = true; diff --git a/test/collection-graph-manipulation.js b/test/collection-graph-manipulation.js index 9c3e31cb7b..e38acb327b 100644 --- a/test/collection-graph-manipulation.js +++ b/test/collection-graph-manipulation.js @@ -51,6 +51,31 @@ describe('Collection graph manipulation', function(){ expect( cy.$('#n1n2') ).to.have.length(0); }); + it('should not emit the remove event on an already removed element', function(){ + var n1 = cy.$('#n1'); + var emitted = false; + + n1.remove(); + + n1.on('remove', function(){ emitted = true; }); + + n1.remove(); + + expect(emitted).to.be.false; + }); + + it('should only emit the remove event once', function(){ + var n1 = cy.$('#n1'); + var emits = 0; + + n1.on('remove', function(){ emits++; }); + + n1.remove(); + n1.remove(); + + expect(emits).to.equal(1); + }); + }); describe('ele.removed()', function(){