-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathbuild-registry.ts
110 lines (94 loc) · 3.2 KB
/
build-registry.ts
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
import type Resolver from '@ember/application/resolver';
import ApplicationInstance from '@ember/application/instance';
import Application from '@ember/application';
import EmberObject from '@ember/object';
import require, { has } from 'require';
import Ember from 'ember';
/**
* Adds methods that are normally only on registry to the container. This is largely to support the legacy APIs
* that are not using `owner` (but are still using `this.container`).
*
* @private
* @param {Object} container the container to modify
*/
function exposeRegistryMethodsWithoutDeprecations(container: any) {
let methods = [
'register',
'unregister',
'resolve',
'normalize',
'typeInjection',
'injection',
'factoryInjection',
'factoryTypeInjection',
'has',
'options',
'optionsForType',
];
for (let i = 0, l = methods.length; i < l; i++) {
let method = methods[i];
if (method in container) {
container[method] = function (...args: unknown[]) {
return container._registry[method](...args);
};
}
}
}
const RegistryProxyMixin = (Ember as any)._RegistryProxyMixin;
const ContainerProxyMixin = (Ember as any)._ContainerProxyMixin;
const Owner = EmberObject.extend(RegistryProxyMixin, ContainerProxyMixin, {
_emberTestHelpersMockOwner: true,
});
/**
* @private
* @param {Object} resolver the resolver to use with the registry
* @returns {Object} owner, container, registry
*/
export default function (resolver: Resolver) {
let fallbackRegistry, registry, container;
let namespace = EmberObject.create({
Resolver: {
create() {
return resolver;
},
},
});
fallbackRegistry = (Application as any).buildRegistry(namespace);
// TODO: only do this on Ember < 3.13
fallbackRegistry.register(
'component-lookup:main',
(Ember as any).ComponentLookup
);
registry = new (Ember as any).Registry({
fallback: fallbackRegistry,
});
(ApplicationInstance as any).setupRegistry(registry);
// these properties are set on the fallback registry by `buildRegistry`
// and on the primary registry within the ApplicationInstance constructor
// but we need to manually recreate them since ApplicationInstance's are not
// exposed externally
registry.normalizeFullName = fallbackRegistry.normalizeFullName;
registry.makeToString = fallbackRegistry.makeToString;
registry.describe = fallbackRegistry.describe;
let owner = Owner.create({
__registry__: registry,
__container__: null,
});
container = registry.container({ owner: owner });
owner.__container__ = container;
exposeRegistryMethodsWithoutDeprecations(container);
if (has('ember-data/setup-container')) {
// ember-data is a proper ember-cli addon since 2.3; if no 'import
// 'ember-data'' is present somewhere in the tests, there is also no `DS`
// available on the globalContext and hence ember-data wouldn't be setup
// correctly for the tests; that's why we import and call setupContainer
// here; also see https://github.com/emberjs/data/issues/4071 for context
let setupContainer = require('ember-data/setup-container')['default'];
setupContainer(registry || container);
}
return {
registry,
container,
owner,
};
}