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.

176 lines
4.6 KiB

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