forked from bpmn-io/bpmn-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
291 lines (242 loc) · 6.93 KB
/
Copy pathindex.js
File metadata and controls
291 lines (242 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* A helper file that may be used in test cases for bpmn-js and extensions.
*
* Provides the globals
*
* * bootstrapModeler(): bootstrap a modeler instance
* * bootstrapViewer(): bootstrap a viewer instance
* * inject(function(a, b) {}): inject the bpmn-js services in the given function
*
*
* In addition it provides the utilities
*
* * insertCSS(name, css): add a CSS file to be used in test cases
*
*
* It is recommended to expose the helper through a per-project utility and
* and perform custom bootstrapping (CSS, ...) in that utility.
*
* ```
* export * from 'bpmn-js/test/helper';
*
* import {
* insertCSS
* } from 'bpmn-js/test/helper';
*
* var fs = require('fs');
*
* // insert diagram.css
* insertCSS('diagram.css', fs.readFileSync('some-css.css', 'utf8'));
* ```
*/
import {
isFunction,
forEach,
merge
} from 'min-dash';
import TestContainer from 'mocha-test-container-support';
import Modeler from '../../lib/Modeler';
import NavigatedViewer from '../../lib/NavigatedViewer';
import Viewer from '../../lib/Viewer';
var OPTIONS, BPMN_JS;
import translationModule from './TranslationCollector';
export var collectTranslations = window.__env__ && window.__env__.COLLECT_TRANSLATIONS;
// inject logging translation module into default modules
if (collectTranslations) {
[ Modeler, Viewer, NavigatedViewer ].forEach(function(constructor) {
constructor.prototype._modules.push(translationModule);
});
}
export function bootstrapBpmnJS(BpmnJS, diagram, options, locals) {
return function() {
var testContainer;
// Make sure the test container is an optional dependency and we fall back
// to an empty <div> if it does not exist.
//
// This is needed if other libraries rely on this helper for testing
// while not adding the mocha-test-container-support as a dependency.
try {
// 'this' is the current test context
testContainer = TestContainer.get(this);
} catch (e) {
testContainer = document.createElement('div');
testContainer.classList.add('test-content-container');
document.body.appendChild(testContainer);
}
var _options = options,
_locals = locals;
if (_locals === undefined && isFunction(_options)) {
_locals = _options;
_options = null;
}
if (isFunction(_options)) {
_options = _options();
}
if (isFunction(_locals)) {
_locals = _locals();
}
_options = merge({
container: testContainer,
canvas: {
deferUpdate: false
}
}, OPTIONS, _options);
if (_locals) {
var mockModule = {};
forEach(_locals, function(v, k) {
mockModule[k] = ['value', v];
});
_options.modules = [].concat(_options.modules || [], [ mockModule ]);
}
if (_options.modules && !_options.modules.length) {
_options.modules = undefined;
}
// used to extract translations used during tests
if (collectTranslations) {
_options.additionalModules = [].concat(
_options.additionalModules || [],
[ translationModule ]
);
}
clearBpmnJS();
var instance = new BpmnJS(_options);
setBpmnJS(instance);
return instance.importXML(diagram).then(function(result) {
return { error: null, warnings: result.warnings };
}).catch(function(err) {
return { error: err, warnings: err.warnings };
});
};
}
/**
* Bootstrap the Modeler given the specified options and a number of locals (i.e. services)
*
* @example
*
* describe(function() {
*
* var mockEvents;
*
* beforeEach(bootstrapModeler('some-xml', function() {
* mockEvents = new Events();
*
* return {
* events: mockEvents
* };
* }));
*
* });
*
* @param {string} xml document to display
* @param {Object} (options) optional options to be passed to the diagram upon instantiation
* @param {Object|Function} locals the local overrides to be used by the diagram or a function that produces them
* @return {Function} a function to be passed to beforeEach
*/
export function bootstrapModeler(diagram, options, locals) {
return bootstrapBpmnJS(Modeler, diagram, options, locals);
}
/**
* Bootstrap the Viewer given the specified options and a number of locals (i.e. services)
*
* @example
*
* describe(function() {
*
* var mockEvents;
*
* beforeEach(bootstrapViewer('some-xml', function() {
* mockEvents = new Events();
*
* return {
* events: mockEvents
* };
* }));
*
* });
*
* @param {string} xml document to display
* @param {Object} (options) optional options to be passed to the diagram upon instantiation
* @param {Object|Function} locals the local overrides to be used by the diagram or a function that produces them
* @return {Function} a function to be passed to beforeEach
*/
export function bootstrapViewer(diagram, options, locals) {
return bootstrapBpmnJS(Viewer, diagram, options, locals);
}
/**
* Injects services of an instantiated diagram into the argument.
*
* Use it in conjunction with {@link #bootstrapModeler} or {@link #bootstrapViewer}.
*
* @example
*
* describe(function() {
*
* var mockEvents;
*
* beforeEach(bootstrapViewer(...));
*
* it('should provide mocked events', inject(function(events) {
* expect(events).to.eql(mockEvents);
* }));
*
* });
*
* @param {Function} fn the function to inject to
* @return {Function} a function that can be passed to it to carry out the injection
*/
export function inject(fn) {
return function() {
if (!BPMN_JS) {
throw new Error(
'no bootstraped bpmn-js instance, ' +
'ensure you created it via #boostrap(Modeler|Viewer)'
);
}
return BPMN_JS.invoke(fn);
};
}
/**
* Returns the current active BpmnJS instance.
*
* @return {BpmnJS}
*/
export function getBpmnJS() {
return BPMN_JS;
}
export function clearBpmnJS() {
// clean up old bpmn-js instance
if (BPMN_JS) {
BPMN_JS.destroy();
BPMN_JS = null;
}
}
// This method always resolves.
// It helps us to do done(err) within the same block.
export function createViewer(container, viewerInstance, xml, diagramId) {
clearBpmnJS();
var viewer = new viewerInstance({ container: container });
setBpmnJS(viewer);
return viewer.importXML(xml, diagramId).then(function(result) {
return { warnings: result.warnings, viewer: viewer };
}).catch(function(err) {
return { error: err, viewer: viewer, warnings: err.warnings };
});
}
export function setBpmnJS(instance) {
BPMN_JS = instance;
}
export function insertCSS(name, css) {
if (document.querySelector('[data-css-file="' + name + '"]')) {
return;
}
var head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.setAttribute('data-css-file', name);
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}