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.

99 lines
2.6 KiB

  1. var assert = require('assert');
  2. var vis = require('../dist/vis');
  3. var moment = vis.moment;
  4. var DataSet = vis.DataSet;
  5. var DataView = vis.DataView;
  6. // TODO: test the source code immediately, but this is ES6
  7. // TODO: improve DataView tests, split up in one test per function
  8. describe('DataView', function () {
  9. it('should work', function () {
  10. var groups = new DataSet();
  11. // add items with different groups
  12. groups.add([
  13. {id: 1, content: 'Item 1', group: 1},
  14. {id: 2, content: 'Item 2', group: 2},
  15. {id: 3, content: 'Item 3', group: 2},
  16. {id: 4, content: 'Item 4', group: 1},
  17. {id: 5, content: 'Item 5', group: 3}
  18. ]);
  19. var group2 = new DataView(groups, {
  20. filter: function (item) {
  21. return item.group == 2;
  22. }
  23. });
  24. // test getting the filtered data
  25. assert.deepEqual(group2.get(), [
  26. {id: 2, content: 'Item 2', group: 2},
  27. {id: 3, content: 'Item 3', group: 2}
  28. ]);
  29. assert.equal(group2.length, 2);
  30. // test filtering the view contents
  31. assert.deepEqual(group2.get({
  32. filter: function (item) {
  33. return item.id > 2;
  34. }
  35. }), [
  36. {id: 3, content: 'Item 3', group: 2}
  37. ]);
  38. // test event subscription
  39. var groupsTriggerCount = 0;
  40. groups.on('*', function () {
  41. groupsTriggerCount++;
  42. });
  43. var group2TriggerCount = 0;
  44. group2.on('*', function () {
  45. group2TriggerCount++;
  46. });
  47. groups.update({id:2, content: 'Item 2 (changed)'});
  48. assert.equal(groupsTriggerCount, 1);
  49. assert.equal(group2TriggerCount, 1);
  50. assert.equal(group2.length, 2);
  51. groups.update({id:5, content: 'Item 5 (changed)'});
  52. assert.equal(groupsTriggerCount, 2);
  53. assert.equal(group2TriggerCount, 1);
  54. assert.equal(group2.length, 2);
  55. // detach the view from groups
  56. group2.setData(null);
  57. assert.equal(groupsTriggerCount, 2);
  58. assert.equal(group2TriggerCount, 2);
  59. assert.equal(group2.length, 0);
  60. groups.update({id:2, content: 'Item 2 (changed again)'});
  61. assert.equal(groupsTriggerCount, 3);
  62. assert.equal(group2TriggerCount, 2);
  63. // test updating of .length property
  64. group2.setData(groups);
  65. assert.equal(group2.length, 2);
  66. // add a new item
  67. groups.add({id: 6, content: 'Item 6', group: 2});
  68. assert.equal(group2.length, 3);
  69. // change an items group to 2
  70. groups.update({id: 4, group: 2});
  71. assert.equal(group2.length, 4);
  72. // change an items group to 1
  73. groups.update({id: 4, group: 1});
  74. assert.equal(group2.length, 3);
  75. // remove an item
  76. groups.remove(2);
  77. assert.equal(group2.length, 2);
  78. // remove all items
  79. groups.clear();
  80. assert.equal(group2.length, 0);
  81. });
  82. });