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(){