-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathcontroller_test.js
198 lines (158 loc) · 4.9 KB
/
controller_test.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
/* global EmberDev */
import Controller from '../../controllers/controller';
import Service from '../../system/service';
import { Mixin, get } from 'ember-metal';
import EmberObject from '../../system/object';
import inject from '../../inject';
import { buildOwner } from 'internal-test-helpers';
QUnit.module('Controller event handling');
QUnit.test('can access `actions` hash via `_actions` [DEPRECATED]', function() {
expect(2);
let controller = Controller.extend({
actions: {
foo: function() {
ok(true, 'called foo action');
}
}
}).create();
expectDeprecation(function() {
controller._actions.foo();
}, 'Usage of `_actions` is deprecated, use `actions` instead.');
});
QUnit.test('Action can be handled by a function on actions object', function() {
expect(1);
let TestController = Controller.extend({
actions: {
poke() {
ok(true, 'poked');
}
}
});
let controller = TestController.create();
controller.send('poke');
});
QUnit.test('A handled action can be bubbled to the target for continued processing', function() {
expect(2);
let TestController = Controller.extend({
actions: {
poke() {
ok(true, 'poked 1');
return true;
}
}
});
let controller = TestController.create({
target: Controller.extend({
actions: {
poke() {
ok(true, 'poked 2');
}
}
}).create()
});
controller.send('poke');
});
QUnit.test('Action can be handled by a superclass\' actions object', function() {
expect(4);
let SuperController = Controller.extend({
actions: {
foo() {
ok(true, 'foo');
},
bar(msg) {
equal(msg, 'HELLO');
}
}
});
let BarControllerMixin = Mixin.create({
actions: {
bar(msg) {
equal(msg, 'HELLO');
this._super(msg);
}
}
});
let IndexController = SuperController.extend(BarControllerMixin, {
actions: {
baz() {
ok(true, 'baz');
}
}
});
let controller = IndexController.create({});
controller.send('foo');
controller.send('bar', 'HELLO');
controller.send('baz');
});
QUnit.module('Controller deprecations');
QUnit.module('Controller Content -> Model Alias');
QUnit.test('`model` is aliased as `content`', function() {
expect(1);
let controller = Controller.extend({
model: 'foo-bar'
}).create();
equal(controller.get('content'), 'foo-bar', 'content is an alias of model');
});
QUnit.test('`content` is moved to `model` when `model` is unset', function() {
expect(2);
let controller;
ignoreDeprecation(function() {
controller = Controller.extend({
content: 'foo-bar'
}).create();
});
equal(controller.get('model'), 'foo-bar', 'model is set properly');
equal(controller.get('content'), 'foo-bar', 'content is set properly');
});
QUnit.test('specifying `content` (without `model` specified) results in deprecation', function() {
expect(1);
let controller;
expectDeprecation(function() {
controller = Controller.extend({
content: 'foo-bar'
}).create();
}, 'Do not specify `content` on a Controller, use `model` instead.');
});
QUnit.test('specifying `content` (with `model` specified) does not result in deprecation', function() {
expect(3);
expectNoDeprecation();
let controller = Controller.extend({
content: 'foo-bar',
model: 'blammo'
}).create();
equal(get(controller, 'content'), 'foo-bar');
equal(get(controller, 'model'), 'blammo');
});
QUnit.module('Controller injected properties');
if (!EmberDev.runningProdBuild) {
QUnit.test('defining a controller on a non-controller should fail assertion', function() {
expectAssertion(function() {
let owner = buildOwner();
let AnObject = EmberObject.extend({
foo: inject.controller('bar')
});
owner.register('foo:main', AnObject);
owner._lookupFactory('foo:main');
}, /Defining an injected controller property on a non-controller is not allowed./);
});
}
QUnit.test('controllers can be injected into controllers', function() {
let owner = buildOwner();
owner.register('controller:post', Controller.extend({
postsController: inject.controller('posts')
}));
owner.register('controller:posts', Controller.extend());
let postController = owner.lookup('controller:post');
let postsController = owner.lookup('controller:posts');
equal(postsController, postController.get('postsController'), 'controller.posts is injected');
});
QUnit.test('services can be injected into controllers', function() {
let owner = buildOwner();
owner.register('controller:application', Controller.extend({
authService: inject.service('auth')
}));
owner.register('service:auth', Service.extend());
let appController = owner.lookup('controller:application');
let authService = owner.lookup('service:auth');
equal(authService, appController.get('authService'), 'service.auth is injected');
});