index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {
  2. isFunction,
  3. forEach,
  4. merge
  5. } from 'min-dash';
  6. import TestContainer from 'mocha-test-container-support';
  7. import Diagram from '../../lib/Diagram';
  8. var OPTIONS, DIAGRAM_JS;
  9. /**
  10. * Bootstrap the diagram given the specified options and a number of locals (i.e. services)
  11. *
  12. * @example
  13. *
  14. * describe(function() {
  15. *
  16. * var mockEvents;
  17. *
  18. * beforeEach(bootstrapDiagram(function() {
  19. * mockEvents = new Events();
  20. *
  21. * return {
  22. * events: mockEvents
  23. * };
  24. * }));
  25. *
  26. * });
  27. *
  28. * @param {Object} (options) optional options to be passed to the diagram upon instantiation
  29. * @param {Object|Function} locals the local overrides to be used by the diagram or a function that produces them
  30. * @return {Function} a function to be passed to beforeEach
  31. */
  32. export function bootstrapDiagram(options, locals) {
  33. return function() {
  34. var testContainer;
  35. // Make sure the test container is an optional dependency and we fall back
  36. // to an empty <div> if it does not exist.
  37. //
  38. // This is needed if other libraries rely on this helper for testing
  39. // while not adding the mocha-test-container-support as a dependency.
  40. try {
  41. testContainer = TestContainer.get(this);
  42. } catch (e) {
  43. testContainer = document.createElement('div');
  44. document.body.appendChild(testContainer);
  45. }
  46. testContainer.classList.add('test-container');
  47. var _options = options,
  48. _locals = locals;
  49. if (!_locals && isFunction(_options)) {
  50. _locals = _options;
  51. _options = null;
  52. }
  53. if (isFunction(_options)) {
  54. _options = _options();
  55. }
  56. if (isFunction(_locals)) {
  57. _locals = _locals();
  58. }
  59. _options = merge({
  60. canvas: {
  61. container: testContainer,
  62. deferUpdate: false
  63. }
  64. }, OPTIONS, _options);
  65. var mockModule = {};
  66. forEach(_locals, function(v, k) {
  67. mockModule[k] = ['value', v];
  68. });
  69. _options.modules = [].concat(_options.modules || [], [ mockModule ]);
  70. // remove previous instance
  71. cleanup();
  72. DIAGRAM_JS = new Diagram(_options);
  73. };
  74. }
  75. /**
  76. * Injects services of an instantiated diagram into the argument.
  77. *
  78. * Use it in conjunction with {@link #bootstrapDiagram}.
  79. *
  80. * @example
  81. *
  82. * describe(function() {
  83. *
  84. * var mockEvents;
  85. *
  86. * beforeEach(bootstrapDiagram(...));
  87. *
  88. * it('should provide mocked events', inject(function(events) {
  89. * expect(events).toBe(mockEvents);
  90. * }));
  91. *
  92. * });
  93. *
  94. * @param {Function} fn the function to inject to
  95. * @return {Function} a function that can be passed to it to carry out the injection
  96. */
  97. export function inject(fn) {
  98. return function() {
  99. if (!DIAGRAM_JS) {
  100. throw new Error('no bootstraped diagram, ensure you created it via #bootstrapDiagram');
  101. }
  102. return DIAGRAM_JS.invoke(fn);
  103. };
  104. }
  105. function cleanup() {
  106. if (!DIAGRAM_JS) {
  107. return;
  108. }
  109. DIAGRAM_JS.destroy();
  110. }
  111. export function insertCSS(name, css) {
  112. if (document.querySelector('[data-css-file="' + name + '"]')) {
  113. return;
  114. }
  115. var head = document.head || document.getElementsByTagName('head')[0],
  116. style = document.createElement('style');
  117. style.setAttribute('data-css-file', name);
  118. style.type = 'text/css';
  119. if (style.styleSheet) {
  120. style.styleSheet.cssText = css;
  121. } else {
  122. style.appendChild(document.createTextNode(css));
  123. }
  124. head.appendChild(style);
  125. }
  126. export function getDiagramJS() {
  127. return DIAGRAM_JS;
  128. }