-
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
221f85a
commit c73b392
Showing
6 changed files
with
189 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
packages/upgrade/src/testing/create_angular_test_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,31 @@ | ||
/** | ||
* @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 '../common/angular1'; | ||
import {$INJECTOR, INJECTOR_KEY} from '../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; } | ||
} | ||
|
||
export function createAngularTestingModule(angularJSModules: string[], strictDI = true) { | ||
angular.module('$$angularJSTestingModule', angularJSModules) | ||
.factory(INJECTOR_KEY, () => injector); | ||
$injector = angular.injector(['$$angularJSTestingModule'], strictDI); | ||
return AngularTestingModule; | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/upgrade/src/testing/create_angularjs_test_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,30 @@ | ||
/** | ||
* @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 '../common/angular1'; | ||
import {$INJECTOR, INJECTOR_KEY} from '../common/constants'; | ||
|
||
export function createAngularJSTestingModule(AngularModules: any[]) { | ||
return angular.module('$$angularJSTestingModule', []) | ||
.factory( | ||
INJECTOR_KEY, | ||
[ | ||
$INJECTOR, | ||
($injector: any) => { | ||
TestBed.configureTestingModule({ | ||
imports: AngularModules, | ||
providers: [{provide: $INJECTOR, useValue: $injector}] | ||
}); | ||
return TestBed.get(Injector); | ||
} | ||
]) | ||
.name; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/upgrade/test/testing/create_angular_test_module_spec.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,27 @@ | ||
/** | ||
* @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 {TestBed} from '@angular/core/testing'; | ||
import * as angular from '@angular/upgrade/src/common/angular1'; | ||
import {createAngularTestingModule} from '@angular/upgrade/src/testing/create_angular_test_module'; | ||
|
||
import {AppModule, Inventory, serverRequestInstance} from './mocks'; | ||
|
||
|
||
export function main() { | ||
describe('Angular entry point', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({imports: [createAngularTestingModule(['app']), AppModule]}); | ||
}); | ||
|
||
it('should allow us to get an upgraded AngularJS service from an Angular service', () => { | ||
const inventory = TestBed.get(Inventory) as Inventory; | ||
expect(inventory.serverRequest).toBe(serverRequestInstance); | ||
}); | ||
}); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/upgrade/test/testing/create_angularjs_test_module_spec.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,26 @@ | ||
/** | ||
* @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 * as angular from '@angular/upgrade/src/common/angular1'; | ||
import {createAngularJSTestingModule} from '@angular/upgrade/src/testing/create_angularjs_test_module'; | ||
import {AppModule, Inventory} from './mocks'; | ||
|
||
const ngMock = (window as any).angular.mock; | ||
|
||
export function main() { | ||
describe('AngularJS entry point', () => { | ||
|
||
beforeEach(ngMock.module(createAngularJSTestingModule([AppModule]))); | ||
beforeEach(ngMock.module('app')); | ||
|
||
it('should allow us to get a downgraded Angular service from an AngularJS service', | ||
ngMock.inject(function(shoppingCart: any) { | ||
expect(shoppingCart.inventory).toEqual(jasmine.any(Inventory)); | ||
})); | ||
}); | ||
} |
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,71 @@ | ||
/** | ||
* @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 {Inject, Injectable, NgModule} from '@angular/core'; | ||
import * as angular from '@angular/upgrade/src/common/angular1'; | ||
import {downgradeInjectable} from '@angular/upgrade/static'; | ||
|
||
/** | ||
* Mock application code contains the following services and their dependencies: | ||
* | ||
* shoppingCart (AngularJS) | ||
* -> Inventory (Angular - downgraded) | ||
* -> serverRequest (AngularJS - upgraded) | ||
* -> Logger (Angular - downgraded) | ||
* | ||
* This allows us to test two scenarios: | ||
* * from AngularJS -> Angular -> AngularJS | ||
* * from Angular -> AngularJS -> Angular | ||
*/ | ||
|
||
@Injectable() | ||
export class Logger { | ||
warn() {} | ||
} | ||
|
||
@Injectable() | ||
export class Inventory { | ||
constructor(@Inject('serverRequest') public serverRequest: any) {} | ||
} | ||
|
||
export function serverRequestFactory(i: angular.IInjectorService) { | ||
return i.get('serverRequest'); | ||
} | ||
|
||
@NgModule({ | ||
providers: [ | ||
Logger, | ||
Inventory, | ||
{provide: 'serverRequest', useFactory: serverRequestFactory, deps: ['$injector']}, | ||
] | ||
}) | ||
export class AppModule { | ||
} | ||
|
||
export const serverRequestInstance: {logger?: Logger} = {}; | ||
export const shoppingCartInstance: {inventory?: Inventory} = {}; | ||
|
||
angular.module('app', []) | ||
.factory('logger', downgradeInjectable(Logger)) | ||
.factory('inventory', downgradeInjectable(Inventory)) | ||
.factory( | ||
'serverRequest', | ||
[ | ||
'logger', | ||
function(logger: Logger) { | ||
serverRequestInstance.logger = logger; | ||
return serverRequestInstance; | ||
} | ||
]) | ||
.factory('shoppingCart', [ | ||
'inventory', | ||
function(inventory: Inventory) { | ||
shoppingCartInstance.inventory = inventory; | ||
return shoppingCartInstance; | ||
} | ||
]); |