Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Commit

Permalink
DataSet - Adds unit tests for add, setOptions and on/off (#3394)
Browse files Browse the repository at this point in the history
* Adds unit tests for add, setOptions and on/off

* removes extraneous return value name in @throws tag of jsdoc
  • Loading branch information
macleodbroad-wf authored and yotamberk committed Sep 2, 2017
1 parent f75a893 commit b97b265
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 20 deletions.
21 changes: 2 additions & 19 deletions lib/DataSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 = [];
Expand Down
115 changes: 114 additions & 1 deletion test/DataSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit b97b265

Please sign in to comment.