From c6e893953fecc8a043c3d972e5f9b1e13c77a517 Mon Sep 17 00:00:00 2001 From: Janne Vanhala Date: Sat, 17 Dec 2016 01:14:16 +0200 Subject: [PATCH] fix(upgrade): fix `registerForNg1Tests` (#13522) Fix an issue in `registerForNg1Tests`, where it passes a `null` as `ng1Injector` to `_bootstrapDone`. This causes a "TypeError: Cannot read property 'get' of null" to be thrown from `_bootstrapDone`. --- karma-js.conf.js | 20 +++++++--- .../@angular/upgrade/src/upgrade_adapter.ts | 2 +- modules/@angular/upgrade/test/upgrade_spec.ts | 40 ++++++++++++++++++- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/karma-js.conf.js b/karma-js.conf.js index f8de95f154..6f387139d2 100644 --- a/karma-js.conf.js +++ b/karma-js.conf.js @@ -24,17 +24,25 @@ module.exports = function(config) { 'node_modules/core-js/client/core.js', // include Angular v1 for upgrade module testing 'node_modules/angular/angular.js', + 'node_modules/angular-mocks/angular-mocks.js', - 'node_modules/zone.js/dist/zone.js', 'node_modules/zone.js/dist/long-stack-trace-zone.js', - 'node_modules/zone.js/dist/proxy.js', 'node_modules/zone.js/dist/sync-test.js', - 'node_modules/zone.js/dist/jasmine-patch.js', 'node_modules/zone.js/dist/async-test.js', + 'node_modules/zone.js/dist/zone.js', + 'node_modules/zone.js/dist/long-stack-trace-zone.js', + 'node_modules/zone.js/dist/proxy.js', + 'node_modules/zone.js/dist/sync-test.js', + 'node_modules/zone.js/dist/jasmine-patch.js', + 'node_modules/zone.js/dist/async-test.js', 'node_modules/zone.js/dist/fake-async-test.js', // Including systemjs because it defines `__eval`, which produces correct stack traces. - 'shims_for_IE.js', 'node_modules/systemjs/dist/system.src.js', + 'shims_for_IE.js', + 'node_modules/systemjs/dist/system.src.js', {pattern: 'node_modules/rxjs/**', included: false, watched: false, served: true}, - 'node_modules/reflect-metadata/Reflect.js', 'tools/build/file2modulename.js', 'test-main.js', - {pattern: 'dist/all/empty.*', included: false, watched: false}, { + 'node_modules/reflect-metadata/Reflect.js', + 'tools/build/file2modulename.js', + 'test-main.js', + {pattern: 'dist/all/empty.*', included: false, watched: false}, + { pattern: 'modules/@angular/platform-browser/test/static_assets/**', included: false, watched: false diff --git a/modules/@angular/upgrade/src/upgrade_adapter.ts b/modules/@angular/upgrade/src/upgrade_adapter.ts index cdf92b4e2f..c8b1722939 100644 --- a/modules/@angular/upgrade/src/upgrade_adapter.ts +++ b/modules/@angular/upgrade/src/upgrade_adapter.ts @@ -320,7 +320,7 @@ export class UpgradeAdapter { windowNgMock.module(this.ng1Module.name); const upgrade = new UpgradeAdapterRef(); this.ng2BootstrapDeferred.promise.then( - () => { (upgrade)._bootstrapDone(this.moduleRef, upgrade.ng1Injector); }, onError); + (ng1Injector) => { (upgrade)._bootstrapDone(this.moduleRef, ng1Injector); }, onError); return upgrade; } diff --git a/modules/@angular/upgrade/test/upgrade_spec.ts b/modules/@angular/upgrade/test/upgrade_spec.ts index 8e75817ce6..22738e5134 100644 --- a/modules/@angular/upgrade/test/upgrade_spec.ts +++ b/modules/@angular/upgrade/test/upgrade_spec.ts @@ -11,7 +11,7 @@ import {async, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing'; import {BrowserModule} from '@angular/platform-browser'; import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; import * as angular from '@angular/upgrade/src/angular_js'; -import {UpgradeAdapter, sortProjectableNodes} from '@angular/upgrade/src/upgrade_adapter'; +import {UpgradeAdapter, UpgradeAdapterRef, sortProjectableNodes} from '@angular/upgrade/src/upgrade_adapter'; export function main() { describe('adapter: ng1 to ng2', () => { @@ -1296,6 +1296,44 @@ export function main() { }); })); }); + + describe('registerForNg1Tests', () => { + let upgradeAdapterRef: UpgradeAdapterRef; + let $compile: angular.ICompileService; + let $rootScope: angular.IRootScopeService; + + beforeEach(() => { + const ng1Module = angular.module('ng1', []); + + const Ng2 = Component({ + selector: 'ng2', + template: 'Hello World', + }).Class({constructor: function() {}}); + + const Ng2Module = NgModule({declarations: [Ng2], imports: [BrowserModule]}).Class({ + constructor: function() {} + }); + + const upgradeAdapter = new UpgradeAdapter(Ng2Module); + ng1Module.directive('ng2', upgradeAdapter.downgradeNg2Component(Ng2)); + + upgradeAdapterRef = upgradeAdapter.registerForNg1Tests(['ng1']); + }); + + beforeEach( + inject((_$compile_: angular.ICompileService, _$rootScope_: angular.IRootScopeService) => { + $compile = _$compile_; + $rootScope = _$rootScope_; + })); + + it('should be able to test ng1 components that use ng2 components', async(() => { + upgradeAdapterRef.ready(() => { + const element = $compile('')($rootScope); + $rootScope.$digest(); + expect(element[0].textContent).toContain('Hello World'); + }); + })); + }); }); describe('sortProjectableNodes', () => {