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.

287 lines
7.4 KiB

  1. var assert = require('assert');
  2. var vis = require('../dist/vis');
  3. var moment = vis.moment;
  4. var DataSet = vis.DataSet;
  5. var Queue = vis.Queue;
  6. // TODO: test the source code immediately, but this is ES6
  7. var now = new Date();
  8. describe('DataSet', function () {
  9. it('should work', function () {
  10. // TODO: improve DataSet tests, split up in one test per function
  11. var data = new DataSet({
  12. type: {
  13. start: 'Date',
  14. end: 'Date'
  15. }
  16. });
  17. // add single items with different date types
  18. data.add({id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  19. data.add({id: 2, content: 'Item 2', start: now.toISOString()});
  20. data.add([
  21. //{id: 3, content: 'Item 3', start: moment(now)}, // TODO: moment fails, not the same instance
  22. {id: 3, content: 'Item 3', start: now},
  23. {id: 4, content: 'Item 4', start: '/Date(' + now.valueOf() + ')/'}
  24. ]);
  25. var items = data.get();
  26. assert.equal(data.length, 4);
  27. assert.equal(items.length, 4);
  28. items.forEach(function (item) {
  29. assert.ok(item.start instanceof Date);
  30. });
  31. // get filtered fields only
  32. var sort = function (a, b) {
  33. return a.id > b.id;
  34. };
  35. assert.deepEqual(data.get({
  36. fields: ['id', 'content']
  37. }).sort(sort), [
  38. {id: 1, content: 'Item 1'},
  39. {id: 2, content: 'Item 2'},
  40. {id: 3, content: 'Item 3'},
  41. {id: 4, content: 'Item 4'}
  42. ]);
  43. // convert dates
  44. assert.deepEqual(data.get({
  45. fields: ['id', 'start'],
  46. type: {start: 'Number'}
  47. }).sort(sort), [
  48. {id: 1, start: now.valueOf()},
  49. {id: 2, start: now.valueOf()},
  50. {id: 3, start: now.valueOf()},
  51. {id: 4, start: now.valueOf()}
  52. ]);
  53. // get a single item
  54. assert.deepEqual(data.get(1, {
  55. fields: ['id', 'start'],
  56. type: {start: 'ISODate'}
  57. }), {
  58. id: 1,
  59. start: now.toISOString()
  60. });
  61. // remove an item
  62. data.remove(2);
  63. assert.deepEqual(data.get({
  64. fields: ['id']
  65. }).sort(sort), [
  66. {id: 1},
  67. {id: 3},
  68. {id: 4}
  69. ]);
  70. assert.equal(data.length, 3);
  71. // add an item
  72. data.add({id: 5, content: 'Item 5', start: now.valueOf()});
  73. assert.deepEqual(data.get({
  74. fields: ['id']
  75. }).sort(sort), [
  76. {id: 1},
  77. {id: 3},
  78. {id: 4},
  79. {id: 5}
  80. ]);
  81. assert.equal(data.length, 4);
  82. // update an item
  83. data.update({id: 5, content: 'changed!'}); // update item (extend existing fields)
  84. assert.equal(data.length, 4);
  85. data.remove(3); // remove existing item
  86. assert.equal(data.length, 3);
  87. data.add({id: 3, other: 'bla'}); // add new item
  88. assert.equal(data.length, 4);
  89. data.update({id: 6, content: 'created!', start: now.valueOf()}); // this item is not yet existing, create it
  90. assert.equal(data.length, 5);
  91. assert.deepEqual(data.get().sort(sort), [
  92. {id: 1, content: 'Item 1', start: now},
  93. {id: 3, other: 'bla'},
  94. {id: 4, content: 'Item 4', start: now},
  95. {id: 5, content: 'changed!', start: now},
  96. {id: 6, content: 'created!', start: now}
  97. ]);
  98. assert.equal(data.length, 5);
  99. data.clear();
  100. assert.equal(data.length, 0);
  101. assert.equal(data.get().length, 0);
  102. // test filtering and sorting
  103. data = new DataSet();
  104. data.add([
  105. {id: 1, age: 30, group: 2},
  106. {id: 2, age: 25, group: 4},
  107. {id: 3, age: 17, group: 2},
  108. {id: 4, age: 27, group: 3}
  109. ]);
  110. assert.deepEqual(data.get({order: 'age'}), [
  111. {id: 3, age: 17, group: 2},
  112. {id: 2, age: 25, group: 4},
  113. {id: 4, age: 27, group: 3},
  114. {id: 1, age: 30, group: 2}
  115. ]);
  116. assert.deepEqual(data.getIds({order: 'age'}), [3, 2, 4, 1]);
  117. assert.deepEqual(data.get({order: 'age', fields: ['id']}), [
  118. {id: 3},
  119. {id: 2},
  120. {id: 4},
  121. {id: 1}
  122. ]);
  123. assert.deepEqual(data.get({
  124. order: 'age',
  125. filter: function (item) {
  126. return item.group == 2;
  127. },
  128. fields: ['id']
  129. }), [
  130. {id: 3},
  131. {id: 1}
  132. ]);
  133. assert.deepEqual(data.getIds({
  134. order: 'age',
  135. filter: function (item) {
  136. return (item.group == 2);
  137. }
  138. }), [3, 1]);
  139. data.clear();
  140. // test if the setting of the showInternalIds works locally for a single get request
  141. data.add({content: 'Item 1'});
  142. data.add({content: 'Item 2'});
  143. assert.notStrictEqual(data.get()[0].id, undefined);
  144. // create a dataset with initial data
  145. var data = new DataSet([
  146. {id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  147. {id: 2, content: 'Item 2', start: now.toISOString()}
  148. ]);
  149. assert.deepEqual(data.getIds(), [1, 2]);
  150. // create a dataset with initial data and options
  151. var data = new DataSet([
  152. {_id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  153. {_id: 2, content: 'Item 2', start: now.toISOString()}
  154. ], {fieldId: '_id'});
  155. assert.deepEqual(data.getIds(), [1, 2]);
  156. // TODO: extensively test DataSet
  157. // TODO: test subscribing to events
  158. });
  159. it('should queue and flush changes', function () {
  160. var options = {queue: true};
  161. var dataset = new DataSet([
  162. {id: 1, content: 'Item 1'},
  163. {id: 2, content: 'Item 2'}
  164. ], options);
  165. assert.deepEqual(dataset.get(), [
  166. {id: 1, content: 'Item 1'},
  167. {id: 2, content: 'Item 2'}
  168. ]);
  169. dataset.add({id: 3, content: 'Item 3'});
  170. dataset.update({id: 1, content: 'Item 1 (updated)'});
  171. dataset.remove(2);
  172. assert.deepEqual(dataset.get(), [
  173. {id: 1, content: 'Item 1'},
  174. {id: 2, content: 'Item 2'}
  175. ]);
  176. dataset.flush();
  177. assert.deepEqual(dataset.get(), [
  178. {id: 1, content: 'Item 1 (updated)'},
  179. {id: 3, content: 'Item 3'}
  180. ]);
  181. });
  182. it('should queue and flush changes after a timeout', function (done) {
  183. var options = {queue: {delay: 100}};
  184. var dataset = new DataSet([
  185. {id: 1, content: 'Item 1'},
  186. {id: 2, content: 'Item 2'}
  187. ], options);
  188. assert.deepEqual(dataset.get(), [
  189. {id: 1, content: 'Item 1'},
  190. {id: 2, content: 'Item 2'}
  191. ]);
  192. dataset.add({id: 3, content: 'Item 3'});
  193. dataset.update({id: 1, content: 'Item 1 (updated)'});
  194. dataset.remove(2);
  195. assert.deepEqual(dataset.get(), [
  196. {id: 1, content: 'Item 1'},
  197. {id: 2, content: 'Item 2'}
  198. ]);
  199. setTimeout(function () {
  200. assert.deepEqual(dataset.get(), [
  201. {id: 1, content: 'Item 1 (updated)'},
  202. {id: 3, content: 'Item 3'}
  203. ]);
  204. done();
  205. }, 200)
  206. });
  207. it('should remove a queue from the dataset', function () {
  208. var options = {queue: true};
  209. var dataset = new DataSet([
  210. {id: 1, content: 'Item 1'},
  211. {id: 2, content: 'Item 2'}
  212. ], options);
  213. assert.deepEqual(dataset.get(), [
  214. {id: 1, content: 'Item 1'},
  215. {id: 2, content: 'Item 2'}
  216. ]);
  217. dataset.add({id: 3, content: 'Item 3'});
  218. dataset.update({id: 1, content: 'Item 1 (updated)'});
  219. dataset.remove(2);
  220. assert.deepEqual(dataset.get(), [
  221. {id: 1, content: 'Item 1'},
  222. {id: 2, content: 'Item 2'}
  223. ]);
  224. dataset.setOptions({queue: false}); // remove queue, should flush changes
  225. assert.deepEqual(dataset.get(), [
  226. {id: 1, content: 'Item 1 (updated)'},
  227. {id: 3, content: 'Item 3'}
  228. ]);
  229. dataset.add({id: 4, content: 'Item 4'});
  230. assert.deepEqual(dataset.get(), [
  231. {id: 1, content: 'Item 1 (updated)'},
  232. {id: 3, content: 'Item 3'},
  233. {id: 4, content: 'Item 4'}
  234. ]);
  235. });
  236. });