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.
This commit is contained in:
Georgios Kalpakas
2017-01-13 16:20:28 +02:00
committed by Miško Hevery
parent 49fce37013
commit 1f90f29369
35 changed files with 169 additions and 181 deletions

View File

@ -6,17 +6,18 @@
* found in the LICENSE file at https://angular.io/license
*/
import {INJECTOR_KEY} from '@angular/upgrade/src/aot/constants';
import {downgradeInjectable} from '@angular/upgrade/src/aot/downgrade_injectable';
import {INJECTOR_KEY} from '@angular/upgrade/src/common/constants';
import {downgradeInjectable} from '@angular/upgrade/src/common/downgrade_injectable';
export function main() {
describe('downgradeInjectable', () => {
it('should return an AngularJS annotated factory for the token', () => {
const factory = downgradeInjectable('someToken');
expect(factory[0]).toEqual(INJECTOR_KEY);
expect(factory[1]).toEqual(jasmine.any(Function));
expect(factory).toEqual(jasmine.any(Function));
expect((factory as any).$inject).toEqual([INJECTOR_KEY]);
const injector = {get: jasmine.createSpy('get').and.returnValue('service value')};
const value = (factory as any)[1](injector);
const value = factory(injector);
expect(injector.get).toHaveBeenCalledWith('someToken');
expect(value).toEqual('service value');
});

View File

@ -0,0 +1,25 @@
/**
* @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
*/
export function html(html: string): Element {
// Don't return `body` itself, because using it as a `$rootElement` for ng1
// will attach `$injector` to it and that will affect subsequent tests.
const body = document.body;
body.innerHTML = `<div>${html.trim()}</div>`;
const div = document.body.firstChild as Element;
if (div.childNodes.length === 1 && div.firstChild instanceof HTMLElement) {
return div.firstChild;
}
return div;
}
export function multiTrim(text: string): string {
return text.replace(/\n/g, '').replace(/\s\s+/g, ' ').trim();
}

View File

@ -8,7 +8,7 @@
import {Component} from '@angular/core';
import {describe, expect, it} from '@angular/core/testing/testing_internal';
import {getComponentInfo, parseFields} from '@angular/upgrade/src/metadata';
import {getComponentInfo, parseFields} from '@angular/upgrade/src/dynamic/metadata';
export function main() {
describe('upgrade metadata', () => {

View File

@ -0,0 +1,9 @@
/**
* @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
*/
export * from '../common/test_helpers';

View File

@ -10,8 +10,9 @@ import {ChangeDetectorRef, Class, Component, EventEmitter, Input, NO_ERRORS_SCHE
import {async, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import {UpgradeAdapter, UpgradeAdapterRef, sortProjectableNodes} from '@angular/upgrade/src/upgrade_adapter';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeAdapter, UpgradeAdapterRef, sortProjectableNodes} from '@angular/upgrade/src/dynamic/upgrade_adapter';
import {html, multiTrim} from './test_helpers';
export function main() {
describe('adapter: ng1 to ng2', () => {
@ -1923,23 +1924,7 @@ export function main() {
});
}
function multiTrim(text: string): string {
return text.replace(/\n/g, '').replace(/\s{2,}/g, ' ').trim();
}
function html(html: string): Element {
const body = document.body;
body.innerHTML = html;
if (body.childNodes.length == 1 && body.firstChild instanceof HTMLElement) {
return <Element>body.firstChild;
}
return body;
}
function nodes(html: string) {
const element = document.createElement('div');
element.innerHTML = html;
return Array.prototype.slice.call(element.childNodes);
}
return Array.prototype.slice.call(element.childNodes);

View File

@ -6,8 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Ng1Token} from '@angular/upgrade/src/angular_js';
import {compileFactory, injectorFactory, parseFactory, rootScopeFactory, setTempInjectorRef} from '@angular/upgrade/src/aot/angular1_providers';
import {Ng1Token} from '@angular/upgrade/src/common/angular1';
import {compileFactory, injectorFactory, parseFactory, rootScopeFactory, setTempInjectorRef} from '@angular/upgrade/src/static/angular1_providers';
export function main() {
describe('upgrade angular1_providers', () => {
@ -55,4 +55,4 @@ export function main() {
});
});
});
}
}

View File

@ -10,7 +10,7 @@ import {Component, Directive, ElementRef, Injector, Input, NgModule, NgZone, Sim
import {async} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeComponent, UpgradeModule, downgradeComponent} from '@angular/upgrade/static';
import {bootstrap, html} from '../test_helpers';

View File

@ -10,7 +10,7 @@ import {Component, Directive, ElementRef, Injector, NgModule, destroyPlatform} f
import {async} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeComponent, UpgradeModule, downgradeComponent} from '@angular/upgrade/static';
import {bootstrap, html} from '../test_helpers';

View File

@ -10,7 +10,7 @@ import {Component, EventEmitter, NgModule, OnChanges, OnDestroy, SimpleChanges,
import {async} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeModule, downgradeComponent} from '@angular/upgrade/static';
import {bootstrap, html, multiTrim} from '../test_helpers';

View File

@ -10,7 +10,7 @@ import {Component, Directive, ElementRef, Injector, Input, NgModule, destroyPlat
import {async} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeComponent, UpgradeModule, downgradeComponent} from '@angular/upgrade/static';
import {bootstrap, html, multiTrim} from '../test_helpers';

View File

@ -10,8 +10,8 @@ import {InjectionToken, Injector, NgModule, destroyPlatform} from '@angular/core
import {async} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import {$INJECTOR, INJECTOR_KEY} from '@angular/upgrade/src/aot/constants';
import * as angular from '@angular/upgrade/src/common/angular1';
import {$INJECTOR, INJECTOR_KEY} from '@angular/upgrade/src/common/constants';
import {UpgradeModule, downgradeInjectable} from '@angular/upgrade/static';
import {bootstrap, html} from '../test_helpers';

View File

@ -11,7 +11,7 @@ import {NgZone} from '@angular/core/src/zone/ng_zone';
import {fakeAsync, tick} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeModule} from '@angular/upgrade/static';
import {bootstrap, html} from '../test_helpers';

View File

@ -10,7 +10,7 @@ import {Component, Directive, ElementRef, EventEmitter, Injector, Input, NO_ERRO
import {async, fakeAsync, tick} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import * as angular from '@angular/upgrade/src/common/angular1';
import {UpgradeComponent, UpgradeModule, downgradeComponent} from '@angular/upgrade/static';
import {bootstrap, digest, html, multiTrim} from '../test_helpers';

View File

@ -5,11 +5,14 @@
* 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
*/
import {PlatformRef, Type} from '@angular/core';
import * as angular from '@angular/upgrade/src/angular_js';
import {$ROOT_SCOPE} from '@angular/upgrade/src/aot/constants';
import * as angular from '@angular/upgrade/src/common/angular1';
import {$ROOT_SCOPE} from '@angular/upgrade/src/common/constants';
import {UpgradeModule} from '@angular/upgrade/static';
export * from '../common/test_helpers';
export function bootstrap(
platform: PlatformRef, Ng2Module: Type<{}>, element: Element, ng1Module: angular.IModule) {
// We bootstrap the Angular module first; then when it is ready (async)
@ -25,21 +28,3 @@ export function digest(adapter: UpgradeModule) {
const $rootScope = adapter.$injector.get($ROOT_SCOPE) as angular.IRootScopeService;
$rootScope.$digest();
}
export function html(html: string): Element {
// Don't return `body` itself, because using it as a `$rootElement` for ng1
// will attach `$injector` to it and that will affect subsequent tests.
const body = document.body;
body.innerHTML = `<div>${html.trim()}</div>`;
const div = document.body.firstChild as Element;
if (div.childNodes.length === 1 && div.firstChild instanceof HTMLElement) {
return div.firstChild;
}
return div;
}
export function multiTrim(text: string): string {
return text.replace(/\n/g, '').replace(/\s\s+/g, ' ').trim();
}