vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
11 KiB

  1. var assert = require('assert');
  2. var DataSet = require('../lib/DataSet');
  3. var Queue = require('../lib/Queue');
  4. // TODO: test the source code immediately, but this is ES6
  5. var now = new Date();
  6. describe('DataSet', function () {
  7. it('should work', function () {
  8. // TODO: improve DataSet tests, split up in one test per function
  9. var data = new DataSet({
  10. type: {
  11. start: 'Date',
  12. end: 'Date'
  13. }
  14. });
  15. // add single items with different date types
  16. data.add({id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  17. data.add({id: 2, content: 'Item 2', start: now.toISOString()});
  18. data.add([
  19. //{id: 3, content: 'Item 3', start: moment(now)}, // TODO: moment fails, not the same instance
  20. {id: 3, content: 'Item 3', start: now},
  21. {id: 4, content: 'Item 4', start: '/Date(' + now.valueOf() + ')/'}
  22. ]);
  23. var items = data.get();
  24. assert.equal(data.length, 4);
  25. assert.equal(items.length, 4);
  26. items.forEach(function (item) {
  27. assert.ok(item.start instanceof Date);
  28. });
  29. // get filtered fields only
  30. var sort = function (a, b) {
  31. return a.id > b.id;
  32. };
  33. assert.deepEqual(data.get({
  34. fields: ['id', 'content']
  35. }).sort(sort), [
  36. {id: 1, content: 'Item 1'},
  37. {id: 2, content: 'Item 2'},
  38. {id: 3, content: 'Item 3'},
  39. {id: 4, content: 'Item 4'}
  40. ]);
  41. // convert dates
  42. assert.deepEqual(data.get({
  43. fields: ['id', 'start'],
  44. type: {start: 'Number'}
  45. }).sort(sort), [
  46. {id: 1, start: now.valueOf()},
  47. {id: 2, start: now.valueOf()},
  48. {id: 3, start: now.valueOf()},
  49. {id: 4, start: now.valueOf()}
  50. ]);
  51. // get a single item
  52. assert.deepEqual(data.get(1, {
  53. fields: ['id', 'start'],
  54. type: {start: 'ISODate'}
  55. }), {
  56. id: 1,
  57. start: now.toISOString()
  58. });
  59. // remove an item
  60. data.remove(2);
  61. assert.deepEqual(data.get({
  62. fields: ['id']
  63. }).sort(sort), [
  64. {id: 1},
  65. {id: 3},
  66. {id: 4}
  67. ]);
  68. assert.equal(data.length, 3);
  69. // add an item
  70. data.add({id: 5, content: 'Item 5', start: now.valueOf()});
  71. assert.deepEqual(data.get({
  72. fields: ['id']
  73. }).sort(sort), [
  74. {id: 1},
  75. {id: 3},
  76. {id: 4},
  77. {id: 5}
  78. ]);
  79. assert.equal(data.length, 4);
  80. // update an item
  81. data.update({id: 5, content: 'changed!'}); // update item (extend existing fields)
  82. assert.equal(data.length, 4);
  83. data.remove(3); // remove existing item
  84. assert.equal(data.length, 3);
  85. data.add({id: 3, other: 'bla'}); // add new item
  86. assert.equal(data.length, 4);
  87. data.update({id: 6, content: 'created!', start: now.valueOf()}); // this item is not yet existing, create it
  88. assert.equal(data.length, 5);
  89. assert.deepEqual(data.get().sort(sort), [
  90. {id: 1, content: 'Item 1', start: now},
  91. {id: 3, other: 'bla'},
  92. {id: 4, content: 'Item 4', start: now},
  93. {id: 5, content: 'changed!', start: now},
  94. {id: 6, content: 'created!', start: now}
  95. ]);
  96. assert.equal(data.length, 5);
  97. data.clear();
  98. assert.equal(data.length, 0);
  99. assert.equal(data.get().length, 0);
  100. // test filtering and sorting
  101. data = new DataSet();
  102. data.add([
  103. {id: 1, age: 30, group: 2},
  104. {id: 2, age: 25, group: 4},
  105. {id: 3, age: 17, group: 2},
  106. {id: 4, age: 27, group: 3}
  107. ]);
  108. assert.deepEqual(data.get({order: 'age'}), [
  109. {id: 3, age: 17, group: 2},
  110. {id: 2, age: 25, group: 4},
  111. {id: 4, age: 27, group: 3},
  112. {id: 1, age: 30, group: 2}
  113. ]);
  114. assert.deepEqual(data.getIds({order: 'age'}), [3, 2, 4, 1]);
  115. assert.deepEqual(data.get({order: 'age', fields: ['id']}), [
  116. {id: 3},
  117. {id: 2},
  118. {id: 4},
  119. {id: 1}
  120. ]);
  121. assert.deepEqual(data.get({
  122. order: 'age',
  123. filter: function (item) {
  124. return item.group == 2;
  125. },
  126. fields: ['id']
  127. }), [
  128. {id: 3},
  129. {id: 1}
  130. ]);
  131. assert.deepEqual(data.getIds({
  132. order: 'age',
  133. filter: function (item) {
  134. return (item.group == 2);
  135. }
  136. }), [3, 1]);
  137. data.clear();
  138. // test if the setting of the showInternalIds works locally for a single get request
  139. data.add({content: 'Item 1'});
  140. data.add({content: 'Item 2'});
  141. assert.notStrictEqual(data.get()[0].id, undefined);
  142. // create a dataset with initial data
  143. var data = new DataSet([
  144. {id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  145. {id: 2, content: 'Item 2', start: now.toISOString()}
  146. ]);
  147. assert.deepEqual(data.getIds(), [1, 2]);
  148. // create a dataset with initial data and options
  149. var data = new DataSet([
  150. {_id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  151. {_id: 2, content: 'Item 2', start: now.toISOString()}
  152. ], {fieldId: '_id'});
  153. assert.deepEqual(data.getIds(), [1, 2]);
  154. // TODO: extensively test DataSet
  155. // TODO: test subscribing to events
  156. });
  157. it('should queue and flush changes', function () {
  158. var options = {queue: true};
  159. var dataset = new DataSet([
  160. {id: 1, content: 'Item 1'},
  161. {id: 2, content: 'Item 2'}
  162. ], options);
  163. assert.deepEqual(dataset.get(), [
  164. {id: 1, content: 'Item 1'},
  165. {id: 2, content: 'Item 2'}
  166. ]);
  167. dataset.add({id: 3, content: 'Item 3'});
  168. dataset.update({id: 1, content: 'Item 1 (updated)'});
  169. dataset.remove(2);
  170. assert.deepEqual(dataset.get(), [
  171. {id: 1, content: 'Item 1'},
  172. {id: 2, content: 'Item 2'}
  173. ]);
  174. dataset.flush();
  175. assert.deepEqual(dataset.get(), [
  176. {id: 1, content: 'Item 1 (updated)'},
  177. {id: 3, content: 'Item 3'}
  178. ]);
  179. });
  180. it('should queue and flush changes after a timeout', function (done) {
  181. var options = {queue: {delay: 100}};
  182. var dataset = new DataSet([
  183. {id: 1, content: 'Item 1'},
  184. {id: 2, content: 'Item 2'}
  185. ], options);
  186. assert.deepEqual(dataset.get(), [
  187. {id: 1, content: 'Item 1'},
  188. {id: 2, content: 'Item 2'}
  189. ]);
  190. dataset.add({id: 3, content: 'Item 3'});
  191. dataset.update({id: 1, content: 'Item 1 (updated)'});
  192. dataset.remove(2);
  193. assert.deepEqual(dataset.get(), [
  194. {id: 1, content: 'Item 1'},
  195. {id: 2, content: 'Item 2'}
  196. ]);
  197. setTimeout(function () {
  198. assert.deepEqual(dataset.get(), [
  199. {id: 1, content: 'Item 1 (updated)'},
  200. {id: 3, content: 'Item 3'}
  201. ]);
  202. done();
  203. }, 200)
  204. });
  205. it('should remove a queue from the dataset', function () {
  206. var options = {queue: true};
  207. var dataset = new DataSet([
  208. {id: 1, content: 'Item 1'},
  209. {id: 2, content: 'Item 2'}
  210. ], options);
  211. assert.deepEqual(dataset.get(), [
  212. {id: 1, content: 'Item 1'},
  213. {id: 2, content: 'Item 2'}
  214. ]);
  215. dataset.add({id: 3, content: 'Item 3'});
  216. dataset.update({id: 1, content: 'Item 1 (updated)'});
  217. dataset.remove(2);
  218. assert.deepEqual(dataset.get(), [
  219. {id: 1, content: 'Item 1'},
  220. {id: 2, content: 'Item 2'}
  221. ]);
  222. dataset.setOptions({queue: false}); // remove queue, should flush changes
  223. assert.deepEqual(dataset.get(), [
  224. {id: 1, content: 'Item 1 (updated)'},
  225. {id: 3, content: 'Item 3'}
  226. ]);
  227. dataset.add({id: 4, content: 'Item 4'});
  228. assert.deepEqual(dataset.get(), [
  229. {id: 1, content: 'Item 1 (updated)'},
  230. {id: 3, content: 'Item 3'},
  231. {id: 4, content: 'Item 4'}
  232. ]);
  233. });
  234. describe('add', function () {
  235. it('adds nothing for an empty array', function () {
  236. var dataset = new DataSet([]);
  237. var dataItems = [];
  238. assert.equal(dataset.add(dataItems).length, 0)
  239. });
  240. it('adds items of an array', function () {
  241. var dataset = new DataSet([]);
  242. var dataItems = [
  243. {_id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  244. {_id: 2, content: 'Item 2', start: new Date(now.valueOf())}
  245. ];
  246. assert.equal(dataset.add(dataItems).length, 2)
  247. });
  248. it('adds a single object', function () {
  249. var dataset = new DataSet([]);
  250. var dataItem = {_id: 1, content: 'Item 1', start: new Date(now.valueOf())};
  251. assert.equal(dataset.add(dataItem).length, 1)
  252. });
  253. it('throws an error when passed bad datatypes', function () {
  254. var dataset = new DataSet([]);
  255. assert.throws(function () { dataset.add(null) }, Error, "null type throws error");
  256. assert.throws(function () { dataset.add(undefined) }, Error, "undefined type throws error");
  257. });
  258. });
  259. describe('setOptions', function () {
  260. var dataset = new DataSet([
  261. {_id: 1, content: 'Item 1', start: new Date(now.valueOf())}
  262. ], {queue: true});
  263. it('does not update queue when passed an undefined queue', function () {
  264. var dataset = new DataSet([], {queue: true});
  265. dataset.setOptions({queue: undefined});
  266. assert.notEqual(dataset._queue, undefined)
  267. });
  268. it('destroys the queue when queue set to false', function () {
  269. var dataset = new DataSet([]);
  270. dataset.setOptions({queue: false});
  271. assert.equal(dataset._queue, undefined)
  272. });
  273. it('udpates queue options', function () {
  274. var dataset = new DataSet([]);
  275. dataset.setOptions({queue: {max: 5, delay: 3}});
  276. assert.equal(dataset._queue.max, 5);
  277. assert.equal(dataset._queue.delay, 3);
  278. });
  279. it('creates new queue given if none is set', function () {
  280. var dataset = new DataSet([], {queue: true});
  281. dataset._queue.destroy();
  282. dataset._queue = null;
  283. dataset.setOptions({queue: {max: 5, delay: 3}});
  284. assert.equal(dataset._queue.max, 5);
  285. assert.equal(dataset._queue.delay, 3);
  286. });
  287. });
  288. describe('on / off', function () {
  289. var dataset = new DataSet([
  290. {_id: 1, content: 'Item 1', start: new Date(now.valueOf())}
  291. ]);
  292. var count = 0;
  293. function inc() {count++;}
  294. it('fires for put', function () {
  295. var dataset = new DataSet([]);
  296. count = 0;
  297. // on
  298. dataset.on('add', inc);
  299. dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  300. assert.equal(count, 1);
  301. // off
  302. dataset.off('add', inc);
  303. dataset.add({_id: 2, content: 'Item 2', start: new Date(now.valueOf())});
  304. assert.equal(count, 1);
  305. });
  306. it('fires for remove', function () {
  307. var dataset = new DataSet([]);
  308. count = 0;
  309. // on
  310. dataset.on('remove', inc);
  311. var id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  312. dataset.remove(id);
  313. assert.equal(count, 1);
  314. // off
  315. dataset.off('remove', inc);
  316. id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  317. dataset.remove(id);
  318. assert.equal(count, 1);
  319. });
  320. it('fires for update', function () {
  321. var dataset = new DataSet([]);
  322. count = 0;
  323. // on
  324. dataset.on('update', inc);
  325. var id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  326. dataset.update({id: id, content: 'beep boop'});
  327. assert.equal(count, 1);
  328. // off
  329. dataset.off('update', inc);
  330. id = dataset.add({_id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  331. dataset.update({id: id, content: 'beep boop'});
  332. assert.equal(count, 1);
  333. });
  334. });
  335. });