This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtesting.js
More file actions
96 lines (76 loc) · 2.26 KB
/
testing.js
File metadata and controls
96 lines (76 loc) · 2.26 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
import {Injector} from './injector';
import {Inject, annotate, readAnnotations} from './annotations';
import {isFunction} from './util';
import {createProviderFromFnOrClass} from './providers';
var currentSpec = null;
beforeEach(function() {
currentSpec = this;
currentSpec.$$providers = [];
});
afterEach(function() {
currentSpec.$$providers = null;
currentSpec.$$injector = null;
currentSpec = null;
});
function isRunning() {
return !!currentSpec;
}
function use(mock) {
if (currentSpec && currentSpec.$$injector) {
throw new Error('Cannot call use() after inject() has already been called.');
}
var providerWrapper = {
provider: mock
};
var fn = function() {
currentSpec.$$providers.push(providerWrapper);
};
fn.as = function(token) {
if (currentSpec && currentSpec.$$injector) {
throw new Error('Cannot call as() after inject() has already been called.');
}
providerWrapper.as = token;
if (isRunning()) {
return undefined;
}
return fn;
};
if (isRunning()) {
fn();
}
return fn;
}
function inject(...params) {
var behavior = params.pop();
annotate(behavior, new Inject(...params));
var run = function() {
if (!currentSpec.$$injector) {
var providers = new Map();
var modules = [];
var annotations;
currentSpec.$$providers.forEach(function(providerWrapper) {
if (!providerWrapper.as) {
// load as a regular module
modules.push(providerWrapper.provider);
} else {
if (!isFunction(providerWrapper.provider)) {
// inlined mock
providers.set(providerWrapper.as, createProviderFromFnOrClass(function() {
return providerWrapper.provider;
}, {provide: {token: null, isPromise: false}, params: []}));
} else {
// a fn/class provider with overriden token
annotations = readAnnotations(providerWrapper.provider);
providers.set(providerWrapper.as, createProviderFromFnOrClass(providerWrapper.provider, annotations));
}
}
});
currentSpec.$$injector = new Injector(modules, [], null, providers);
}
currentSpec.$$injector.get(behavior);
};
return isRunning() ? run() : run;
}
export {
use, inject
};