2015-05-15 19:42:52 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
dispatchEvent,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
2015-11-11 08:28:23 -05:00
|
|
|
beforeEachProviders,
|
2015-05-15 19:42:52 -04:00
|
|
|
it,
|
2015-05-28 18:02:20 -04:00
|
|
|
xit,
|
|
|
|
TestComponentBuilder
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-11-18 18:55:43 -05:00
|
|
|
import {Injectable, provide} from 'angular2/core';
|
|
|
|
import {NgIf} from 'angular2/common';
|
2015-09-04 01:01:36 -04:00
|
|
|
import {Directive, Component, View, ViewMetadata} from 'angular2/src/core/metadata';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
@Component({selector: 'child-comp'})
|
2015-06-14 12:29:58 -04:00
|
|
|
@View({template: `<span>Original {{childBinding}}</span>`, directives: []})
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
|
|
|
class ChildComp {
|
|
|
|
childBinding: string;
|
2015-05-28 18:02:20 -04:00
|
|
|
constructor() { this.childBinding = 'Child'; }
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'child-comp'})
|
|
|
|
@View({template: `<span>Mock</span>`})
|
|
|
|
@Injectable()
|
|
|
|
class MockChildComp {
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:02:20 -04:00
|
|
|
@Component({selector: 'parent-comp'})
|
|
|
|
@View({template: `Parent(<child-comp></child-comp>)`, directives: [ChildComp]})
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
|
|
|
class ParentComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'my-if-comp'})
|
2015-11-23 19:02:19 -05:00
|
|
|
@View({template: `MyIf(<span *ngIf="showMore">More</span>)`, directives: [NgIf]})
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
|
|
|
class MyIfComp {
|
|
|
|
showMore: boolean = false;
|
|
|
|
}
|
|
|
|
|
2015-08-17 14:43:14 -04:00
|
|
|
@Component({selector: 'child-child-comp'})
|
|
|
|
@View({template: `<span>ChildChild</span>`})
|
|
|
|
@Injectable()
|
2015-09-08 13:14:57 -04:00
|
|
|
class ChildChildComp {
|
|
|
|
}
|
2015-08-17 14:43:14 -04:00
|
|
|
|
|
|
|
@Component({selector: 'child-comp'})
|
2015-09-08 13:14:57 -04:00
|
|
|
@View({
|
|
|
|
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
|
|
|
|
directives: [ChildChildComp]
|
|
|
|
})
|
2015-08-17 14:43:14 -04:00
|
|
|
@Injectable()
|
|
|
|
class ChildWithChildComp {
|
|
|
|
childBinding: string;
|
|
|
|
constructor() { this.childBinding = 'Child'; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'child-child-comp'})
|
|
|
|
@View({template: `<span>ChildChild Mock</span>`})
|
|
|
|
@Injectable()
|
2015-09-08 13:14:57 -04:00
|
|
|
class MockChildChildComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FancyService {
|
|
|
|
value: string = 'real value';
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockFancyService extends FancyService {
|
|
|
|
value: string = 'mocked out value';
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'my-service-comp', bindings: [FancyService]})
|
|
|
|
@View({template: `injected value: {{fancyService.value}}`})
|
|
|
|
class TestBindingsComp {
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
}
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
@Component({selector: 'my-service-comp', viewProviders: [FancyService]})
|
2015-09-08 13:14:57 -04:00
|
|
|
@View({template: `injected value: {{fancyService.value}}`})
|
|
|
|
class TestViewBindingsComp {
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
}
|
2015-08-17 14:43:14 -04:00
|
|
|
|
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
export function main() {
|
|
|
|
describe('test component builder', function() {
|
|
|
|
it('should instantiate a component with valid DOM',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
tcb.createAsync(ChildComp).then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement).toHaveText('Original Child');
|
2015-05-15 19:42:52 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-05-28 18:02:20 -04:00
|
|
|
it('should allow changing members of the component',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
tcb.createAsync(MyIfComp).then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement).toHaveText('MyIf()');
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-11-30 15:49:22 -05:00
|
|
|
componentFixture.componentInstance.showMore = true;
|
2015-10-31 12:50:19 -04:00
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement).toHaveText('MyIf(More)');
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should override a template',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-05-28 18:02:20 -04:00
|
|
|
tcb.overrideTemplate(MockChildComp, '<span>Mock</span>')
|
2015-05-15 19:42:52 -04:00
|
|
|
.createAsync(MockChildComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement).toHaveText('Mock');
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should override a view',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
tcb.overrideView(ChildComp,
|
2015-08-14 13:03:45 -04:00
|
|
|
new ViewMetadata({template: '<span>Modified {{childBinding}}</span>'}))
|
2015-05-15 19:42:52 -04:00
|
|
|
.createAsync(ChildComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement).toHaveText('Modified Child');
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should override component dependencies',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
tcb.overrideDirective(ParentComp, ChildComp, MockChildComp)
|
|
|
|
.createAsync(ParentComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement).toHaveText('Parent(Mock)');
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-08-17 14:43:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
it("should override child component's dependencies",
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-09-08 13:14:57 -04:00
|
|
|
|
|
|
|
tcb.overrideDirective(ParentComp, ChildComp, ChildWithChildComp)
|
|
|
|
.overrideDirective(ChildWithChildComp, ChildChildComp, MockChildChildComp)
|
|
|
|
.createAsync(ParentComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement)
|
2015-09-08 13:14:57 -04:00
|
|
|
.toHaveText('Parent(Original Child(ChildChild Mock))');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
it('should override a provider',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
tcb.overrideProviders(TestBindingsComp,
|
2015-10-12 14:30:34 -04:00
|
|
|
[provide(FancyService, {useClass: MockFancyService})])
|
2015-09-08 13:14:57 -04:00
|
|
|
.createAsync(TestBindingsComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement)
|
2015-09-08 13:14:57 -04:00
|
|
|
.toHaveText('injected value: mocked out value');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should override a viewBinding',
|
2015-09-11 16:45:31 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
tcb.overrideViewProviders(TestViewBindingsComp,
|
2015-10-12 14:30:34 -04:00
|
|
|
[provide(FancyService, {useClass: MockFancyService})])
|
2015-09-08 13:14:57 -04:00
|
|
|
.createAsync(TestViewBindingsComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
2015-11-30 15:49:22 -05:00
|
|
|
expect(componentFixture.nativeElement)
|
2015-09-08 13:14:57 -04:00
|
|
|
.toHaveText('injected value: mocked out value');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-15 19:42:52 -04:00
|
|
|
});
|
|
|
|
}
|