Skip to content

Commit

Permalink
feat(upgrade): provide test helpers for wiring up injectors
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Jul 29, 2017
1 parent d71ae27 commit 0ca84ba
Show file tree
Hide file tree
Showing 28 changed files with 428 additions and 12 deletions.
2 changes: 2 additions & 0 deletions aio/tools/transforms/angular-api-package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ module.exports = new Package('angular-api', [basePackage, typeScriptPackage])
'router/testing/index.ts',
'router/upgrade/index.ts',
'upgrade/index.ts',
'upgrade/testing/index.ts',
'upgrade/static/index.ts',
'upgrade/static/testing/index.ts',
];

// API Examples
Expand Down
2 changes: 1 addition & 1 deletion aio/tools/transforms/authors-package/api-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const packageMap = {
'platform-webworker': ['platform-webworker/index.ts'],
'platform-webworker-dynamic': 'platform-webworker-dynamic/index.ts',
router: ['router/index.ts', 'router/testing/index.ts', 'router/upgrade/index.ts'],
upgrade: ['upgrade/index.ts', 'upgrade/static/index.ts']
upgrade: ['upgrade/index.ts', 'upgrade/static/index.ts', 'upgrade/testing/index.ts']
};


Expand Down
4 changes: 3 additions & 1 deletion integration/typings_test_ts21/include-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as platformServerTesting from '@angular/platform-server/testing';
import * as router from '@angular/router';
import * as routerTesting from '@angular/router/testing';
import * as upgrade from '@angular/upgrade';
import * as upgradeTesting from '@angular/upgrade/testing';

