-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
test-module.js
274 lines (220 loc) · 7.31 KB
/
test-module.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
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
import Ember from 'ember';
import AbstractTestModule from './abstract-test-module';
import { getResolver } from './test-resolver';
import buildRegistry from './build-registry';
import hasEmberVersion from './has-ember-version';
export default class extends AbstractTestModule {
constructor(subjectName, description, callbacks) {
// Allow `description` to be omitted, in which case it should
// default to `subjectName`
if (!callbacks && typeof description === 'object') {
callbacks = description;
description = subjectName;
}
super(description || subjectName, callbacks);
this.subjectName = subjectName;
this.description = description || subjectName;
this.resolver = this.callbacks.resolver || getResolver();
if (this.callbacks.integration && this.callbacks.needs) {
throw new Error("cannot declare 'integration: true' and 'needs' in the same module");
}
if (this.callbacks.integration) {
this.initIntegration(callbacks);
delete callbacks.integration;
}
this.initSubject();
this.initNeeds();
}
initIntegration(options) {
if (options.integration === 'legacy') {
throw new Error('`integration: \'legacy\'` is only valid for component tests.');
}
this.isIntegration = true;
}
initSubject() {
this.callbacks.subject = this.callbacks.subject || this.defaultSubject;
}
initNeeds() {
this.needs = [this.subjectName];
if (this.callbacks.needs) {
this.needs = this.needs.concat(this.callbacks.needs);
delete this.callbacks.needs;
}
}
initSetupSteps() {
this.setupSteps = [];
this.contextualizedSetupSteps = [];
if (this.callbacks.beforeSetup) {
this.setupSteps.push( this.callbacks.beforeSetup );
delete this.callbacks.beforeSetup;
}
this.setupSteps.push(this.setupContainer);
this.setupSteps.push(this.setupContext);
this.setupSteps.push(this.setupTestElements);
this.setupSteps.push(this.setupAJAXListeners);
if (this.callbacks.setup) {
this.contextualizedSetupSteps.push( this.callbacks.setup );
delete this.callbacks.setup;
}
}
initTeardownSteps() {
this.teardownSteps = [];
this.contextualizedTeardownSteps = [];
if (this.callbacks.teardown) {
this.contextualizedTeardownSteps.push( this.callbacks.teardown );
delete this.callbacks.teardown;
}
this.teardownSteps.push(this.teardownSubject);
this.teardownSteps.push(this.teardownContainer);
this.teardownSteps.push(this.teardownContext);
this.teardownSteps.push(this.teardownTestElements);
this.teardownSteps.push(this.teardownAJAXListeners);
if (this.callbacks.afterTeardown) {
this.teardownSteps.push( this.callbacks.afterTeardown );
delete this.callbacks.afterTeardown;
}
}
setupContainer() {
if (this.isIntegration || this.isLegacy) {
this._setupIntegratedContainer();
} else {
this._setupIsolatedContainer();
}
}
setupContext() {
var subjectName = this.subjectName;
var container = this.container;
var factory = function() {
return container.lookupFactory(subjectName);
};
super.setupContext({
container: this.container,
registry: this.registry,
factory: factory,
register() {
var target = this.registry || this.container;
return target.register.apply(target, arguments);
}
});
if (Ember.setOwner) {
Ember.setOwner(this.context, this.container.owner);
}
this.setupInject();
}
setupInject() {
var module = this;
var context = this.context;
if (Ember.inject) {
var keys = (Object.keys || Ember.keys)(Ember.inject);
keys.forEach(function(typeName) {
context.inject[typeName] = function(name, opts) {
var alias = (opts && opts.as) || name;
Ember.run(function() {
Ember.set(context, alias, module.container.lookup(typeName + ':' + name));
});
};
});
}
}
teardownSubject() {
var subject = this.cache.subject;
if (subject) {
Ember.run(function() {
Ember.tryInvoke(subject, 'destroy');
});
}
}
teardownContainer() {
var container = this.container;
Ember.run(function() {
container.destroy();
});
}
defaultSubject(options, factory) {
return factory.create(options);
}
// allow arbitrary named factories, like rspec let
contextualizeCallbacks() {
var callbacks = this.callbacks;
var context = this.context;
this.cache = this.cache || {};
this.cachedCalls = this.cachedCalls || {};
var keys = (Object.keys || Ember.keys)(callbacks);
var keysLength = keys.length;
if (keysLength) {
var deprecatedContext = this._buildDeprecatedContext(this, context);
for (var i = 0; i < keysLength; i++) {
this._contextualizeCallback(context, keys[i], deprecatedContext);
}
}
}
_contextualizeCallback(context, key, callbackContext) {
var _this = this;
var callbacks = this.callbacks;
var factory = context.factory;
context[key] = function(options) {
if (_this.cachedCalls[key]) { return _this.cache[key]; }
var result = callbacks[key].call(callbackContext, options, factory());
_this.cache[key] = result;
_this.cachedCalls[key] = true;
return result;
};
}
/*
Builds a version of the passed in context that contains deprecation warnings
for accessing properties that exist on the module.
*/
_buildDeprecatedContext(module, context) {
var deprecatedContext = Object.create(context);
var keysForDeprecation = Object.keys(module);
for (var i = 0, l = keysForDeprecation.length; i < l; i++) {
this._proxyDeprecation(module, deprecatedContext, keysForDeprecation[i]);
}
return deprecatedContext;
}
/*
Defines a key on an object to act as a proxy for deprecating the original.
*/
_proxyDeprecation(obj, proxy, key) {
if (typeof proxy[key] === 'undefined') {
Object.defineProperty(proxy, key, {
get() {
Ember.deprecate('Accessing the test module property "' + key + '" from a callback is deprecated.', false, { id: 'ember-test-helpers.test-module.callback-context', until: '0.6.0' });
return obj[key];
}
});
}
}
_setupContainer(isolated) {
var resolver = this.resolver;
var items = buildRegistry(!isolated ? resolver : Object.create(resolver, {
resolve: {
value() {}
}
}));
this.container = items.container;
this.registry = items.registry;
if (hasEmberVersion(1, 13)) {
var thingToRegisterWith = this.registry || this.container;
var router = resolver.resolve('router:main');
router = router || Ember.Router.extend();
thingToRegisterWith.register('router:main', router);
}
}
_setupIsolatedContainer() {
var resolver = this.resolver;
this._setupContainer(true);
var thingToRegisterWith = this.registry || this.container;
for (var i = this.needs.length; i > 0; i--) {
var fullName = this.needs[i - 1];
var normalizedFullName = resolver.normalize(fullName);
thingToRegisterWith.register(fullName, resolver.resolve(normalizedFullName));
}
if (!this.registry) {
this.container.resolver = function() {};
}
}
_setupIntegratedContainer() {
this._setupContainer();
}
}