2015-05-26 12:45:15 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
2015-11-18 12:18:37 -05:00
|
|
|
afterEach,
|
2015-05-26 12:45:15 -04:00
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xdescribe,
|
2015-11-18 12:18:37 -05:00
|
|
|
Log,
|
2015-08-11 17:00:54 -04:00
|
|
|
xit
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {IS_DART, isPresent, stringify} from 'angular2/src/facade/lang';
|
2015-11-13 14:21:16 -05:00
|
|
|
import {bootstrap} from 'angular2/platform/browser';
|
|
|
|
import {ApplicationRef} from 'angular2/src/core/application_ref';
|
2015-11-10 18:42:22 -05:00
|
|
|
import {Component, Directive, View, OnDestroy, platform} from 'angular2/core';
|
2015-11-13 14:21:16 -05:00
|
|
|
import {BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS} from 'angular2/platform/browser';
|
2015-11-19 18:09:34 -05:00
|
|
|
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
2015-11-17 18:24:36 -05:00
|
|
|
import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {PromiseWrapper} from 'angular2/src/facade/async';
|
2015-11-18 12:18:37 -05:00
|
|
|
import {provide, Inject, Injector, PLATFORM_INITIALIZER, APP_INITIALIZER} from 'angular2/core';
|
|
|
|
import {disposePlatform} from 'angular2/src/core/application_ref';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {ExceptionHandler} from 'angular2/src/facade/exceptions';
|
2015-05-26 12:45:15 -04:00
|
|
|
import {Testability, TestabilityRegistry} from 'angular2/src/core/testability/testability';
|
2015-10-08 11:22:11 -04:00
|
|
|
import {ComponentRef_} from "angular2/src/core/linker/dynamic_component_loader";
|
2015-05-26 12:45:15 -04:00
|
|
|
|
|
|
|
@Component({selector: 'hello-app'})
|
|
|
|
@View({template: '{{greeting}} world!'})
|
|
|
|
class HelloRootCmp {
|
|
|
|
greeting: string;
|
|
|
|
constructor() { this.greeting = 'hello'; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'hello-app'})
|
2015-06-24 16:46:39 -04:00
|
|
|
@View({template: 'before: <ng-content></ng-content> after: done'})
|
2015-05-26 12:45:15 -04:00
|
|
|
class HelloRootCmpContent {
|
|
|
|
constructor() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'hello-app-2'})
|
|
|
|
@View({template: '{{greeting}} world, again!'})
|
|
|
|
class HelloRootCmp2 {
|
|
|
|
greeting: string;
|
|
|
|
constructor() { this.greeting = 'hello'; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'hello-app'})
|
|
|
|
@View({template: ''})
|
|
|
|
class HelloRootCmp3 {
|
|
|
|
appBinding;
|
|
|
|
|
|
|
|
constructor(@Inject("appBinding") appBinding) { this.appBinding = appBinding; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'hello-app'})
|
|
|
|
@View({template: ''})
|
|
|
|
class HelloRootCmp4 {
|
2015-10-28 13:34:13 -04:00
|
|
|
appRef;
|
2015-05-26 12:45:15 -04:00
|
|
|
|
2015-10-28 13:34:13 -04:00
|
|
|
constructor(@Inject(ApplicationRef) appRef) { this.appRef = appRef; }
|
2015-05-26 12:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'hello-app'})
|
|
|
|
class HelloRootMissingTemplate {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'hello-app'})
|
|
|
|
class HelloRootDirectiveIsNotCmp {
|
|
|
|
}
|
|
|
|
|
2015-11-10 18:42:22 -05:00
|
|
|
@Component({selector: 'hello-app'})
|
|
|
|
@View({template: ''})
|
|
|
|
class HelloOnDestroyTickCmp implements OnDestroy {
|
|
|
|
appRef: ApplicationRef;
|
|
|
|
constructor(@Inject(ApplicationRef) appRef) { this.appRef = appRef; }
|
|
|
|
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 20:04:36 -05:00
|
|
|
ngOnDestroy(): void { this.appRef.tick(); }
|
2015-11-10 18:42:22 -05:00
|
|
|
}
|
|
|
|
|
2015-07-27 18:47:42 -04:00
|
|
|
class _ArrayLogger {
|
|
|
|
res: any[] = [];
|
|
|
|
log(s: any): void { this.res.push(s); }
|
2015-08-24 14:35:27 -04:00
|
|
|
logError(s: any): void { this.res.push(s); }
|
2015-07-27 18:47:42 -04:00
|
|
|
logGroup(s: any): void { this.res.push(s); }
|
2015-07-23 21:00:19 -04:00
|
|
|
logGroupEnd(){};
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:47:42 -04:00
|
|
|
|
2015-05-26 12:45:15 -04:00
|
|
|
export function main() {
|
2015-10-11 01:11:13 -04:00
|
|
|
var fakeDoc, el, el2, testProviders, lightDom;
|
2015-05-26 12:45:15 -04:00
|
|
|
|
|
|
|
describe('bootstrap factory method', () => {
|
2015-06-24 16:46:39 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
fakeDoc = DOM.createHtmlDocument();
|
|
|
|
el = DOM.createElement('hello-app', fakeDoc);
|
|
|
|
el2 = DOM.createElement('hello-app-2', fakeDoc);
|
|
|
|
lightDom = DOM.createElement('light-dom-el', fakeDoc);
|
|
|
|
DOM.appendChild(fakeDoc.body, el);
|
|
|
|
DOM.appendChild(fakeDoc.body, el2);
|
|
|
|
DOM.appendChild(el, lightDom);
|
|
|
|
DOM.setText(lightDom, 'loading');
|
2015-10-12 14:30:34 -04:00
|
|
|
testProviders = [provide(DOCUMENT, {useValue: fakeDoc})];
|
2015-06-24 16:46:39 -04:00
|
|
|
});
|
|
|
|
|
2015-11-18 12:18:37 -05:00
|
|
|
afterEach(disposePlatform);
|
|
|
|
|
2015-05-26 12:45:15 -04:00
|
|
|
it('should throw if bootstrapped Directive is not a Component',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-08-05 13:37:11 -04:00
|
|
|
var logger = new _ArrayLogger();
|
|
|
|
var exceptionHandler = new ExceptionHandler(logger, false);
|
|
|
|
var refPromise =
|
|
|
|
bootstrap(HelloRootDirectiveIsNotCmp,
|
2015-10-12 14:30:34 -04:00
|
|
|
[testProviders, provide(ExceptionHandler, {useValue: exceptionHandler})]);
|
2015-06-26 18:59:18 -04:00
|
|
|
|
2015-07-23 21:00:19 -04:00
|
|
|
PromiseWrapper.then(refPromise, null, (exception) => {
|
|
|
|
expect(exception).toContainError(
|
2015-10-01 13:07:49 -04:00
|
|
|
`Could not compile '${stringify(HelloRootDirectiveIsNotCmp)}' because it is not a component.`);
|
|
|
|
expect(logger.res.join("")).toContain("Could not compile");
|
2015-05-26 12:45:15 -04:00
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should throw if no element is found', inject([AsyncTestCompleter], (async) => {
|
2015-07-27 18:47:42 -04:00
|
|
|
var logger = new _ArrayLogger();
|
2015-08-11 17:00:54 -04:00
|
|
|
var exceptionHandler = new ExceptionHandler(logger, IS_DART ? false : true);
|
2015-07-23 21:00:19 -04:00
|
|
|
|
2015-07-27 18:47:42 -04:00
|
|
|
var refPromise =
|
2015-10-12 14:30:34 -04:00
|
|
|
bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]);
|
2015-05-26 12:45:15 -04:00
|
|
|
PromiseWrapper.then(refPromise, null, (reason) => {
|
|
|
|
expect(reason.message).toContain('The selector "hello-app" did not match any elements');
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-07-27 18:47:42 -04:00
|
|
|
if (DOM.supportsDOMEvents()) {
|
|
|
|
it('should invoke the default exception handler when bootstrap fails',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var logger = new _ArrayLogger();
|
2015-08-11 17:00:54 -04:00
|
|
|
var exceptionHandler = new ExceptionHandler(logger, IS_DART ? false : true);
|
2015-07-27 18:47:42 -04:00
|
|
|
|
|
|
|
var refPromise =
|
2015-10-12 14:30:34 -04:00
|
|
|
bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]);
|
2015-07-27 18:47:42 -04:00
|
|
|
PromiseWrapper.then(refPromise, null, (reason) => {
|
|
|
|
expect(logger.res.join(""))
|
|
|
|
.toContain('The selector "hello-app" did not match any elements');
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:45:15 -04:00
|
|
|
it('should create an injector promise', () => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var refPromise = bootstrap(HelloRootCmp, testProviders);
|
2015-05-26 12:45:15 -04:00
|
|
|
expect(refPromise).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should display hello world', inject([AsyncTestCompleter], (async) => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var refPromise = bootstrap(HelloRootCmp, testProviders);
|
2015-05-26 12:45:15 -04:00
|
|
|
refPromise.then((ref) => {
|
|
|
|
expect(el).toHaveText('hello world!');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should support multiple calls to bootstrap', inject([AsyncTestCompleter], (async) => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var refPromise1 = bootstrap(HelloRootCmp, testProviders);
|
|
|
|
var refPromise2 = bootstrap(HelloRootCmp2, testProviders);
|
2015-05-26 12:45:15 -04:00
|
|
|
PromiseWrapper.all([refPromise1, refPromise2])
|
|
|
|
.then((refs) => {
|
|
|
|
expect(el).toHaveText('hello world!');
|
|
|
|
expect(el2).toHaveText('hello world, again!');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-11-13 14:21:16 -05:00
|
|
|
|
2015-11-10 18:42:22 -05:00
|
|
|
it('should not crash if change detection is invoked when the root component is disposed',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
bootstrap(HelloOnDestroyTickCmp, testProviders)
|
|
|
|
.then((ref) => {
|
|
|
|
expect(() => ref.dispose()).not.toThrow();
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-11-10 13:40:33 -05:00
|
|
|
it('should unregister change detectors when components are disposed',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-11-13 14:21:16 -05:00
|
|
|
var app = platform(BROWSER_PROVIDERS).application([BROWSER_APP_PROVIDERS, testProviders]);
|
2015-11-10 13:40:33 -05:00
|
|
|
app.bootstrap(HelloRootCmp)
|
|
|
|
.then((ref) => {
|
|
|
|
ref.dispose();
|
|
|
|
expect(() => app.tick()).not.toThrow();
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 12:45:15 -04:00
|
|
|
|
|
|
|
it("should make the provided bindings available to the application component",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var refPromise = bootstrap(
|
2015-10-12 14:30:34 -04:00
|
|
|
HelloRootCmp3, [testProviders, provide("appBinding", {useValue: "BoundValue"})]);
|
2015-05-26 12:45:15 -04:00
|
|
|
|
|
|
|
refPromise.then((ref) => {
|
2015-06-26 18:59:18 -04:00
|
|
|
expect(ref.hostComponent.appBinding).toEqual("BoundValue");
|
2015-05-26 12:45:15 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should avoid cyclic dependencies when root component requires Lifecycle through DI",
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var refPromise = bootstrap(HelloRootCmp4, testProviders);
|
2015-05-26 12:45:15 -04:00
|
|
|
|
|
|
|
refPromise.then((ref) => {
|
2015-10-28 13:34:13 -04:00
|
|
|
expect(ref.hostComponent.appRef).toBe((<ComponentRef_>ref).injector.get(ApplicationRef));
|
2015-05-26 12:45:15 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-11-18 12:18:37 -05:00
|
|
|
it("should run platform initializers", inject([Log], (log: Log) => {
|
|
|
|
let p = platform([
|
|
|
|
BROWSER_PROVIDERS,
|
|
|
|
provide(PLATFORM_INITIALIZER, {useValue: log.fn("platform_init1"), multi: true}),
|
|
|
|
provide(PLATFORM_INITIALIZER, {useValue: log.fn("platform_init2"), multi: true})
|
|
|
|
]);
|
|
|
|
expect(log.result()).toEqual("platform_init1; platform_init2");
|
|
|
|
log.clear();
|
|
|
|
p.application([
|
|
|
|
BROWSER_APP_PROVIDERS,
|
|
|
|
provide(APP_INITIALIZER, {useValue: log.fn("app_init1"), multi: true}),
|
|
|
|
provide(APP_INITIALIZER, {useValue: log.fn("app_init2"), multi: true})
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(log.result()).toEqual("app_init1; app_init2");
|
|
|
|
}));
|
|
|
|
|
2015-05-26 12:45:15 -04:00
|
|
|
it('should register each application with the testability registry',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var refPromise1 = bootstrap(HelloRootCmp, testProviders);
|
|
|
|
var refPromise2 = bootstrap(HelloRootCmp2, testProviders);
|
2015-05-26 12:45:15 -04:00
|
|
|
|
|
|
|
PromiseWrapper.all([refPromise1, refPromise2])
|
2015-06-12 18:49:34 -04:00
|
|
|
.then((refs: ApplicationRef[]) => {
|
|
|
|
var registry = refs[0].injector.get(TestabilityRegistry);
|
|
|
|
var testabilities =
|
2015-06-26 18:59:18 -04:00
|
|
|
[refs[0].injector.get(Testability), refs[1].injector.get(Testability)];
|
2015-06-12 18:49:34 -04:00
|
|
|
PromiseWrapper.all(testabilities)
|
|
|
|
.then((testabilities: Testability[]) => {
|
2015-05-26 12:45:15 -04:00
|
|
|
expect(registry.findTestabilityInTree(el)).toEqual(testabilities[0]);
|
|
|
|
expect(registry.findTestabilityInTree(el2)).toEqual(testabilities[1]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|