forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-test.js
More file actions
281 lines (242 loc) · 10.3 KB
/
basic-test.js
File metadata and controls
281 lines (242 loc) · 10.3 KB
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
275
276
277
278
279
280
281
import { StrictResolver } from '@ember/engine/lib/strict-resolver';
const { module, test } = QUnit;
module('StrictResolver', function (hooks) {
let resolver;
let modules;
hooks.beforeEach(function () {
modules = {};
resolver = new StrictResolver(modules);
});
module('#resolve + #addModules', function () {
test('resolves the standard ember types via default pluralization', function (assert) {
// All of these go through the same `type + 's' -> dir` path. One table-
// driven test keeps the coverage without nine copies of the same setup.
let cases = [
{ fullName: 'adapter:post', key: './adapters/post' },
{ fullName: 'service:session', key: './services/session' },
{ fullName: 'helper:reverse-list', key: './helpers/reverse-list' },
{ fullName: 'component:my-widget', key: './components/my-widget' },
{ fullName: 'modifier:auto-focus', key: './modifiers/auto-focus' },
{ fullName: 'template:application', key: './templates/application' },
{ fullName: 'route:index', key: './routes/index' },
{ fullName: 'controller:application', key: './controllers/application' },
];
for (let { fullName, key } of cases) {
let expected = { fullName };
let longForm = new StrictResolver({ [key]: { default: expected } });
let shortForm = new StrictResolver({ [key]: expected });
assert.strictEqual(longForm.resolve(fullName), expected, `${fullName} -> ${key}`);
assert.strictEqual(shortForm.resolve(fullName), expected, `${fullName} -> ${key}`);
}
});
test('`type:main` resolves to the unpluralized `type` module key', function (assert) {
// The mainLookup strategy short-circuits pluralization: for any type
// `type:main` reads the module at the type's bare name.
resolver.addModules({
'./router': 'the-router',
'./store': 'the-store',
});
assert.strictEqual(resolver.resolve('router:main'), 'the-router');
assert.strictEqual(resolver.resolve('store:main'), 'the-store');
});
test('returns undefined for missing modules', function (assert) {
let result = resolver.resolve('service:nonexistent');
assert.strictEqual(result, undefined, 'undefined was returned');
});
test('can resolve self via resolver:current', function (assert) {
let self = resolver.resolve('resolver:current');
assert.ok(self, 'resolver:current returned a factory');
assert.strictEqual(self.create(), resolver, 'factory creates the resolver');
});
});
module('#addModules + normalization', function () {
test('normalization', function (assert) {
assert.strictEqual(resolver.normalize('controller:posts'), 'controller:posts');
assert.strictEqual(resolver.normalize('controller:postsIndex'), 'controller:posts-index');
assert.strictEqual(resolver.normalize('controller:posts.index'), 'controller:posts/index');
assert.strictEqual(resolver.normalize('controller:posts_index'), 'controller:posts-index');
assert.strictEqual(resolver.normalize('controller:posts-index'), 'controller:posts-index');
assert.strictEqual(
resolver.normalize('controller:posts.post.index'),
'controller:posts/post/index'
);
assert.strictEqual(
resolver.normalize('controller:posts_post.index'),
'controller:posts-post/index'
);
assert.strictEqual(
resolver.normalize('controller:posts.post_index'),
'controller:posts/post-index'
);
assert.strictEqual(
resolver.normalize('controller:posts.post-index'),
'controller:posts/post-index'
);
assert.strictEqual(
resolver.normalize('controller:blogPosts.index'),
'controller:blog-posts/index'
);
assert.strictEqual(
resolver.normalize('controller:blog/posts.index'),
'controller:blog/posts/index'
);
assert.strictEqual(
resolver.normalize('controller:blog/posts-index'),
'controller:blog/posts-index'
);
assert.strictEqual(
resolver.normalize('controller:blog/posts.post.index'),
'controller:blog/posts/post/index'
);
assert.strictEqual(
resolver.normalize('controller:blog/posts_post.index'),
'controller:blog/posts-post/index'
);
assert.strictEqual(
resolver.normalize('controller:blog/posts_post-index'),
'controller:blog/posts-post-index'
);
assert.strictEqual(
resolver.normalize('template:blog/posts_index'),
'template:blog/posts-index'
);
assert.strictEqual(resolver.normalize('service:userAuth'), 'service:user-auth');
// For helpers, we have special logic to avoid the situation of a template's
// `{{someName}}` being surprisingly shadowed by a `some-name` helper
assert.strictEqual(resolver.normalize('helper:make-fabulous'), 'helper:make-fabulous');
assert.strictEqual(resolver.normalize('helper:fabulize'), 'helper:fabulize');
assert.strictEqual(resolver.normalize('helper:make_fabulous'), 'helper:make-fabulous');
assert.strictEqual(resolver.normalize('helper:makeFabulous'), 'helper:makeFabulous');
// The same applies to components
assert.strictEqual(
resolver.normalize('component:fabulous-component'),
'component:fabulous-component'
);
assert.strictEqual(
resolver.normalize('component:fabulousComponent'),
'component:fabulousComponent'
);
assert.strictEqual(
resolver.normalize('template:components/fabulousComponent'),
'template:components/fabulousComponent'
);
// and modifiers
assert.strictEqual(
resolver.normalize('modifier:fabulous-component'),
'modifier:fabulous-component'
);
assert.strictEqual(
resolver.normalize('modifier:fabulouslyMissing'),
'modifier:fabulouslyMissing'
);
});
test('addModules allows adding modules after construction', function (assert) {
let expected = {};
resolver.addModules({
'./components/hello': { default: expected },
});
let component = resolver.resolve('component:hello');
assert.strictEqual(component, expected, 'component was resolved');
});
});
test('module paths with ./ prefix are normalized', function (assert) {
let resolver2 = new StrictResolver({
'./services/foo': { default: 'from-dot-slash' },
});
assert.strictEqual(
resolver2.resolve('service:foo'),
'from-dot-slash',
'./ prefix was stripped'
);
});
test('module paths with file extensions are normalized', function (assert) {
let resolver2 = new StrictResolver({
'./services/foo.ts': { default: 'from-ts' },
});
assert.strictEqual(resolver2.resolve('service:foo'), 'from-ts', 'file extension was stripped');
});
module('weird scenarios', function () {
test('shorthand class with a falsy or missing `default` falls back to the class itself', function (assert) {
// `.default` being falsy (undefined / null / 0 / '') means the shorthand
// value is used directly — matching the "if there's a default use it,
// else use the value" rule from the other direction.
class ClassWithUndefinedDefault {
static default = undefined;
static create() {
return new this();
}
}
class ClassWithoutDefault {
static create() {
return new this();
}
}
let r = new StrictResolver({
'./services/with-undefined-default': ClassWithUndefinedDefault,
'./services/without-default': ClassWithoutDefault,
});
assert.strictEqual(
r.resolve('service:with-undefined-default'),
ClassWithUndefinedDefault,
'undefined `.default` is treated as "no default", class is used'
);
assert.strictEqual(
r.resolve('service:without-default'),
ClassWithoutDefault,
'class without a `default` property is used as-is'
);
});
test('ES-module-shaped module with extra named exports still unwraps to `default`', function (assert) {
// A normal `import * as mod from './...'` yields an object whose
// `default` is the default export plus any named exports. The resolver
// returns the default; everything else on the namespace object is
// ignored. Documenting this so authors know the named exports don't
// leak through.
let defaultExport = { isDefault: true };
let registered = {
default: defaultExport,
named: 'not used',
another: 42,
};
let resolver2 = new StrictResolver({
'./services/extras': registered,
});
assert.strictEqual(resolver2.resolve('service:extras'), defaultExport);
});
test('`type:name` falls back to `types/name/index`, but only for comopnents', function (assert) {
let component = { component: true };
let helper = { helper: true };
let modifier = { modifier: true };
let route = { route: true };
resolver.addModules({
'./components/my-widget/index': { default: component },
'./helpers/format-date/index': { default: helper },
'./modifiers/on-intersect/index': { default: modifier },
'./routes/some-route/index': { default: route },
});
assert.strictEqual(resolver.resolve('component:my-widget'), component);
assert.strictEqual(resolver.resolve('helper:format-date'), undefined);
assert.strictEqual(resolver.resolve('modifier:on-intersect'), undefined);
assert.strictEqual(resolver.resolve('route:some-route'), undefined);
});
test('direct module takes precedence over the nested-colocation index', function (assert) {
let direct = { direct: true };
let nested = { nested: true };
resolver.addModules({
'./components/my-widget': { default: direct },
'./components/my-widget/index': { default: nested },
});
assert.strictEqual(
resolver.resolve('component:my-widget'),
direct,
'direct match wins over the colocation fallback'
);
});
});
test('config type pluralizes as config by default', function (assert) {
modules['./config/environment'] = 'env-config';
resolver.addModules(modules);
let result = resolver.resolve('config:environment');
assert.strictEqual(result, 'env-config', 'config/environment is found');
});
});