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
|
|
|
|
*/
|
|
|
|
|
2019-01-10 10:18:05 -05:00
|
|
|
import {IInjectorService, Ng1Token} from '@angular/upgrade/static/src/common/angular1';
|
2018-02-15 12:14:20 -05:00
|
|
|
import {compileFactory, injectorFactory, parseFactory, rootScopeFactory, setTempInjectorRef} from '@angular/upgrade/static/src/static/angular1_providers';
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-10-19 16:41:04 -04:00
|
|
|
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', () => {
|
2019-01-10 10:18:05 -05:00
|
|
|
const mockInjector = {get: () => undefined, has: () => false};
|
2016-10-19 16:41:04 -04:00
|
|
|
setTempInjectorRef(mockInjector);
|
|
|
|
const injector = injectorFactory();
|
|
|
|
expect(injector).toBe(mockInjector);
|
|
|
|
});
|
|
|
|
|
2019-02-08 06:51:51 -05:00
|
|
|
it('should throw if the injector value is not set', () => {
|
|
|
|
// Ensure the injector is not set. This shouldn't be necessary, but on CI there seems to be
|
|
|
|
// some race condition with previous tests not being cleaned up properly.
|
|
|
|
// Related:
|
|
|
|
// - https://github.com/angular/angular/pull/28045
|
|
|
|
// - https://github.com/angular/angular/pull/28181
|
|
|
|
setTempInjectorRef(null as any);
|
2019-01-10 10:18:05 -05:00
|
|
|
|
2019-02-08 06:51:51 -05:00
|
|
|
expect(injectorFactory).toThrowError();
|
2017-07-20 12:06:13 -04:00
|
|
|
});
|
|
|
|
|
2016-10-19 16:41:04 -04:00
|
|
|
it('should unset the injector after the first call (to prevent memory leaks)', () => {
|
2019-01-10 10:18:05 -05:00
|
|
|
const mockInjector = {get: () => undefined, has: () => false};
|
2016-10-19 16:41:04 -04:00
|
|
|
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
|
|
|
}
|