fix(upgrade): add testability hook to downgraded component
Add testability hook to downgraded component so that protractor can wait for asynchronous call to complete. Add unregisterApplication() and unregisterAllApplications() to testability registry for cleaning up testability and unit test.
This commit is contained in:
@ -5,10 +5,12 @@
|
||||
* 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 {ApplicationRef, Compiler, Component, ComponentFactory, ComponentRef, Injector, NgModule, Testability, TestabilityRegistry} from '@angular/core';
|
||||
import {TestBed, getTestBed, inject} from '@angular/core/testing';
|
||||
import * as angular from '@angular/upgrade/src/common/angular1';
|
||||
import {groupNodesBySelector} from '@angular/upgrade/src/common/downgrade_component_adapter';
|
||||
import {nodes} from './test_helpers';
|
||||
import {DowngradeComponentAdapter, groupNodesBySelector} from '@angular/upgrade/src/common/downgrade_component_adapter';
|
||||
|
||||
import {nodes} from './test_helpers';
|
||||
|
||||
export function main() {
|
||||
describe('DowngradeComponentAdapter', () => {
|
||||
@ -23,7 +25,6 @@ export function main() {
|
||||
|
||||
const selectors = ['input[type=date]', 'span', '.x'];
|
||||
const projectableNodes = groupNodesBySelector(selectors, contentNodes);
|
||||
|
||||
expect(projectableNodes[0]).toEqual(nodes('<input type="date" name="myDate">'));
|
||||
expect(projectableNodes[1]).toEqual(nodes('<span>span content</span>'));
|
||||
expect(projectableNodes[2])
|
||||
@ -75,5 +76,118 @@ export function main() {
|
||||
expect(noMatchSelectorNodes).toEqual([[]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('testability', () => {
|
||||
|
||||
let adapter: DowngradeComponentAdapter;
|
||||
let content: string;
|
||||
let compiler: Compiler;
|
||||
let element: angular.IAugmentedJQuery;
|
||||
|
||||
class mockScope implements angular.IScope {
|
||||
$new() { return this; };
|
||||
$watch(exp: angular.Ng1Expression, fn?: (a1?: any, a2?: any) => void) {
|
||||
return () => {};
|
||||
};
|
||||
$on(event: string, fn?: (event?: any, ...args: any[]) => void) {
|
||||
return () => {};
|
||||
};
|
||||
$destroy() {
|
||||
return () => {};
|
||||
};
|
||||
$apply(exp?: angular.Ng1Expression) {
|
||||
return () => {};
|
||||
};
|
||||
$digest() {
|
||||
return () => {};
|
||||
};
|
||||
$evalAsync(exp: angular.Ng1Expression, locals?: any) {
|
||||
return () => {};
|
||||
};
|
||||
$$childTail: angular.IScope;
|
||||
$$childHead: angular.IScope;
|
||||
$$nextSibling: angular.IScope;
|
||||
[key: string]: any;
|
||||
$id = 'mockScope';
|
||||
$parent: angular.IScope;
|
||||
$root: angular.IScope;
|
||||
}
|
||||
|
||||
function getAdaptor(): DowngradeComponentAdapter {
|
||||
let attrs = undefined as any;
|
||||
let scope: angular.IScope; // mock
|
||||
let ngModel = undefined as any;
|
||||
let parentInjector: Injector; // testbed
|
||||
let $injector = undefined as any;
|
||||
let $compile = undefined as any;
|
||||
let $parse = undefined as any;
|
||||
let componentFactory: ComponentFactory<any>; // testbed
|
||||
let wrapCallback = undefined as any;
|
||||
|
||||
content = `
|
||||
<h1> new component </h1>
|
||||
<div> a great component </div>
|
||||
<comp></comp>
|
||||
`;
|
||||
element = angular.element(content);
|
||||
scope = new mockScope();
|
||||
|
||||
@Component({
|
||||
selector: 'comp',
|
||||
template: '',
|
||||
})
|
||||
class NewComponent {
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
providers: [{provide: 'hello', useValue: 'component'}],
|
||||
declarations: [NewComponent],
|
||||
entryComponents: [NewComponent],
|
||||
})
|
||||
class NewModule {
|
||||
}
|
||||
|
||||
const modFactory = compiler.compileModuleSync(NewModule);
|
||||
const module = modFactory.create(TestBed);
|
||||
componentFactory = module.componentFactoryResolver.resolveComponentFactory(NewComponent) !;
|
||||
parentInjector = TestBed;
|
||||
|
||||
return new DowngradeComponentAdapter(
|
||||
element, attrs, scope, ngModel, parentInjector, $injector, $compile, $parse,
|
||||
componentFactory, wrapCallback);
|
||||
};
|
||||
|
||||
beforeEach((inject([Compiler], (inject_compiler: Compiler) => {
|
||||
compiler = inject_compiler;
|
||||
adapter = getAdaptor();
|
||||
})));
|
||||
|
||||
afterEach(() => {
|
||||
let registry = TestBed.get(TestabilityRegistry);
|
||||
registry.unregisterAllApplications();
|
||||
});
|
||||
|
||||
it('should add testabilities hook when creating components', () => {
|
||||
|
||||
let registry = TestBed.get(TestabilityRegistry);
|
||||
adapter.createComponent([]);
|
||||
expect(registry.getAllTestabilities().length).toEqual(1);
|
||||
|
||||
adapter = getAdaptor(); // get a new adaptor to creat a new component
|
||||
adapter.createComponent([]);
|
||||
expect(registry.getAllTestabilities().length).toEqual(2);
|
||||
});
|
||||
|
||||
it('should remove the testability hook when destroy a component', () => {
|
||||
const registry = TestBed.get(TestabilityRegistry);
|
||||
expect(registry.getAllTestabilities().length).toEqual(0);
|
||||
adapter.createComponent([]);
|
||||
expect(registry.getAllTestabilities().length).toEqual(1);
|
||||
adapter.registerCleanup(true);
|
||||
element.remove !();
|
||||
expect(registry.getAllTestabilities().length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user