From 5e0ce08804067a761adb0274ba7ddd8215ef705d Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 7 Dec 2016 11:56:44 +0100 Subject: [PATCH] Fix ol.Collection#push return value --- src/ol/collection.js | 6 +++--- test/spec/ol/collection.test.js | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/ol/collection.js b/src/ol/collection.js index 6132df8ab8..88e667a06a 100644 --- a/src/ol/collection.js +++ b/src/ol/collection.js @@ -145,13 +145,13 @@ ol.Collection.prototype.pop = function() { /** * Insert the provided element at the end of the collection. * @param {T} elem Element. - * @return {number} Length. + * @return {number} New length of the collection. * @api stable */ ol.Collection.prototype.push = function(elem) { - var n = this.array_.length; + var n = this.getLength(); this.insertAt(n, elem); - return n; + return this.getLength(); }; diff --git a/test/spec/ol/collection.test.js b/test/spec/ol/collection.test.js index 9cfac96a0b..8e20f4b87b 100644 --- a/test/spec/ol/collection.test.js +++ b/test/spec/ol/collection.test.js @@ -31,11 +31,23 @@ describe('ol.collection', function() { describe('push to a collection', function() { it('adds elements to the collection', function() { - collection.push(1); - expect(collection.getLength()).to.eql(1); + var length = collection.push(1); + expect(collection.getLength()).to.eql(length); expect(collection.getArray()).to.eql([1]); expect(collection.item(0)).to.eql(1); }); + it('returns the correct new length of the collection', function() { + var length; + ol.events.listen(collection, 'add', function(event) { + if (event.element === 'remove_me') { + collection.remove(event.element); + } + }); + length = collection.push('keep_me'); + expect(collection.getLength()).to.eql(length); + length = collection.push('remove_me'); + expect(collection.getLength()).to.eql(length); + }); }); describe('pop from a collection', function() { @@ -236,8 +248,8 @@ describe('ol.collection', function() { ol.events.listen(collection, ol.Collection.EventType.ADD, function(e) { elem = e.element; }); - collection.push(1); - expect(elem).to.eql(1); + var length = collection.push(1); + expect(elem).to.eql(length); }); });