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.

285 lines
7.4 KiB

  1. var assert = require('assert');
  2. var moment = require('moment');
  3. var DataSet = require('../lib/DataSet');
  4. var Queue = require('../lib/Queue');
  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. });