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.

171 lines
3.9 KiB

  1. var assert = require('assert'),
  2. moment = require('moment'),
  3. vis = require('../dist/vis.js'),
  4. DataSet = vis.DataSet;
  5. var now = new Date();
  6. var data = new DataSet({
  7. type: {
  8. start: 'Date',
  9. end: 'Date'
  10. }
  11. });
  12. // add single items with different date types
  13. data.add({id: 1, content: 'Item 1', start: new Date(now.valueOf())});
  14. data.add({id: 2, content: 'Item 2', start: now.toISOString()});
  15. data.add([
  16. //{id: 3, content: 'Item 3', start: moment(now)}, // TODO: moment fails, not the same instance
  17. {id: 3, content: 'Item 3', start: now},
  18. {id: 4, content: 'Item 4', start: '/Date(' + now.valueOf() + ')/'}
  19. ]);
  20. var items = data.get();
  21. assert.equal(items.length, 4);
  22. items.forEach(function (item) {
  23. assert.ok(item.start instanceof Date);
  24. });
  25. // get filtered fields only
  26. var sort = function (a, b) {
  27. return a.id > b.id;
  28. };
  29. assert.deepEqual(data.get({
  30. fields: ['id', 'content']
  31. }).sort(sort), [
  32. {id: 1, content: 'Item 1'},
  33. {id: 2, content: 'Item 2'},
  34. {id: 3, content: 'Item 3'},
  35. {id: 4, content: 'Item 4'}
  36. ]);
  37. // convert dates
  38. assert.deepEqual(data.get({
  39. fields: ['id', 'start'],
  40. type: {start: 'Number'}
  41. }).sort(sort), [
  42. {id: 1, start: now.valueOf()},
  43. {id: 2, start: now.valueOf()},
  44. {id: 3, start: now.valueOf()},
  45. {id: 4, start: now.valueOf()}
  46. ]);
  47. // get a single item
  48. assert.deepEqual(data.get(1, {
  49. fields: ['id', 'start'],
  50. type: {start: 'ISODate'}
  51. }), {
  52. id: 1,
  53. start: now.toISOString()
  54. });
  55. // remove an item
  56. data.remove(2);
  57. assert.deepEqual(data.get({
  58. fields: ['id']
  59. }).sort(sort), [
  60. {id: 1},
  61. {id: 3},
  62. {id: 4}
  63. ]);
  64. // add an item
  65. data.add({id: 5, content: 'Item 5', start: now.valueOf()});
  66. assert.deepEqual(data.get({
  67. fields: ['id']
  68. }).sort(sort), [
  69. {id: 1},
  70. {id: 3},
  71. {id: 4},
  72. {id: 5}
  73. ]);
  74. // update an item
  75. data.update({id: 5, content: 'changed!'}); // update item (extend existing fields)
  76. data.remove(3); // remove existing item
  77. data.add({id: 3, other: 'bla'}); // add new item
  78. data.update({id: 6, content: 'created!', start: now.valueOf()}); // this item is not yet existing, create it
  79. assert.deepEqual(data.get().sort(sort), [
  80. {id: 1, content: 'Item 1', start: now},
  81. {id: 3, other: 'bla'},
  82. {id: 4, content: 'Item 4', start: now},
  83. {id: 5, content: 'changed!', start: now},
  84. {id: 6, content: 'created!', start: now}
  85. ]);
  86. data.clear();
  87. assert.equal(data.get().length, 0);
  88. // test filtering and sorting
  89. data = new vis.DataSet();
  90. data.add([
  91. {id:1, age: 30, group: 2},
  92. {id:2, age: 25, group: 4},
  93. {id:3, age: 17, group: 2},
  94. {id:4, age: 27, group: 3}
  95. ]);
  96. assert.deepEqual(data.get({order: 'age'}), [
  97. {id:3, age: 17, group: 2},
  98. {id:2, age: 25, group: 4},
  99. {id:4, age: 27, group: 3},
  100. {id:1, age: 30, group: 2}
  101. ]);
  102. assert.deepEqual(data.getIds({order: 'age'}), [3,2,4,1]);
  103. assert.deepEqual(data.get({order: 'age', fields: ['id']}), [
  104. {id:3},
  105. {id:2},
  106. {id:4},
  107. {id:1}
  108. ]);
  109. assert.deepEqual(data.get({
  110. order: 'age',
  111. filter: function (item) {
  112. return item.group == 2;
  113. },
  114. fields: ['id']
  115. }), [
  116. {id:3},
  117. {id:1}
  118. ]);
  119. assert.deepEqual(data.getIds({
  120. order: 'age',
  121. filter: function (item) {
  122. return (item.group == 2);
  123. }
  124. }), [3,1]);
  125. data.clear();
  126. // test if the setting of the showInternalIds works locally for a single get request
  127. data.add({content: 'Item 1'});
  128. data.add({content: 'Item 2'});
  129. assert.notStrictEqual(data.get()[0].id, undefined);
  130. // create a dataset with initial data
  131. var data = new DataSet([
  132. {id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  133. {id: 2, content: 'Item 2', start: now.toISOString()}
  134. ]);
  135. assert.deepEqual(data.getIds(), [1, 2]);
  136. // create a dataset with initial data and options
  137. var data = new DataSet([
  138. {_id: 1, content: 'Item 1', start: new Date(now.valueOf())},
  139. {_id: 2, content: 'Item 2', start: now.toISOString()}
  140. ], {fieldId: '_id'});
  141. assert.deepEqual(data.getIds(), [1, 2]);
  142. // TODO: extensively test DataSet
  143. // TODO: test subscribing to events