2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
dispatchEvent,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
beforeEachProviders,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
TestComponentBuilder
|
|
|
|
} 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';
|
2016-03-08 16:36:48 -05:00
|
|
|
import {Directive, Component, ViewMetadata} from 'angular2/src/core/metadata';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component(
|
|
|
|
{selector: 'child-comp', 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
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'child-comp', template: `<span>Mock</span>`})
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
|
|
|
class MockChildComp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'parent-comp',
|
|
|
|
template: `Parent(<child-comp></child-comp>)`,
|
|
|
|
directives: [ChildComp]
|
|
|
|
})
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
|
|
|
class ParentComp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'my-if-comp',
|
|
|
|
template: `MyIf(<span *ngIf="showMore">More</span>)`,
|
|
|
|
directives: [NgIf]
|
|
|
|
})
|
2015-05-15 19:42:52 -04:00
|
|
|
@Injectable()
|
|
|
|
class MyIfComp {
|
|
|
|
showMore: boolean = false;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'child-child-comp', template: `<span>ChildChild</span>`})
|
2015-08-17 14:43:14 -04:00
|
|
|
@Injectable()
|
2015-09-08 13:14:57 -04:00
|
|
|
class ChildChildComp {
|
|
|
|
}
|
2015-08-17 14:43:14 -04:00
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'child-comp',
|
2015-09-08 13:14:57 -04:00
|
|
|
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'; }
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'child-child-comp', template: `<span>ChildChild Mock</span>`})
|
2015-08-17 14:43:14 -04:00
|
|
|
@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';
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'my-service-comp',
|
|
|
|
bindings: [FancyService],
|
|
|
|
template: `injected value: {{fancyService.value}}`
|
|
|
|
})
|
2015-09-08 13:14:57 -04:00
|
|
|
class TestBindingsComp {
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'my-service-comp',
|
|
|
|
viewProviders: [FancyService],
|
|
|
|
template: `injected value: {{fancyService.value}}`
|
|
|
|
})
|
2015-09-08 13:14:57 -04:00
|
|
|
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
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
tcb.overrideView(ChildComp,
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-04-12 12:40:37 -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
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
tcb.overrideProviders(TestBindingsComp,
|
|
|
|
[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
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
tcb.overrideViewProviders(TestViewBindingsComp,
|
|
|
|
[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
|
|
|
});
|
|
|
|
}
|