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 May 17, 2017
1 parent 221f85a commit c73b392
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/upgrade/src/common/angular1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,15 @@ let angular: {
void,
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 @@ -255,7 +257,8 @@ export const module = (prefix: string, dependencies?: string[]): IModule =>
angular.module(prefix, dependencies);

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

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

export const getTestability = (e: Element): ITestabilityService => angular.getTestability(e);
Expand Down
31 changes: 31 additions & 0 deletions packages/upgrade/src/testing/create_angular_test_module.ts
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 packages/upgrade/src/testing/create_angularjs_test_module.ts
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 packages/upgrade/test/testing/create_angular_test_module_spec.ts
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 packages/upgrade/test/testing/create_angularjs_test_module_spec.ts
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));
}));
});
}
71 changes: 71 additions & 0 deletions packages/upgrade/test/testing/mocks.ts
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;
}
]);

0 comments on commit c73b392

Please sign in to comment.