forked from emberjs/ember-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-context.js
137 lines (112 loc) · 3.6 KB
/
setup-context.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
import { run, next } from '@ember/runloop';
import { set, setProperties, get, getProperties } from '@ember/object';
import buildOwner from './build-owner';
import { _setupPromiseListeners } from './ext/rsvp';
import { _setupAJAXHooks } from './settled';
import Ember from 'ember';
import { Promise } from 'rsvp';
import { assert } from '@ember/debug';
import global from './global';
import { getResolver } from './resolver';
import { getApplication } from './application';
let __test_context__;
export function setContext(context) {
__test_context__ = context;
}
export function getContext() {
return __test_context__;
}
export function unsetContext() {
__test_context__ = undefined;
}
export function pauseTest() {
let context = getContext();
if (!context || typeof context.pauseTest !== 'function') {
throw new Error(
'Cannot call `pauseTest` without having first called `setupTest` or `setupRenderingTest`.'
);
}
return context.pauseTest();
}
export function resumeTest() {
let context = getContext();
if (!context || typeof context.resumeTest !== 'function') {
throw new Error(
'Cannot call `resumeTest` without having first called `setupTest` or `setupRenderingTest`.'
);
}
return context.resumeTest();
}
/*
* Responsible for:
*
* - sets the "global testing context" to the provided context
* - create an owner object and set it on the provided context (e.g. this.owner)
* - setup this.set, this.setProperties, this.get, and this.getProperties to the provided context
* - setting up AJAX listeners
* - setting up RSVP promise integration
*/
export default function(context, options = {}) {
Ember.testing = true;
setContext(context);
return new Promise(resolve => {
// ensure "real" async and not "fake" RSVP based async
next(resolve);
})
.then(() => {
let { resolver } = options;
let buildOwnerOptions;
// This handles precendence, specifying a specific option of
// resolver always trumps whatever is auto-detected, then we fallback to
// the suite-wide registrations
//
// At some later time this can be extended to support specifying a custom
// engine or application...
if (resolver) {
buildOwnerOptions = { resolver };
} else {
buildOwnerOptions = {
resolver: getResolver(),
application: getApplication(),
};
}
return buildOwner(buildOwnerOptions);
})
.then(owner => {
context.owner = owner;
context.set = function(key, value) {
let ret = run(function() {
return set(context, key, value);
});
return ret;
};
context.setProperties = function(hash) {
let ret = run(function() {
return setProperties(context, hash);
});
return ret;
};
context.get = function(key) {
return get(context, key);
};
context.getProperties = function(...args) {
return getProperties(context, args);
};
let resume;
context.resumeTest = function resumeTest() {
assert('Testing has not been paused. There is nothing to resume.', resume);
resume();
global.resumeTest = resume = undefined;
};
context.pauseTest = function pauseTest() {
console.info('Testing paused. Use `resumeTest()` to continue.'); // eslint-disable-line no-console
return new Promise(resolve => {
resume = resolve;
global.resumeTest = resumeTest;
}, 'TestAdapter paused promise');
};
_setupAJAXHooks();
_setupPromiseListeners();
return context;
});
}