-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
abstract-test-module.js
200 lines (165 loc) · 5 KB
/
abstract-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
import { run } from '@ember/runloop';
import { Promise as EmberPromise, resolve } from 'rsvp';
import { assign } from '@ember/polyfills';
import { _setupPromiseListeners, _teardownPromiseListeners } from './ext/rsvp';
import { _setupAJAXHooks, _teardownAJAXHooks } from '@ember/test-helpers/settled';
import { getContext, setContext, unsetContext } from '@ember/test-helpers';
import Ember from 'ember';
export default class {
constructor(name, options) {
this.context = undefined;
this.name = name;
this.callbacks = options || {};
this.initSetupSteps();
this.initTeardownSteps();
}
setup(assert) {
Ember.testing = true;
Ember.run.backburner.DEBUG = true;
return this.invokeSteps(this.setupSteps, this, assert).then(() => {
this.contextualizeCallbacks();
return this.invokeSteps(this.contextualizedSetupSteps, this.context, assert);
});
}
teardown(assert) {
return this.invokeSteps(this.contextualizedTeardownSteps, this.context, assert)
.then(() => {
return this.invokeSteps(this.teardownSteps, this, assert);
})
.then(() => {
this.cache = null;
this.cachedCalls = null;
})
.finally(function() {
Ember.testing = false;
});
}
initSetupSteps() {
this.setupSteps = [];
this.contextualizedSetupSteps = [];
if (this.callbacks.beforeSetup) {
this.setupSteps.push(this.callbacks.beforeSetup);
delete this.callbacks.beforeSetup;
}
this.setupSteps.push(this.setupContext);
this.setupSteps.push(this.setupTestElements);
this.setupSteps.push(this.setupAJAXListeners);
this.setupSteps.push(this.setupPromiseListeners);
if (this.callbacks.setup) {
this.contextualizedSetupSteps.push(this.callbacks.setup);
delete this.callbacks.setup;
}
}
invokeSteps(steps, context, assert) {
steps = steps.slice();
function nextStep() {
var step = steps.shift();
if (step) {
// guard against exceptions, for example missing components referenced from needs.
return new EmberPromise(resolve => {
resolve(step.call(context, assert));
}).then(nextStep);
} else {
return resolve();
}
}
return nextStep();
}
contextualizeCallbacks() {}
initTeardownSteps() {
this.teardownSteps = [];
this.contextualizedTeardownSteps = [];
if (this.callbacks.teardown) {
this.contextualizedTeardownSteps.push(this.callbacks.teardown);
delete this.callbacks.teardown;
}
this.teardownSteps.push(this.teardownContext);
this.teardownSteps.push(this.teardownTestElements);
this.teardownSteps.push(this.teardownAJAXListeners);
this.teardownSteps.push(this.teardownPromiseListeners);
if (this.callbacks.afterTeardown) {
this.teardownSteps.push(this.callbacks.afterTeardown);
delete this.callbacks.afterTeardown;
}
}
setupTestElements() {
let testElementContainer = document.querySelector('#ember-testing-container');
if (!testElementContainer) {
testElementContainer = document.createElement('div');
testElementContainer.setAttribute('id', 'ember-testing-container');
document.body.appendChild(testElementContainer);
}
let testEl = document.querySelector('#ember-testing');
if (!testEl) {
let element = document.createElement('div');
element.setAttribute('id', 'ember-testing');
testElementContainer.appendChild(element);
this.fixtureResetValue = '';
} else {
this.fixtureResetValue = testElementContainer.innerHTML;
}
}
setupContext(options) {
let context = this.getContext();
assign(
context,
{
dispatcher: null,
inject: {},
},
options
);
this.setToString();
setContext(context);
this.context = context;
}
setContext(context) {
this.context = context;
}
getContext() {
if (this.context) {
return this.context;
}
return (this.context = getContext() || {});
}
setToString() {
this.context.toString = () => {
if (this.subjectName) {
return `test context for: ${this.subjectName}`;
}
if (this.name) {
return `test context for: ${this.name}`;
}
};
}
setupAJAXListeners() {
_setupAJAXHooks();
}
teardownAJAXListeners() {
_teardownAJAXHooks();
}
setupPromiseListeners() {
_setupPromiseListeners();
}
teardownPromiseListeners() {
_teardownPromiseListeners();
}
teardownTestElements() {
document.getElementById('ember-testing-container').innerHTML = this.fixtureResetValue;
// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
if (Ember.View && Ember.View.views) {
Ember.View.views = {};
}
}
teardownContext() {
var context = this.context;
this.context = undefined;
unsetContext();
if (context && context.dispatcher && !context.dispatcher.isDestroyed) {
run(function() {
context.dispatcher.destroy();
});
}
}
}