Add index to the Collection events

This commit is contained in:
Frederic Junod
2018-11-17 08:43:35 +01:00
parent a81c2ad995
commit 6219e31e40
2 changed files with 44 additions and 27 deletions

View File

@@ -140,53 +140,58 @@ describe('ol.collection', function() {
describe('setAt and event', function() {
it('does dispatch events', function() {
const collection = new Collection(['a', 'b']);
let added, removed;
let added, removed, addedIndex, removedIndex;
listen(collection, CollectionEventType.ADD, function(e) {
added = e.element;
addedIndex = e.index;
});
listen(collection, CollectionEventType.REMOVE, function(e) {
removed = e.element;
removedIndex = e.index;
});
listen(
collection, CollectionEventType.REMOVE, function(e) {
removed = e.element;
});
collection.setAt(1, 1);
expect(added).to.eql(1);
expect(addedIndex).to.eql(1);
expect(removed).to.eql('b');
expect(removedIndex).to.eql(1);
});
});
describe('removeAt and event', function() {
it('does dispatch events', function() {
const collection = new Collection(['a']);
let removed;
listen(
collection, CollectionEventType.REMOVE, function(e) {
removed = e.element;
});
let removed, removedIndex;
listen(collection, CollectionEventType.REMOVE, function(e) {
removed = e.element;
removedIndex = e.index;
});
collection.pop();
expect(removed).to.eql('a');
expect(removedIndex).to.eql(0);
});
});
describe('insertAt and event', function() {
it('does dispatch events', function() {
const collection = new Collection([0, 2]);
let added;
listen(
collection, CollectionEventType.ADD, function(e) {
added = e.element;
});
let added, addedIndex;
listen(collection, CollectionEventType.ADD, function(e) {
added = e.element;
addedIndex = e.index;
});
collection.insertAt(1, 1);
expect(added).to.eql(1);
expect(addedIndex).to.eql(1);
});
});
describe('setAt beyond end', function() {
it('triggers events properly', function() {
const added = [];
listen(
collection, CollectionEventType.ADD, function(e) {
added.push(e.element);
});
const added = [], addedIndexes = [];
listen(collection, CollectionEventType.ADD, function(e) {
added.push(e.element);
addedIndexes.push(e.index);
});
collection.setAt(2, 0);
expect(collection.getLength()).to.eql(3);
expect(collection.item(0)).to.be(undefined);
@@ -196,6 +201,7 @@ describe('ol.collection', function() {
expect(added[0]).to.eql(undefined);
expect(added[1]).to.eql(undefined);
expect(added[2]).to.eql(0);
expect(addedIndexes).to.eql([0, 1, 2]);
});
});
@@ -232,12 +238,14 @@ describe('ol.collection', function() {
describe('add event', function() {
it('triggers add when pushing', function() {
const collection = new Collection();
let elem;
let elem, addedIndex;
listen(collection, CollectionEventType.ADD, function(e) {
elem = e.element;
addedIndex = e.index;
});
const length = collection.push(1);
expect(elem).to.eql(length);
expect(addedIndex).to.eql(0);
});
});
@@ -276,12 +284,14 @@ describe('ol.collection', function() {
});
it('fires events', function() {
const collection = new Collection();
const elems = [];
const elems = [], addedIndexes = [];
listen(collection, CollectionEventType.ADD, function(e) {
elems.push(e.element);
addedIndexes.push(e.index);
});
collection.extend([1, 2]);
expect(elems).to.eql([1, 2]);
expect(addedIndexes).to.eql([0, 1]);
});
});