2016-10-19 16:41:04 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 09:20:28 -05:00
|
|
|
import {Ng1Token} from '@angular/upgrade/src/common/angular1';
|
|
|
|
import {compileFactory, injectorFactory, parseFactory, rootScopeFactory, setTempInjectorRef} from '@angular/upgrade/src/static/angular1_providers';
|
2016-10-19 16:41:04 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('upgrade angular1_providers', () => {
|
|
|
|
describe('compileFactory', () => {
|
|
|
|
it('should retrieve and return `$compile`', () => {
|
|
|
|
const services: {[key: string]: any} = {$compile: 'foo'};
|
|
|
|
const mockInjector = {get: (name: Ng1Token): any => services[name], has: () => true};
|
|
|
|
|
|
|
|
expect(compileFactory(mockInjector)).toBe('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('injectorFactory', () => {
|
|
|
|
it('should return the injector value that was previously set', () => {
|
|
|
|
const mockInjector = {get: () => {}, has: () => false};
|
|
|
|
setTempInjectorRef(mockInjector);
|
|
|
|
const injector = injectorFactory();
|
|
|
|
expect(injector).toBe(mockInjector);
|
|
|
|
});
|
|
|
|
|
2017-07-20 12:06:13 -04:00
|
|
|
it('should throw if the injector value has not been set yet', () => {
|
|
|
|
const mockInjector = {get: () => {}, has: () => false};
|
|
|
|
expect(injectorFactory).toThrowError();
|
|
|
|
});
|
|
|
|
|
2016-10-19 16:41:04 -04:00
|
|
|
it('should unset the injector after the first call (to prevent memory leaks)', () => {
|
|
|
|
const mockInjector = {get: () => {}, has: () => false};
|
|
|
|
setTempInjectorRef(mockInjector);
|
|
|
|
injectorFactory();
|
2017-07-20 12:06:13 -04:00
|
|
|
expect(injectorFactory).toThrowError(); // ...because it has been unset
|
2016-10-19 16:41:04 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('parseFactory', () => {
|
|
|
|
it('should retrieve and return `$parse`', () => {
|
|
|
|
const services: {[key: string]: any} = {$parse: 'bar'};
|
|
|
|
const mockInjector = {get: (name: Ng1Token): any => services[name], has: () => true};
|
|
|
|
|
|
|
|
expect(parseFactory(mockInjector)).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rootScopeFactory', () => {
|
|
|
|
it('should retrieve and return `$rootScope`', () => {
|
|
|
|
const services: {[key: string]: any} = {$rootScope: 'baz'};
|
|
|
|
const mockInjector = {get: (name: Ng1Token): any => services[name], has: () => true};
|
|
|
|
|
|
|
|
expect(rootScopeFactory(mockInjector)).toBe('baz');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 09:20:28 -05:00
|
|
|
}
|