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.

141 lines
5.0 KiB

  1. var assert = require('assert');
  2. var jsdom_global = require('jsdom-global');
  3. var canvasMockify = require('./canvas-mock');
  4. var Configurator = require('../lib/shared/Configurator').default;
  5. var Network = require('../lib/network/Network');
  6. var {allOptions, configureOptions} = require('../lib/network/options.js');
  7. describe('Configurator', function () {
  8. beforeEach(function() {
  9. this.jsdom_global = jsdom_global(
  10. "<div id='mynetwork'></div><div id='other'></div>",
  11. { skipWindowCheck: true}
  12. );
  13. canvasMockify(window);
  14. this.container = document.getElementById('mynetwork');
  15. });
  16. afterEach(function() {
  17. this.jsdom_global();
  18. this.container.remove();
  19. this.container = undefined;
  20. });
  21. describe('constructor', function () {
  22. it('sets extends options with default options', function () {
  23. var config = new Configurator();
  24. assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
  25. });
  26. });
  27. describe('setOptions', function () {
  28. it('with undefined will not modify defaults', function () {
  29. var config = new Configurator(Network, this.container);
  30. config.setOptions();
  31. assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
  32. });
  33. it('with undefined will set enabled to false', function () {
  34. var config = new Configurator(Network, this.container);
  35. config.options.enabled = false;
  36. config.setOptions();
  37. assert.equal(config.options.enabled, false);
  38. });
  39. it('with string sets filter and set enabled to true', function () {
  40. var config = new Configurator(Network, this.container);
  41. config.setOptions('stringFilter!');
  42. assert.equal(config.options.filter, 'stringFilter!');
  43. assert.equal(config.options.enabled, true);
  44. });
  45. it('with array sets filter and set enabled to true', function () {
  46. var config = new Configurator(Network, this.container);
  47. config.setOptions(['array', 'Filter', '!']);
  48. assert.equal(config.options.filter, 'array,Filter,!');
  49. assert.equal(config.options.enabled, true);
  50. });
  51. it('with object sets filter', function () {
  52. var config = new Configurator(Network, this.container);
  53. config.setOptions(
  54. {container: 'newContainer',
  55. filter: 'newFilter',
  56. showButton: 'newShowButton',
  57. enabled: false
  58. });
  59. assert.equal(config.options.container, 'newContainer');
  60. assert.equal(config.options.filter, 'newFilter');
  61. assert.equal(config.options.showButton, 'newShowButton');
  62. assert.equal(config.options.enabled, false);
  63. });
  64. it('with object and filter is false enabled will be false', function () {
  65. var config = new Configurator(Network, this.container);
  66. config.setOptions({filter: false});
  67. assert.equal(config.options.enabled, false);
  68. });
  69. it('with boolean true sets filter', function () {
  70. var config = new Configurator(Network, this.container);
  71. config.setOptions(true);
  72. assert.equal(config.options.enabled, true);
  73. });
  74. it('with boolean false sets filter', function () {
  75. var config = new Configurator(Network, this.container);
  76. config.setOptions(false);
  77. assert.equal(config.options.enabled, false);
  78. });
  79. it('with function sets filter', function () {
  80. var config = new Configurator(Network, this.container);
  81. config.setOptions(function () {});
  82. assert.equal(config.options.enabled, true);
  83. });
  84. it('with null raises exception', function () {
  85. var config = new Configurator(Network, this.container);
  86. assert.throws(function () {config.setOptions(null)}, TypeError, null);
  87. });
  88. });
  89. describe('setModuleOptions', function () {
  90. it('creates no new dom elements if enabled is false', function () {
  91. var config = new Configurator(Network, this.container);
  92. config.setModuleOptions();
  93. assert.equal(this.container.children.length, 0);
  94. });
  95. it('adds div with vis-configuration-wrapper class when enabled', function () {
  96. var config = new Configurator(Network, this.container);
  97. config.options.enabled = true;
  98. config.setModuleOptions();
  99. assert.equal(this.container.children.length, 1);
  100. assert.equal(this.container.children[0].className, 'vis-configuration-wrapper');
  101. });
  102. it('overwrites config.container with config.options.container', function () {
  103. var config = new Configurator(Network, this.container);
  104. config.options.enabled = true;
  105. config.options.container = document.getElementById('other');
  106. config.setModuleOptions();
  107. assert.equal(config.container, config.options.container);
  108. assert.equal(config.container.children[0].className, 'vis-configuration-wrapper');
  109. });
  110. });
  111. // TODO: This test needs work
  112. describe('getOptions', function () {
  113. xit('creates no new dom elements if enabled is false', function () {
  114. var config = new Configurator(Network, this.container, configureOptions);
  115. var options = config.getOptions();
  116. });
  117. });
  118. });