export default {
compiler,
Expand All @@ -37,5 +38,6 @@ export default {
platformServerTesting,
router,
routerTesting,
upgrade
upgrade,
upgradeTesting
};
4 changes: 3 additions & 1 deletion integration/typings_test_ts22/include-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as platformServerTesting from '@angular/platform-server/testing';
import * as router from '@angular/router';
import * as routerTesting from '@angular/router/testing';
import * as upgrade from '@angular/upgrade';
import * as upgradeTesting from '@angular/upgrade/testing';

export default {
compiler,
Expand All @@ -37,5 +38,6 @@ export default {
platformServerTesting,
router,
routerTesting,
upgrade
upgrade,
upgradeTesting
};
4 changes: 3 additions & 1 deletion integration/typings_test_ts23/include-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as platformServerTesting from '@angular/platform-server/testing';
import * as router from '@angular/router';
import * as routerTesting from '@angular/router/testing';
import * as upgrade from '@angular/upgrade';
import * as upgradeTesting from '@angular/upgrade/testing';

export default {
compiler,
Expand All @@ -37,5 +38,6 @@ export default {
platformServerTesting,
router,
routerTesting,
upgrade
upgrade,
upgradeTesting
};
15 changes: 9 additions & 6 deletions packages/common/http/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
* found in the LICENSE file at https://angular.io/license
*/

const globals = {
'@angular/core': 'ng.core',
'@angular/platform-browser': 'ng.platformBrowser',
'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx'
};

export default {
entry: '../../../dist/packages-dist/common/@angular/common/http.es5.js',
dest: '../../../dist/packages-dist/common/bundles/common-http.umd.js',
format: 'umd',
exports: 'named',
moduleName: 'ng.commmon.http',
globals: {
'@angular/core': 'ng.core',
'@angular/platform-browser': 'ng.platformBrowser',
'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx'
}
external: Object.keys(globals),
globals
};
5 changes: 4 additions & 1 deletion packages/upgrade/src/common/angular1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,15 @@ let angular: {
IInjectorService,
module: (prefix: string, dependencies?: string[]) => IModule,
element: (e: Element | string) => IAugmentedJQuery,
injector: (modules: Array<string|IInjectable>, strictDI?: boolean) => IInjectorService,
version: {major: number},
resumeBootstrap: () => void,
getTestability: (e: Element) => ITestabilityService
} = <any>{
bootstrap: noNg,
module: noNg,
element: noNg,
injector: noNg,
version: noNg,
resumeBootstrap: noNg,
getTestability: noNg
Expand Down Expand Up @@ -269,7 +271,8 @@ export const module = (prefix: string, dependencies?: string[]) =>
angular.module(prefix, dependencies);

export const element = (e: Element | string) => angular.element(e);

export const injector = (modules: Array<string|IInjectable>, strictDI?: boolean) =>
angular.injector(modules, strictDI);
export const resumeBootstrap = () => angular.resumeBootstrap();

export const getTestability = (e: Element) => angular.getTestability(e);
Expand Down
3 changes: 2 additions & 1 deletion packages/upgrade/static/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import resolve from 'rollup-plugin-node-resolve';

const globals = {
'@angular/core': 'ng.core'
'@angular/core': 'ng.core',
'@angular/platform-browser': 'ng.platformBrowser',
};

export default {
Expand Down
1 change: 1 addition & 0 deletions packages/upgrade/static/testing/index.ts
7 changes: 7 additions & 0 deletions packages/upgrade/static/testing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@angular/upgrade/static/testing",
"typings": "../testing.d.ts",
"main": "../bundles/upgrade-static-testing.umd.js",
"module": "../@angular/upgrade/static/testing.es5.js",
"es2015": "../@angular/upgrade/static/testing.js"
}
1 change: 1 addition & 0 deletions packages/upgrade/static/testing/public_api.ts
28 changes: 28 additions & 0 deletions packages/upgrade/static/testing/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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
*/

import resolve from 'rollup-plugin-node-resolve';

const globals = {
'@angular/core': 'ng.core',
'@angular/common': 'ng.common',
'@angular/compiler': 'ng.compiler',
'@angular/platform-browser': 'ng.platformBrowser',
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
};

export default {
entry: '../../../../dist/packages-dist/upgrade/@angular/upgrade/static/testing.es5.js',
dest: '../../../../dist/packages-dist/upgrade/bundles/upgrade-static-testing.umd.js',
format: 'umd',
exports: 'named',
moduleName: 'ng.upgrade.static.testing',
plugins: [resolve()],
external: Object.keys(globals),
globals: globals
};
1 change: 1 addition & 0 deletions packages/upgrade/static/testing/src
1 change: 1 addition & 0 deletions packages/upgrade/static/testing/test
23 changes: 23 additions & 0 deletions packages/upgrade/static/testing/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "../tsconfig-build",
"compilerOptions": {
"paths": {
"@angular/core": ["../../dist/packages/core"],
"@angular/core/testing": ["../../dist/packages/core/testing"],
"@angular/common": ["../../dist/packages/common"],
"@angular/compiler": ["../../dist/packages/compiler"],
"@angular/platform-browser": ["../../dist/packages/platform-browser"],
"@angular/platform-browser-dynamic": ["../../dist/packages/platform-browser-dynamic"]
}
},
"files": [
"public_api.ts",
"../../../../node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/upgrade/static/testing"
}
}
9 changes: 9 additions & 0 deletions packages/upgrade/testing/index.ts
Original file line number Diff line number Diff line change
@@ -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 './public_api';
7 changes: 7 additions & 0 deletions packages/upgrade/testing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@angular/upgrade/testing",
"typings": "../testing.d.ts",
"main": "../bundles/upgrade-testing.umd.js",
"module": "../@angular/upgrade/testing.es5.js",
"es2015": "../@angular/upgrade/testing.js"
}
10 changes: 10 additions & 0 deletions packages/upgrade/testing/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @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 {createAngularTestingModule} from './src/create_angular_testing_module';
export {createAngularJSTestingModule} from './src/create_angularjs_testing_module';
28 changes: 28 additions & 0 deletions packages/upgrade/testing/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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
*/

import resolve from 'rollup-plugin-node-resolve';

const globals = {
'@angular/core': 'ng.core',
'@angular/common': 'ng.common',
'@angular/compiler': 'ng.compiler',
'@angular/platform-browser': 'ng.platformBrowser',
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
};

export default {
entry: '../../../dist/packages-dist/upgrade/@angular/upgrade/testing.es5.js',
dest: '../../../dist/packages-dist/upgrade/bundles/upgrade-testing.umd.js',
format: 'umd',
exports: 'named',
moduleName: 'ng.upgrade.testing',
plugins: [resolve()],
external: Object.keys(globals),
globals: globals
};
67 changes: 67 additions & 0 deletions packages/upgrade/testing/src/create_angular_testing_module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @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
*/

import {Injector, NgModule} from '@angular/core';

import * as angular from '../../src/common/angular1';
import {$INJECTOR, INJECTOR_KEY} from '../../src/common/constants';

let $injector: angular.IInjectorService;
let injector: Injector;

export function $injectorFactory() {
return $injector;
}

@NgModule({providers: [{provide: $INJECTOR, useFactory: $injectorFactory}]})
export class AngularTestingModule {
constructor(i: Injector) { injector = i; }
}

/**
* Create an `NgModule` that can be used as an import to configure the `TestBed`
* for testing services in Angular that may have dependencies on "upgraded" AngularJS services.
*
* In the following code snippet, we are configuring the TestBed with two imports.
* The `AppModule` is the Angular part of our hybrid application and the `appModule` is the AngularJS part.
*
* ```typescript
* import {TestBed} from '@angular/core/testing';
* import {createAngularTestingModule} from '@angular/upgrade/testing';
*
* import {appModule, AppModule, Inventory} from './app';
*
* TestBed.configureTestingModule({
* imports: [
* createAngularTestingModule([appModule.name]),
* AppModule
* ]
* });
* ```
*
* Once this is done we can get hold of services via the Angular `Injector` as normal.
* Services that are (or have dependencies on) an upgraded AngularJS service, will be instantiated as needed
* by the AngularJS `$injector`.
*
* In the following code snippet, `Inventory` is an Angular service that depends upon an AngularJS service.
*
* ```typescript
* it('should ...', () => {
* const inventory = TestBed.get(Inventory) as Inventory;
* });
* ```
*
* @param angularJSModules a collection of the names of AngularJS modules to include in the configuration
* @param strictDI whether the AngularJS injector should have `strictDI` enabled. Defaults to `true`.
*/
export function createAngularTestingModule(angularJSModules: string[], strictDI = true) {
angular.module('$$angularJSTestingModule', angularJSModules)
.factory(INJECTOR_KEY, () => injector);
$injector = angular.injector(['ng', '$$angularJSTestingModule'], strictDI);
return AngularTestingModule;
}
60 changes: 60 additions & 0 deletions packages/upgrade/testing/src/create_angularjs_testing_module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @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
*/

import {Injector} from '@angular/core';
import {TestBed} from '@angular/core/testing';

import * as angular from '../../src/common/angular1';
import {$INJECTOR, INJECTOR_KEY} from '../../src/common/constants';


/**
* Create an AngularJS module that can be used to configure the `$injector`
* for testing services in AngularJS that may have dependencies on "downgraded" Angular services.
*
* In the following code snippet, we are configuring the `$injector` with two modules:
* The AngularJS `appModule`, which is the AngularJS part of our hybrid application and the Angular
* `AppModule`, which is the Angular part.
*
* ```typescript
* import {createAngularJSTestingModule} from '@angular/upgrade/testing';
* import {appModule, AppModule} from './app';
*
* beforeEach(module(createAngularJSTestingModule([AppModule]), appModule.name));
* ```
*
* Once this is done we can get hold of services via the AngularJS `$injector` as normal.
* Services that are (or have dependencies on) a downgraded Angular service, will be instantiated as needed
* by the Angular root `Injector`.
*
* In the following code snippet, `shoppingCart` is an AngularJS service that depends upon an Angular service.
*
* ```typescript
* it('should ...', inject(function(shoppingCart) {
* ...
* }));
* ```
*
* @param AngularModules a collection of Angular modules to include in the configuration
*/
export function createAngularJSTestingModule(AngularModules: any[]) {
return angular.module('$$angularJSTestingModule', [])
.factory(
INJECTOR_KEY,
[
$INJECTOR,
($injector: angular.IInjectorService) => {
TestBed.configureTestingModule({
imports: AngularModules,
providers: [{provide: $INJECTOR, useValue: $injector}]
});
return TestBed.get(Injector);
}
])
.name;
}
Loading

0 comments on commit 0ca84ba

Please sign in to comment.