This repository has been archived by the owner on Jul 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DataSet - Adds unit tests for add, setOptions and on/off #3394
Merged
yotamberk
merged 2 commits into
almende:develop
from
macleodbroad-wf:mbroad/unittest/dataset
Sep 2, 2017
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} Error - Unknown Datatype | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Second |
||
*/ | ||
DataSet.prototype.update = function (data, senderId) { | ||
var addedIds = []; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,7 +282,120 @@ describe('DataSet', function () { | |
{id: 3, content: 'Item 3'}, | ||
{id: 4, content: 'Item 4'} | ||
]); | ||
}); | ||
|
||
describe('add', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 For new tests. Looks clean except for the data initialization thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has been addressed |
||
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); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yesterday, I learned this should be
{string}
. But I suppose that the other PR will handle that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, to be addressed in a follow up PR.