Browse Source

DataSet - Adds unit tests for add, setOptions and on/off (#3394)

* Adds unit tests for add, setOptions and on/off

* removes extraneous return value name in @throws tag of jsdoc
revert-3409-performance
macleodbroad-wf 7 years ago
committed by Yotam Berkowitz
parent
commit
b97b265285
2 changed files with 116 additions and 20 deletions
  1. +2
    -19
      lib/DataSet.js
  2. +114
    -1
      test/DataSet.test.js

+ 2
- 19
lib/DataSet.js View File

@ -73,11 +73,6 @@ function DataSet (data, options) {
}
}
// TODO: deprecated since version 1.1.1 (or 2.0.0?)
if (this._options.convert) {
throw new Error('Option "convert" is deprecated. Use "type" instead.');
}
this._subscribers = {}; // event subscribers
// add initial data when provided
@ -122,7 +117,7 @@ DataSet.prototype.setOptions = function(options) {
/**
* Subscribe to an event, add an event listener
* @param {String} event Event name. Available events: 'put', 'update',
* @param {String} event Event name. Available events: 'add', 'update',
* 'remove'
* @param {function} callback Callback method. Called with three parameters:
* {String} event
@ -141,14 +136,6 @@ DataSet.prototype.on = function(event, callback) {
});
};
/**
* TODO: remove this deprecated function some day (replaced with `on` since version 0.5, deprecated since v4.0)
* @throws {Error}
*/
DataSet.prototype.subscribe = function () {
throw new Error('DataSet.subscribe is deprecated. Use DataSet.on instead.');
};
/**
* Unsubscribe from an event, remove an event listener
* @param {String} event
@ -161,11 +148,6 @@ DataSet.prototype.off = function(event, callback) {
}
};
// TODO: remove this deprecated function some day (replaced with `on` since version 0.5, deprecated since v4.0)
DataSet.prototype.unsubscribe = function () {
throw new Error('DataSet.unsubscribe is deprecated. Use DataSet.off instead.');
};
/**
* Trigger an event
* @param {String} event
@ -234,6 +216,7 @@ DataSet.prototype.add = function (data, senderId) {
* @param {Object | Array} data
* @param {String} [senderId] Optional sender id
* @return {Array} updatedIds The ids of the added or updated items
* @throws {Error} Unknown Datatype
*/
DataSet.prototype.update = function (data, senderId) {
var addedIds = [];

+ 114
- 1
test/DataSet.test.js View File

@ -282,7 +282,120 @@ describe('DataSet', function () {
{id: 3, content: 'Item 3'},
{id: 4, content: 'Item 4'}
]);
});
describe('add', function () {
it('adds nothing for an empty array', function () {
var dataset = new DataSet([]);
var dataItems = [];
assert.equal(dataset.add(dataItems).length, 0)
});
it('adds items of an array', function () {
var dataset = new DataSet([]);
var dataItems = [
{_id: 1, content: 'Item 1', start: new Date(now.valueOf())},
{_id: 2, content: 'Item 2', start: new Date(now.valueOf())}
];
assert.equal(dataset.add(dataItems).length, 2)
});
it('adds a single object', function () {
var dataset = new DataSet([]);
var dataItem = {_id: 1, content: 'Item 1', start: new Date(now.valueOf())};
assert.equal(dataset.add(dataItem).length, 1)
});
it('throws an error when passed bad datatypes', function () {
var dataset = new DataSet([]);
assert.throws(function () { dataset.add(null) }, Error, "null type throws error");
assert.throws(function () { dataset.add(undefined) }, Error, "undefined type throws error");
});
});
describe('setOptions', function () {
var dataset = new DataSet([
{_id: 1, content: 'Item 1', start: new Date(now.valueOf())}
], {queue: true});
it('does not update queue when passed an undefined queue', function () {
var dataset = new DataSet([], {queue: true});
dataset.setOptions({queue: undefined});
assert.notEqual(dataset._queue, undefined)
});
it('destroys the queue when queue set to false', function () {
var dataset = new DataSet([]);
dataset.setOptions({queue: false});
assert.equal(dataset._queue, undefined)
});
it('udpates queue options', function () {
var dataset = new DataSet([]);
dataset.setOptions({queue: {max: 5, delay: 3}});
assert.equal(dataset._queue.max, 5);
assert.equal(dataset._queue.delay, 3);
});
it('creates new queue given if none is set', function () {
var dataset = new DataSet([], {queue: true});
dataset._queue.destroy();
dataset._queue = null;
dataset.setOptions({queue: {max: 5, delay: 3}});
assert.equal(dataset._queue.max, 5);
assert.equal(dataset._queue.delay, 3);
});
});
});
describe('on / off', function () {
var dataset = new DataSet([
{_id: 1, content: 'Item 1', start: new Date(now.valueOf())}
]);
var count = 0;
function inc() {count++;}
it('fires for put', function () {
var dataset = new DataSet([]);
count = 0;
// on
dataset.on('add', inc);
dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
assert.equal(count, 1);
// off
dataset.off('add', inc);
dataset.add({_id: 2, content: 'Item 2', start: new Date(now.valueOf())});
assert.equal(count, 1);
});
it('fires for remove', function () {
var dataset = new DataSet([]);
count = 0;
// on
dataset.on('remove', inc);
var id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
dataset.remove(id);
assert.equal(count, 1);
// off
dataset.off('remove', inc);
id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
dataset.remove(id);
assert.equal(count, 1);
});
it('fires for update', function () {
var dataset = new DataSet([]);
count = 0;
// on
dataset.on('update', inc);
var id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
dataset.update({id: id, content: 'beep boop'});
assert.equal(count, 1);
// off
dataset.off('update', inc);
id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
dataset.update({id: id, content: 'beep boop'});
assert.equal(count, 1);
});
});
});

Loading…
Cancel
Save