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.

104 lines
2.5 KiB

  1. var assert = require('assert'),
  2. moment = require('moment'),
  3. vis = require('../vis.js'),
  4. DataSet = vis.DataSet;
  5. var now = new Date();
  6. var data = new DataSet({
  7. fieldTypes: {
  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. // cast dates
  38. assert.deepEqual(data.get({
  39. fields: ['id', 'start'],
  40. fieldTypes: {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. fieldTypes: {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. // TODO: extensively test DataSet