/
setupGlobals.js
49 lines (40 loc) · 1.18 KB
/
setupGlobals.js
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
import _ from 'underscore';
import {state} from '$state';
/**
* Setup global state for testing Backbone editor components.
*
* For some editor components like (some views or models) it's easier
* to depend on the global mutable state (available via the `$state`
* module alias) instead of injecting dependencies. This helper can be
* used to test these components in isolation.
*
* @param {Object} mapping -
* Properties to set on the global state. Functions as values will
* be evaluated and the return value will be assigned instead.
*/
export const setupGlobals = function(mapping) {
let globalsBackup;
beforeEach(() => {
if (globalsBackup) {
throw new Error('There can only be one setupGlobals call per test.');
}
globalsBackup = {};
setGlobals(mapping);
});
afterEach(() => {
_.each(mapping, function(_, key) {
state[key] = globalsBackup[key];
});
globalsBackup = null;
});
function setGlobals(mapping) {
_.each(mapping, function(value, key) {
globalsBackup[key] = state[key];
state[key] = typeof value === 'function' ? value.call(this) : value;
});
return mapping;
}
return {
setGlobals
};
};