Vikram Subramanian 769835e53e feat(testing): Use NgZone in TestComponentBuilder.
Instantiating the test component within an NgZone will let us track async tasks in event handlers and change detection.

We can also do auto change detection when triggering events through dispatchEvent and not have to do fixture.detectChange() manually in the test.

New API:
ComponentFixture.autoDetectChanges() - This puts the fixture in auto detect mode that automatically calls detectChanges when the microtask queue is empty (Similar to how change detection is triggered in an actual application).

ComponentFixture.isStable() - This returns a boolean whether the fixture is currently stable or has some async tasks that need to be completed.

ComponentFixture.whenStable() - This returns a promise that is resolved when the fixture is stable after all async tasks are complete.

Closes 
2016-04-28 22:37:37 +00:00

77 lines
2.7 KiB
TypeScript

import {
APP_ID,
NgZone,
Provider,
PLATFORM_COMMON_PROVIDERS,
PLATFORM_INITIALIZER
} from 'angular2/core';
import {DirectiveResolver, ViewResolver} from 'angular2/compiler';
import {BROWSER_APP_COMMON_PROVIDERS} from 'angular2/src/platform/browser_common';
import {BrowserDomAdapter} from 'angular2/src/platform/browser/browser_adapter';
import {AnimationBuilder} from 'angular2/src/animate/animation_builder';
import {MockAnimationBuilder} from 'angular2/src/mock/animation_builder_mock';
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
import {LocationStrategy} from 'angular2/platform/common';
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';
import {XHRImpl} from "angular2/src/platform/browser/xhr_impl";
import {XHR} from 'angular2/compiler';
import {
TestComponentBuilder,
ComponentFixtureAutoDetect,
ComponentFixtureNoNgZone
} from 'angular2/src/testing/test_component_builder';
import {BrowserDetection} from 'angular2/src/testing/utils';
import {ELEMENT_PROBE_PROVIDERS} from 'angular2/platform/common_dom';
import {CONST_EXPR, IS_DART} from 'angular2/src/facade/lang';
import {Log} from 'angular2/src/testing/utils';
function initBrowserTests() {
BrowserDomAdapter.makeCurrent();
BrowserDetection.setup();
}
function createNgZone(): NgZone {
return IS_DART ? new MockNgZone() : new NgZone({enableLongStackTrace: true});
}
/**
* Default platform providers for testing without a compiler.
*/
export const TEST_BROWSER_STATIC_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
CONST_EXPR([
PLATFORM_COMMON_PROVIDERS,
new Provider(PLATFORM_INITIALIZER, {useValue: initBrowserTests, multi: true})
]);
export const ADDITIONAL_TEST_BROWSER_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
CONST_EXPR([
new Provider(APP_ID, {useValue: 'a'}),
ELEMENT_PROBE_PROVIDERS,
new Provider(DirectiveResolver, {useClass: MockDirectiveResolver}),
new Provider(ViewResolver, {useClass: MockViewResolver}),
Log,
TestComponentBuilder,
new Provider(NgZone, {useFactory: createNgZone}),
new Provider(LocationStrategy, {useClass: MockLocationStrategy}),
new Provider(AnimationBuilder, {useClass: MockAnimationBuilder}),
]);
/**
* Default application providers for testing without a compiler.
*/
export const TEST_BROWSER_STATIC_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
CONST_EXPR([
BROWSER_APP_COMMON_PROVIDERS,
new Provider(XHR, {useClass: XHRImpl}),
ADDITIONAL_TEST_BROWSER_PROVIDERS
]);