forked from emberjs/ember-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-module-for-integration.js
104 lines (88 loc) · 2.7 KB
/
test-module-for-integration.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
import Ember from 'ember';
import TestModule from './test-module';
import { getResolver } from './test-resolver';
import { getContext, setContext } from './test-context';
export default TestModule.extend({
init: function(name, description, callbacks) {
this._super.call(this, name, description, callbacks);
this.setupSteps.push(this.setupIntegrationHelpers);
this.teardownSteps.push(this.teardownView);
},
setupIntegrationHelpers: function() {
var self = this;
var context = this.context;
context.dispatcher = Ember.EventDispatcher.create();
context.dispatcher.setup({}, '#ember-testing');
this.actionHooks = {};
context.render = function(template) {
if (Ember.isArray(template)) {
template = template.join('');
}
if (typeof template === 'string') {
template = Ember.Handlebars.compile(template);
}
self.view = Ember.View.create({
context: context,
controller: self,
template: template,
container: self.container
});
Ember.run(function() {
self.view.appendTo('#ember-testing');
});
};
context.$ = function() {
return self.view.$.apply(self.view, arguments);
};
context.set = function(key, value) {
Ember.run(function() {
Ember.set(context, key, value);
});
};
context.get = function(key) {
return Ember.get(context, key);
};
context.on = function(actionName, handler) {
self.actionHooks[actionName] = handler;
};
},
setupContainer: function() {
var resolver = getResolver();
var namespace = Ember.Object.create({
Resolver: { create: function() { return resolver; } }
});
if (Ember.Application.buildRegistry) {
var registry;
registry = Ember.Application.buildRegistry(namespace);
registry.register('component-lookup:main', Ember.ComponentLookup);
this.registry = registry;
this.container = registry.container();
} else {
this.container = Ember.Application.buildContainer(namespace);
this.container.register('component-lookup:main', Ember.ComponentLookup);
}
},
setupContext: function() {
setContext({
container: this.container,
factory: function() {},
dispatcher: null
});
this.context = getContext();
},
send: function(actionName) {
var hook = this.actionHooks[actionName];
if (!hook) {
throw new Error("integration testing template received unexpected action " + actionName);
}
hook.apply(this, Array.prototype.slice.call(arguments, 1));
},
teardownView: function() {
var view = this.view;
if (view) {
Ember.run(function() {
view.destroy();
});
}
}
});