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
403 lines (318 loc) · 13.1 KB
/
basic-test.js
File metadata and controls
403 lines (318 loc) · 13.1 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { module, test } from 'qunit';
import { StrictResolver } from '@ember/engine/lib/strict-resolver';
module('strict-resolver | basic', function (hooks) {
let resolver;
let modules;
hooks.beforeEach(function () {
modules = {};
resolver = new StrictResolver(modules);
});
test('can lookup something', function (assert) {
let expected = {};
modules['./adapters/post'] = { default: expected };
resolver.addModules(modules);
let adapter = resolver.resolve('adapter:post');
assert.ok(adapter, 'adapter was returned');
assert.strictEqual(adapter, expected, 'default export was returned');
});
test('can lookup a service', function (assert) {
let expected = {};
modules['./services/session'] = { default: expected };
resolver.addModules(modules);
let service = resolver.resolve('service:session');
assert.ok(service, 'service was returned');
assert.strictEqual(service, expected, 'default export was returned');
});
test('can lookup a helper', function (assert) {
let expected = { isHelperInstance: true };
modules['./helpers/reverse-list'] = { default: expected };
resolver.addModules(modules);
let helper = resolver.resolve('helper:reverse-list');
assert.ok(helper, 'helper was returned');
assert.strictEqual(helper, expected, 'default export was returned');
});
test('can lookup a component', function (assert) {
let expected = { isComponentFactory: true };
modules['./components/my-widget'] = { default: expected };
resolver.addModules(modules);
let component = resolver.resolve('component:my-widget');
assert.ok(component, 'component was returned');
assert.strictEqual(component, expected, 'default export was returned');
});
test('can lookup a modifier', function (assert) {
let expected = { isModifier: true };
modules['./modifiers/auto-focus'] = { default: expected };
resolver.addModules(modules);
let modifier = resolver.resolve('modifier:auto-focus');
assert.ok(modifier, 'modifier was returned');
assert.strictEqual(modifier, expected, 'default export was returned');
});
test('can lookup a template', function (assert) {
let expected = { isTemplate: true };
modules['./templates/application'] = { default: expected };
resolver.addModules(modules);
let template = resolver.resolve('template:application');
assert.ok(template, 'template was returned');
assert.strictEqual(template, expected, 'default export was returned');
});
test('can lookup a view', function (assert) {
let expected = { isViewFactory: true };
modules['./views/queue-list'] = { default: expected };
resolver.addModules(modules);
let view = resolver.resolve('view:queue-list');
assert.ok(view, 'view was returned');
assert.strictEqual(view, expected, 'default export was returned');
});
test('can lookup a route', function (assert) {
let expected = { isRouteFactory: true };
modules['./routes/index'] = { default: expected };
resolver.addModules(modules);
let route = resolver.resolve('route:index');
assert.ok(route, 'route was returned');
assert.strictEqual(route, expected, 'default export was returned');
});
test('can lookup a controller', function (assert) {
let expected = { isController: true };
modules['./controllers/application'] = { default: expected };
resolver.addModules(modules);
let controller = resolver.resolve('controller:application');
assert.ok(controller, 'controller was returned');
assert.strictEqual(controller, expected, 'default export was returned');
});
test("will return the raw value if no 'default' is available", function (assert) {
modules['./fruits/orange'] = 'is awesome';
resolver.addModules(modules);
assert.strictEqual(resolver.resolve('fruit:orange'), 'is awesome', 'raw value was returned');
});
test("will unwrap the 'default' export automatically", function (assert) {
modules['./fruits/orange'] = { default: 'is awesome' };
resolver.addModules(modules);
assert.strictEqual(
resolver.resolve('fruit:orange'),
'is awesome',
'default export was unwrapped'
);
});
test('router:main is looked up as just "router" key', function (assert) {
modules['./router'] = 'the-router';
resolver.addModules(modules);
let result = resolver.resolve('router:main');
assert.strictEqual(result, 'the-router', 'router:main was looked up');
});
test('store:main is looked up as just "store" key', function (assert) {
modules['./store'] = 'the-store';
resolver.addModules(modules);
let result = resolver.resolve('store:main');
assert.strictEqual(result, 'the-store', 'store:main was looked up');
});
test('store:post is looked up as stores/post', function (assert) {
modules['./stores/post'] = 'whatever';
resolver.addModules(modules);
let result = resolver.resolve('store:post');
assert.strictEqual(result, 'whatever', 'store:post was looked up');
});
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');
});
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');
});
test('shorthand module registration (no default wrapper)', function (assert) {
let MyService = {
create() {
return this;
},
};
let resolver2 = new StrictResolver({
'./services/my-thing': MyService,
});
let result = resolver2.resolve('service:my-thing');
assert.strictEqual(result, MyService, 'shorthand module was resolved');
});
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('camel case modifier is not normalized to dasherized', function (assert) {
let expected = {};
resolver.addModules({
'./modifiers/other-thing': { default: 'oh no' },
'./modifiers/otherThing': { default: expected },
});
let modifier = resolver.resolve('modifier:otherThing');
assert.strictEqual(modifier, expected);
});
test('normalization is idempotent', function (assert) {
let examples = [
'controller:posts',
'controller:posts.post.index',
'controller:blog/posts.post_index',
'template:foo_bar',
];
examples.forEach((example) => {
assert.strictEqual(
resolver.normalize(resolver.normalize(example)),
resolver.normalize(example)
);
});
});
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');
});
test('custom plurals are supported', function (assert) {
let resolver2 = new StrictResolver({ './sheep/baaaaaa': 'whatever' }, { sheep: 'sheep' });
let result = resolver2.resolve('sheep:baaaaaa');
assert.strictEqual(result, 'whatever', 'custom plural was used');
});
test("'config' plural can be overridden", function (assert) {
let resolver2 = new StrictResolver(
{ './super-duper-config/environment': 'whatever' },
{ config: 'super-duper-config' }
);
let result = resolver2.resolve('config:environment');
assert.strictEqual(result, 'whatever', 'super-duper-config/environment is found');
});
test('irregular plurals must be opted into via the plurals option', function (assert) {
// Default pluralization is naive (type + 's'), matching ember-resolver's
// behavior. A consumer that wants proper English irregulars registers
// them up-front via the plurals map.
let r = new StrictResolver({ './children/alice': 'alice' }, { child: 'children' });
assert.strictEqual(r.resolve('child:alice'), 'alice');
});
test('can lookup a nested-colocation component (index file)', function (assert) {
let expected = { isComponentFactory: true };
resolver.addModules({
'./components/my-widget/index': { default: expected },
});
assert.strictEqual(resolver.resolve('component:my-widget'), expected);
});
test('nested-colocation also works for helpers and modifiers', function (assert) {
let helper = {};
let modifier = {};
resolver.addModules({
'./helpers/format-date/index': { default: helper },
'./modifiers/on-intersect/index': { default: modifier },
});
assert.strictEqual(resolver.resolve('helper:format-date'), helper);
assert.strictEqual(resolver.resolve('modifier:on-intersect'), modifier);
});
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'
);
});
});