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.

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