Repository navigation
feat(upgrade): provide test helpers for wiring up injectors #16848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
petebacondarwin
wants to merge
3
commits into
angular:master
from
petebacondarwin:ngupgrade-testing-api
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
113e582
fix(compiler): stringify `Object.create(null)` tokens
petebacondarwin c2752af
feat(upgrade): provide unit test helpers for wiring up injectors
petebacondarwin c8afbb6
fixup! feat(upgrade): provide unit test helpers for wiring up injectors
petebacondarwin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| // #docregion angular-setup | ||
| import {TestBed} from '@angular/core/testing'; | ||
| import {createAngularJSTestingModule, createAngularTestingModule} from '@angular/upgrade/static/testing'; | ||
|
|
||
| import {HeroesService, Ng2AppModule, ng1AppModule} from './module'; | ||
|
|
||
| const {module, inject} = (window as any).angular.mock; | ||
|
|
||
| // #enddocregion angular-setup | ||
| describe('HeroesService (from Angular)', () => { | ||
|
|
||
| // #docregion angular-setup | ||
| beforeEach(() => { | ||
| TestBed.configureTestingModule( | ||
| {imports: [createAngularTestingModule([ng1AppModule.name]), Ng2AppModule]}); | ||
| }); | ||
| // #enddocregion angular-setup | ||
|
|
||
| // #docregion angular-spec | ||
| it('should have access to the HeroesService', () => { | ||
| const heroesService = TestBed.get(HeroesService) as HeroesService; | ||
| expect(heroesService).toBeDefined(); | ||
| }); | ||
| // #enddocregion angular-spec | ||
| }); | ||
|
|
||
|
|
||
| describe('HeroesService (from AngularJS)', () => { | ||
| // #docregion angularjs-setup | ||
| beforeEach(module(createAngularJSTestingModule([Ng2AppModule]))); | ||
| beforeEach(module(ng1AppModule.name)); | ||
| // #enddocregion angularjs-setup | ||
|
|
||
| // #docregion angularjs-spec | ||
| it('should have access to the HeroesService', | ||
| inject((heroesService: HeroesService) => { expect(heroesService).toBeDefined(); })); | ||
| // #enddocregion angularjs-spec | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| load("//tools:defaults.bzl", "ng_module") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| exports_files(["package.json"]) | ||
|
|
||
| ng_module( | ||
| name = "testing", | ||
| srcs = glob( | ||
| [ | ||
| "*.ts", | ||
| "src/*.ts", | ||
| ], | ||
| ), | ||
| deps = [ | ||
| "//packages/core/testing", | ||
| "//packages/upgrade/src/common", | ||
| ], | ||
| ) |
This file contains hidden or 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 hidden or 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,11 @@ | ||
| { | ||
| "name": "@angular/upgrade/static/testing", | ||
| "main": "../../bundles/upgrade-static-testing.umd.js", | ||
| "module": "../../fesm5/static/testing.js", | ||
| "es2015": "../../fesm2015/static/testing.js", | ||
| "esm5": "../../esm5/static/testing/testing.js", | ||
| "esm2015": "../../esm2015/static/testing/testing.js", | ||
| "fesm5": "../../fesm5/static/testing.js", | ||
| "fesm2015": "../../fesm2015/static/testing.js", | ||
| "typings": "./testing.d.ts" | ||
| } |
This file contains hidden or 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'; |
99 changes: 99 additions & 0 deletions
99
packages/upgrade/static/testing/src/create_angular_testing_module.ts
This file contains hidden or 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,99 @@ | ||
| /** | ||
| * @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, Type} from '@angular/core'; | ||
|
|
||
| import * as angular from '../../../src/common/src/angular1'; | ||
| import {$INJECTOR, INJECTOR_KEY, UPGRADE_APP_TYPE_KEY} from '../../../src/common/src/constants'; | ||
| import {UpgradeAppType} from '../../../src/common/src/util'; | ||
|
|
||
| export let $injector: angular.IInjectorService|null = null; | ||
| let injector: Injector; | ||
|
|
||
| export function $injectorFactory() { | ||
| return $injector; | ||
| } | ||
|
|
||
| @NgModule({providers: [{provide: $INJECTOR, useFactory: $injectorFactory}]}) | ||
| export class AngularTestingModule { | ||
| constructor(i: Injector) { injector = i; } | ||
| } | ||
|
|
||
| /** | ||
| * A helper function to use when unit testing Angular services that depend upon upgraded AngularJS | ||
| * services. | ||
| * | ||
| * This function returns an `NgModule` decorated class that is configured to wire up the Angular | ||
| * and AngularJS injectors without the need to actually bootstrap a hybrid application. | ||
| * This makes it simpler and faster to unit test services. | ||
| * | ||
| * Use the returned class as an "import" when configuring the `TestBed`. | ||
| * | ||
| * In the following code snippet, we are configuring the TestBed with two imports. | ||
| * The `Ng2AppModule` is the Angular part of our hybrid application and the `ng1AppModule` is the | ||
| * AngularJS part. | ||
| * | ||
| * <code-example path="upgrade/static/ts/full/module.spec.ts" region="angular-setup"></code-example> | ||
| * | ||
| * 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, `HeroesService` is an Angular service that depends upon an | ||
| * AngularJS service, `titleCase`. | ||
| * | ||
| * <code-example path="upgrade/static/ts/full/module.spec.ts" region="angular-spec"></code-example> | ||
| * | ||
| * <div class="alert is-important"> | ||
| * | ||
| * This helper is for testing services not Components. | ||
| * For Component testing you must still bootstrap a hybrid app. See `UpgradeModule` or | ||
| * `downgradeModule` for more information. | ||
| * | ||
| * </div> | ||
| * | ||
| * <div class="alert is-important"> | ||
| * | ||
| * The resulting configuration does not wire up AngularJS digests to Zone hooks. It is the | ||
| * responsibility of the test writer to call `$rootScope.$apply`, as necessary, to trigger | ||
| * AngularJS handlers of async events from Angular. | ||
| * | ||
| * </div> | ||
| * | ||
| * <div class="alert is-important"> | ||
| * | ||
| * The helper sets up global variables to hold the shared Angular and AngularJS injectors. | ||
| * | ||
| * * Only call this helper once per spec. | ||
| * * Do not use `createAngularTestingModule` in the same spec as `createAngularJSTestingModule`. | ||
| * | ||
| * </div> | ||
| * | ||
| * Here is the example application and its unit tests that use `createAngularTestingModule` | ||
| * and `createAngularJSTestingModule`. | ||
| * | ||
| * <code-tabs> | ||
| * <code-pane header="module.spec.ts" path="upgrade/static/ts/full/module.spec.ts"></code-pane> | ||
| * <code-pane header="module.ts" path="upgrade/static/ts/full/module.ts"></code-pane> | ||
| * </code-tabs> | ||
| * | ||
| * | ||
| * @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. | ||
| * | ||
| * @publicApi | ||
| */ | ||
| export function createAngularTestingModule( | ||
| angularJSModules: string[], strictDi?: boolean): Type<any> { | ||
| angular.module_('$$angularJSTestingModule', angularJSModules) | ||
| .constant(UPGRADE_APP_TYPE_KEY, UpgradeAppType.Static) | ||
| .factory(INJECTOR_KEY, () => injector); | ||
| $injector = angular.injector(['ng', '$$angularJSTestingModule'], strictDi); | ||
| return AngularTestingModule; | ||
|
petebacondarwin marked this conversation as resolved.
Resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.