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.

69 lines
1.8 KiB

  1. var assert = require('assert');
  2. var moment = require('moment');
  3. var DataSet = require('../lib/DataSet');
  4. var DataView = require('../lib/DataView');
  5. // TODO: improve DataView tests, split up in one test per function
  6. describe('DataView', function () {
  7. it('should work', function () {
  8. var groups = new DataSet();
  9. // add items with different groups
  10. groups.add([
  11. {id: 1, content: 'Item 1', group: 1},
  12. {id: 2, content: 'Item 2', group: 2},
  13. {id: 3, content: 'Item 3', group: 2},
  14. {id: 4, content: 'Item 4', group: 1},
  15. {id: 5, content: 'Item 5', group: 3}
  16. ]);
  17. var group2 = new DataView(groups, {
  18. filter: function (item) {
  19. return item.group == 2;
  20. }
  21. });
  22. // test getting the filtered data
  23. assert.deepEqual(group2.get(), [
  24. {id: 2, content: 'Item 2', group: 2},
  25. {id: 3, content: 'Item 3', group: 2}
  26. ]);
  27. // test filtering the view contents
  28. assert.deepEqual(group2.get({
  29. filter: function (item) {
  30. return item.id > 2;
  31. }
  32. }), [
  33. {id: 3, content: 'Item 3', group: 2}
  34. ]);
  35. // test event subscription
  36. var groupsTriggerCount = 0;
  37. groups.on('*', function () {
  38. groupsTriggerCount++;
  39. });
  40. var group2TriggerCount = 0;
  41. group2.on('*', function () {
  42. group2TriggerCount++;
  43. });
  44. groups.update({id:2, content: 'Item 2 (changed)'});
  45. assert.equal(groupsTriggerCount, 1);
  46. assert.equal(group2TriggerCount, 1);
  47. groups.update({id:5, content: 'Item 5 (changed)'});
  48. assert.equal(groupsTriggerCount, 2);
  49. assert.equal(group2TriggerCount, 1);
  50. // detach the view from groups
  51. group2.setData(null);
  52. assert.equal(groupsTriggerCount, 2);
  53. assert.equal(group2TriggerCount, 2);
  54. groups.update({id:2, content: 'Item 2 (changed again)'});
  55. assert.equal(groupsTriggerCount, 3);
  56. assert.equal(group2TriggerCount, 2);
  57. });
  58. });