-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upgrade): provide test helpers for wiring up injectors
- Loading branch information
1 parent
d71ae27
commit 0ca84ba
Showing
28 changed files
with
428 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testing/index.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testing/public_api.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testing/src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../testing/test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
packages/upgrade/testing/src/create_angular_testing_module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
packages/upgrade/testing/src/create_angularjs_testing_module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.