', tcb);
+ it('should instantiate directives that depend on another directive', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, NeedsDirective]});
+ const el = createComponent('
');
- var d = el.children[0].injector.get(NeedsDirective);
+ var d = el.children[0].injector.get(NeedsDirective);
- expect(d).toBeAnInstanceOf(NeedsDirective);
- expect(d.dependency).toBeAnInstanceOf(SimpleDirective);
- }));
+ expect(d).toBeAnInstanceOf(NeedsDirective);
+ expect(d.dependency).toBeAnInstanceOf(SimpleDirective);
+ });
- it('should support useValue with different values', fakeAsync(() => {
- var el = createComp('', tcb.overrideProviders(TestComp, [
- {provide: 'numLiteral', useValue: 0},
- {provide: 'boolLiteral', useValue: true},
- {provide: 'strLiteral', useValue: 'a'},
- {provide: 'null', useValue: null},
- {provide: 'array', useValue: [1]},
- {provide: 'map', useValue: {'a': 1}},
- {provide: 'instance', useValue: new TestValue('a')},
- {provide: 'nested', useValue: [{'a': [1]}, new TestValue('b')]},
- ]));
- expect(el.injector.get('numLiteral')).toBe(0);
- expect(el.injector.get('boolLiteral')).toBe(true);
- expect(el.injector.get('strLiteral')).toBe('a');
- expect(el.injector.get('null')).toBe(null);
- expect(el.injector.get('array')).toEqual([1]);
- expect(el.injector.get('map')).toEqual({'a': 1});
- expect(el.injector.get('instance')).toEqual(new TestValue('a'));
- expect(el.injector.get('nested')).toEqual([{'a': [1]}, new TestValue('b')]);
- }));
+ it('should support useValue with different values', () => {
+ const el = createComponent('', [
+ {provide: 'numLiteral', useValue: 0},
+ {provide: 'boolLiteral', useValue: true},
+ {provide: 'strLiteral', useValue: 'a'},
+ {provide: 'null', useValue: null},
+ {provide: 'array', useValue: [1]},
+ {provide: 'map', useValue: {'a': 1}},
+ {provide: 'instance', useValue: new TestValue('a')},
+ {provide: 'nested', useValue: [{'a': [1]}, new TestValue('b')]},
+ ]);
+ expect(el.injector.get('numLiteral')).toBe(0);
+ expect(el.injector.get('boolLiteral')).toBe(true);
+ expect(el.injector.get('strLiteral')).toBe('a');
+ expect(el.injector.get('null')).toBe(null);
+ expect(el.injector.get('array')).toEqual([1]);
+ expect(el.injector.get('map')).toEqual({'a': 1});
+ expect(el.injector.get('instance')).toEqual(new TestValue('a'));
+ expect(el.injector.get('nested')).toEqual([{'a': [1]}, new TestValue('b')]);
+ });
- it('should instantiate providers that have dependencies with SkipSelf', fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideProviders(
- SimpleDirective, [{provide: 'injectable1', useValue: 'injectable1'}])
- .overrideProviders(SomeOtherDirective, [
- {provide: 'injectable1', useValue: 'new-injectable1'}, {
- provide: 'injectable2',
- useFactory: (val: any /** TODO #9100 */) => `${val}-injectable2`,
- deps: [[new InjectMetadata('injectable1'), new SkipSelfMetadata()]]
- }
- ]));
- expect(el.children[0].children[0].injector.get('injectable2'))
- .toEqual('injectable1-injectable2');
- }));
+ it('should instantiate providers that have dependencies with SkipSelf', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, SomeOtherDirective]});
+ TestBed.overrideDirective(
+ SimpleDirective,
+ {add: {providers: [{provide: 'injectable1', useValue: 'injectable1'}]}});
+ TestBed.overrideDirective(SomeOtherDirective, {
+ add: {
+ providers: [
+ {provide: 'injectable1', useValue: 'new-injectable1'}, {
+ provide: 'injectable2',
+ useFactory: (val: any) => `${val}-injectable2`,
+ deps: [[new InjectMetadata('injectable1'), new SkipSelfMetadata()]]
+ }
+ ]
+ }
+ });
+ const el = createComponent('
');
+ expect(el.children[0].children[0].injector.get('injectable2'))
+ .toEqual('injectable1-injectable2');
+ });
- it('should instantiate providers that have dependencies', fakeAsync(() => {
- var providers = [
- {provide: 'injectable1', useValue: 'injectable1'}, {
- provide: 'injectable2',
- useFactory: (val: any /** TODO #9100 */) => `${val}-injectable2`,
- deps: ['injectable1']
- }
- ];
- var el = createComp(
- '
', tcb.overrideProviders(SimpleDirective, providers));
- expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2');
- }));
+ it('should instantiate providers that have dependencies', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective]});
+ const providers = [
+ {provide: 'injectable1', useValue: 'injectable1'}, {
+ provide: 'injectable2',
+ useFactory: (val: any) => `${val}-injectable2`,
+ deps: ['injectable1']
+ }
+ ];
+ TestBed.overrideDirective(SimpleDirective, {add: {providers}});
+ var el = createComponent('
');
+ expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2');
+ });
- it('should instantiate viewProviders that have dependencies', fakeAsync(() => {
- var viewProviders = [
- {provide: 'injectable1', useValue: 'injectable1'}, {
- provide: 'injectable2',
- useFactory: (val: any /** TODO #9100 */) => `${val}-injectable2`,
- deps: ['injectable1']
- }
- ];
+ it('should instantiate viewProviders that have dependencies', () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent]});
+ const viewProviders = [
+ {provide: 'injectable1', useValue: 'injectable1'}, {
+ provide: 'injectable2',
+ useFactory: (val: any) => `${val}-injectable2`,
+ deps: ['injectable1']
+ }
+ ];
+ TestBed.overrideComponent(SimpleComponent, {set: {viewProviders}});
+ const el = createComponent('
');
+ expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2');
+ });
- var el = createComp(
- '
',
- tcb.overrideViewProviders(SimpleComponent, viewProviders));
- expect(el.children[0].injector.get('injectable2')).toEqual('injectable1-injectable2');
- }));
+ it('should instantiate components that depend on viewProviders providers', () => {
+ TestBed.configureTestingModule({declarations: [NeedsServiceComponent]});
+ TestBed.overrideComponent(
+ NeedsServiceComponent, {set: {providers: [{provide: 'service', useValue: 'service'}]}});
+ const el = createComponent('
');
+ expect(el.children[0].injector.get(NeedsServiceComponent).service).toEqual('service');
+ });
- it('should instantiate components that depend on viewProviders providers', fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideViewProviders(
- NeedsServiceComponent, [{provide: 'service', useValue: 'service'}]));
- expect(el.children[0].injector.get(NeedsServiceComponent).service).toEqual('service');
- }));
+ it('should instantiate multi providers', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective]});
+ const providers = [
+ {provide: 'injectable1', useValue: 'injectable11', multi: true},
+ {provide: 'injectable1', useValue: 'injectable12', multi: true}
+ ];
+ TestBed.overrideDirective(SimpleDirective, {set: {providers}});
+ const el = createComponent('
');
+ expect(el.children[0].injector.get('injectable1')).toEqual([
+ 'injectable11', 'injectable12'
+ ]);
+ });
- it('should instantiate multi providers', fakeAsync(() => {
- var providers = [
- {provide: 'injectable1', useValue: 'injectable11', multi: true},
- {provide: 'injectable1', useValue: 'injectable12', multi: true}
- ];
- var el = createComp(
- '
', tcb.overrideProviders(SimpleDirective, providers));
- expect(el.children[0].injector.get('injectable1')).toEqual([
- 'injectable11', 'injectable12'
- ]);
- }));
+ it('should instantiate providers lazily', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective]});
- it('should instantiate providers lazily', fakeAsync(() => {
- var created = false;
- var el = createComp(
- '
',
- tcb.overrideProviders(
- SimpleDirective, [{provide: 'service', useFactory: () => created = true}]));
+ let created = false;
+ TestBed.overrideDirective(
+ SimpleDirective,
+ {set: {providers: [{provide: 'service', useFactory: () => created = true}]}});
+ const el = createComponent('
');
- expect(created).toBe(false);
+ expect(created).toBe(false);
- el.children[0].injector.get('service');
+ el.children[0].injector.get('service');
- expect(created).toBe(true);
- }));
+ expect(created).toBe(true);
+ });
- it('should instantiate providers with a lifecycle hook eagerly', fakeAsync(() => {
- var created = false;
- class SomeInjectable {
- constructor() { created = true; }
- ngOnDestroy() {}
- }
+ it('should instantiate providers lazily', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective]});
+ let created = false;
+ TestBed.overrideDirective(
+ SimpleDirective,
+ {set: {providers: [{provide: 'service', useFactory: () => created = true}]}});
- var el = createComp(
- '
',
- tcb.overrideProviders(SimpleDirective, [SomeInjectable]));
+ const el = createComponent('
');
- expect(created).toBe(true);
- }));
+ expect(created).toBe(false);
- it('should instantiate view providers lazily', fakeAsync(() => {
- var created = false;
- var el = createComp(
- '
',
- tcb.overrideViewProviders(
- SimpleComponent, [{provide: 'service', useFactory: () => created = true}]));
+ el.children[0].injector.get('service');
- expect(created).toBe(false);
+ expect(created).toBe(true);
+ });
- el.children[0].injector.get('service');
+ it('should instantiate providers with a lifecycle hook eagerly', () => {
+ let created = false;
+ class SomeInjectable {
+ constructor() { created = true; }
+ ngOnDestroy() {}
+ }
+ TestBed.configureTestingModule({declarations: [SimpleDirective]});
+ TestBed.overrideDirective(SimpleDirective, {set: {providers: [SomeInjectable]}});
- expect(created).toBe(true);
- }));
+ const el = createComponent('
');
+
+ expect(created).toBe(true);
+ });
+
+ it('should instantiate view providers lazily', () => {
+ let created = false;
+ TestBed.configureTestingModule({declarations: [SimpleComponent]});
+ TestBed.overrideComponent(
+ SimpleComponent,
+ {set: {viewProviders: [{provide: 'service', useFactory: () => created = true}]}});
+ const el = createComponent('
');
+
+ expect(created).toBe(false);
+
+ el.children[0].injector.get('service');
+
+ expect(created).toBe(true);
+ });
it('should not instantiate other directives that depend on viewProviders providers (same element)',
- fakeAsync(() => {
- expect(
- () => createComp(
- '
',
- tcb.overrideViewProviders(
- SimpleComponent, [{provide: 'service', useValue: 'service'}])))
+ () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
+ TestBed.overrideComponent(
+ SimpleComponent,
+ {set: {viewProviders: [{provide: 'service', useValue: 'service'}]}});
+ expect(() => createComponent('
'))
.toThrowError(/No provider for service!/);
- }));
+ });
it('should not instantiate other directives that depend on viewProviders providers (child element)',
- fakeAsync(() => {
- expect(
- () => createComp(
- '
',
- tcb.overrideViewProviders(
- SimpleComponent, [{provide: 'service', useValue: 'service'}])))
+ () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
+ TestBed.overrideComponent(
+ SimpleComponent,
+ {set: {viewProviders: [{provide: 'service', useValue: 'service'}]}});
+ expect(() => createComponent('
'))
.toThrowError(/No provider for service!/);
- }));
+ });
- it('should instantiate directives that depend on providers of other directives',
- fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideProviders(
- SimpleDirective, [{provide: 'service', useValue: 'parentService'}]));
- expect(el.children[0].children[0].injector.get(NeedsService).service)
- .toEqual('parentService');
- }));
+ it('should instantiate directives that depend on providers of other directives', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, NeedsService]});
+ TestBed.overrideDirective(
+ SimpleDirective, {set: {providers: [{provide: 'service', useValue: 'parentService'}]}});
- it('should instantiate directives that depend on providers in a parent view',
- fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideProviders(
- SimpleDirective, [{provide: 'service', useValue: 'parentService'}]));
- expect(el.children[0].children[0].injector.get(NeedsService).service)
- .toEqual('parentService');
- }));
+ const el = createComponent('
');
+ expect(el.children[0].children[0].injector.get(NeedsService).service)
+ .toEqual('parentService');
+ });
- it('should instantiate directives that depend on providers of a component', fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideTemplate(SimpleComponent, '
')
- .overrideProviders(
- SimpleComponent, [{provide: 'service', useValue: 'hostService'}]));
- expect(el.children[0].children[0].injector.get(NeedsService).service)
- .toEqual('hostService');
- }));
+ it('should instantiate directives that depend on providers in a parent view', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, NeedsService]});
+ TestBed.overrideDirective(
+ SimpleDirective, {set: {providers: [{provide: 'service', useValue: 'parentService'}]}});
+ const el = createComponent(
+ '
');
+ expect(el.children[0].children[0].injector.get(NeedsService).service)
+ .toEqual('parentService');
+ });
- it('should instantiate directives that depend on view providers of a component',
- fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideTemplate(SimpleComponent, '
')
- .overrideViewProviders(
- SimpleComponent, [{provide: 'service', useValue: 'hostService'}]));
- expect(el.children[0].children[0].injector.get(NeedsService).service)
- .toEqual('hostService');
- }));
+ it('should instantiate directives that depend on providers of a component', () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
+ TestBed.overrideComponent(SimpleComponent, {set: {template: '
'}});
+ const el = createComponent('
');
+ expect(el.children[0].children[0].injector.get(NeedsService).service)
+ .toEqual('hostService');
+ });
+
+ it('should instantiate directives that depend on view providers of a component', () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
+ TestBed.overrideComponent(SimpleComponent, {set: {template: '
'}});
+ const el = createComponent('
');
+ expect(el.children[0].children[0].injector.get(NeedsService).service)
+ .toEqual('hostService');
+ });
it('should instantiate directives in a root embedded view that depend on view providers of a component',
- fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideTemplate(SimpleComponent, '
')
- .overrideViewProviders(
- SimpleComponent, [{provide: 'service', useValue: 'hostService'}]));
+ () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
+ TestBed.overrideComponent(
+ SimpleComponent,
+ {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {template: '
'}});
+ const el = createComponent('
');
expect(el.children[0].children[0].injector.get(NeedsService).service)
.toEqual('hostService');
- }));
+ });
- it('should instantiate directives that depend on instances in the app injector',
- fakeAsync(() => {
- var el = createComp('
', tcb);
- expect(el.children[0].injector.get(NeedsAppService).service).toEqual('appService');
- }));
+ it('should instantiate directives that depend on instances in the app injector', () => {
+ TestBed.configureTestingModule({declarations: [NeedsAppService]});
+ const el = createComponent('
');
+ expect(el.children[0].injector.get(NeedsAppService).service).toEqual('appService');
+ });
- it('should not instantiate a directive with cyclic dependencies', fakeAsync(() => {
- expect(() => createComp('
', tcb))
- .toThrowError(
- 'Template parse errors:\nCannot instantiate cyclic dependency! CycleDirective ("[ERROR ->]
"): TestComp@0:0');
- }));
+ it('should not instantiate a directive with cyclic dependencies', () => {
+ TestBed.configureTestingModule({declarations: [CycleDirective]});
+ expect(() => createComponent('
'))
+ .toThrowError(
+ 'Template parse errors:\nCannot instantiate cyclic dependency! CycleDirective ("[ERROR ->]
"): TestComp@0:0');
+ });
it('should not instantiate a directive in a view that has a host dependency on providers' +
' of the component',
- fakeAsync(() => {
- expect(
- () => createComp(
- '
',
- tcb.overrideProviders(
- SimpleComponent, [{provide: 'service', useValue: 'hostService'}])
- .overrideTemplate(SimpleComponent, '
')))
+ () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsServiceFromHost]});
+ TestBed.overrideComponent(
+ SimpleComponent,
+ {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {template: '
'}});
+
+ expect(() => createComponent('
'))
.toThrowError(
`Template parse errors:\nNo provider for service ("[ERROR ->]
"): SimpleComponent@0:0`);
- }));
+ });
it('should not instantiate a directive in a view that has a host dependency on providers' +
' of a decorator directive',
- fakeAsync(() => {
- expect(
- () => createComp(
- '
',
- tcb.overrideProviders(
- SomeOtherDirective, [{provide: 'service', useValue: 'hostService'}])
- .overrideTemplate(SimpleComponent, '
')))
+ () => {
+ TestBed.configureTestingModule(
+ {declarations: [SimpleComponent, SomeOtherDirective, NeedsServiceFromHost]});
+ TestBed.overrideComponent(
+ SimpleComponent,
+ {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {template: '
'}});
+
+ expect(() => createComponent('
'))
.toThrowError(
`Template parse errors:\nNo provider for service ("[ERROR ->]
"): SimpleComponent@0:0`);
- }));
+ });
it('should not instantiate a directive in a view that has a self dependency on a parent directive',
- fakeAsync(() => {
+ () => {
+ TestBed.configureTestingModule(
+ {declarations: [SimpleDirective, NeedsDirectiveFromSelf]});
expect(
() =>
- createComp('
', tcb))
+ createComponent('
'))
.toThrowError(
`Template parse errors:\nNo provider for SimpleDirective ("
"): TestComp@0:21`);
- }));
+ });
it('should instantiate directives that depend on other directives', fakeAsync(() => {
- var el = createComp('
', tcb);
- var d = el.children[0].children[0].injector.get(NeedsDirective);
+ TestBed.configureTestingModule({declarations: [SimpleDirective, NeedsDirective]});
+ const el = createComponent('
');
+ const d = el.children[0].children[0].injector.get(NeedsDirective);
expect(d).toBeAnInstanceOf(NeedsDirective);
expect(d.dependency).toBeAnInstanceOf(SimpleDirective);
}));
it('should throw when a dependency cannot be resolved', fakeAsync(() => {
- expect(() => createComp('
', tcb))
+ TestBed.configureTestingModule({declarations: [NeedsService]});
+
+ expect(() => createComponent('
'))
.toThrowError(/No provider for service!/);
}));
- it('should inject null when an optional dependency cannot be resolved', fakeAsync(() => {
- var el = createComp('
', tcb);
- var d = el.children[0].injector.get(OptionallyNeedsDirective);
- expect(d.dependency).toEqual(null);
- }));
+ it('should inject null when an optional dependency cannot be resolved', () => {
+ TestBed.configureTestingModule({declarations: [OptionallyNeedsDirective]});
+ const el = createComponent('
');
+ const d = el.children[0].injector.get(OptionallyNeedsDirective);
+ expect(d.dependency).toEqual(null);
+ });
- it('should instantiate directives that depends on the host component', fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideTemplate(SimpleComponent, '
'));
- var d = el.children[0].children[0].injector.get(NeedsComponentFromHost);
- expect(d.dependency).toBeAnInstanceOf(SimpleComponent);
- }));
+ it('should instantiate directives that depends on the host component', () => {
+ TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsComponentFromHost]});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {template: '
'}});
+ const el = createComponent('
');
+ const d = el.children[0].children[0].injector.get(NeedsComponentFromHost);
+ expect(d.dependency).toBeAnInstanceOf(SimpleComponent);
+ });
- it('should instantiate host views for components that have a @Host dependency ',
- fakeAsync(() => {
- var el = createComp('', tcb, NeedsHostAppService);
- expect(el.componentInstance.service).toEqual('appService');
- }));
+ it('should instantiate host views for components that have a @Host dependency ', () => {
+ TestBed.configureTestingModule({declarations: [NeedsHostAppService]});
+ const el = createComponent('', [], NeedsHostAppService);
+ expect(el.componentInstance.service).toEqual('appService');
+ });
- it('should not instantiate directives that depend on other directives on the host element',
- fakeAsync(() => {
- expect(
- () => createComp(
- '
',
- tcb.overrideTemplate(SimpleComponent, '
')))
- .toThrowError(
- `Template parse errors:\nNo provider for SimpleDirective ("[ERROR ->]
"): SimpleComponent@0:0`);
- }));
+ it('should not instantiate directives that depend on other directives on the host element', () => {
+ TestBed.configureTestingModule(
+ {declarations: [SimpleComponent, SimpleDirective, NeedsDirectiveFromHost]});
+ TestBed.overrideComponent(
+ SimpleComponent, {set: {template: '
'}});
+ expect(() => createComponent('
'))
+ .toThrowError(
+ `Template parse errors:\nNo provider for SimpleDirective ("[ERROR ->]
"): SimpleComponent@0:0`);
+ });
});
describe('static attributes', () => {
- it('should be injectable', fakeAsync(() => {
- var el = createComp('
', tcb);
- var needsAttribute = el.children[0].injector.get(NeedsAttribute);
+ it('should be injectable', () => {
+ TestBed.configureTestingModule({declarations: [NeedsAttribute]});
+ const el = createComponent('
');
+ const needsAttribute = el.children[0].injector.get(NeedsAttribute);
- expect(needsAttribute.typeAttribute).toEqual('text');
- expect(needsAttribute.titleAttribute).toEqual('');
- expect(needsAttribute.fooAttribute).toEqual(null);
- }));
+ expect(needsAttribute.typeAttribute).toEqual('text');
+ expect(needsAttribute.titleAttribute).toEqual('');
+ expect(needsAttribute.fooAttribute).toEqual(null);
+ });
- it('should be injectable without type annotation', fakeAsync(() => {
- var el = createComp('
', tcb);
- var needsAttribute = el.children[0].injector.get(NeedsAttributeNoType);
+ it('should be injectable without type annotation', () => {
+ TestBed.configureTestingModule({declarations: [NeedsAttributeNoType]});
+ const el = createComponent('
');
+ const needsAttribute = el.children[0].injector.get(NeedsAttributeNoType);
- expect(needsAttribute.fooAttribute).toEqual('bar');
- }));
+ expect(needsAttribute.fooAttribute).toEqual('bar');
+ });
});
describe('refs', () => {
- it('should inject ElementRef', fakeAsync(() => {
- var el = createComp('
', tcb);
- expect(el.children[0].injector.get(NeedsElementRef).elementRef.nativeElement)
- .toBe(el.children[0].nativeElement);
- }));
+ it('should inject ElementRef', () => {
+ TestBed.configureTestingModule({declarations: [NeedsElementRef]});
+ const el = createComponent('
');
+ expect(el.children[0].injector.get(NeedsElementRef).elementRef.nativeElement)
+ .toBe(el.children[0].nativeElement);
+ });
it('should inject ChangeDetectorRef of the component\'s view into the component via a proxy',
- fakeAsync(() => {
- var cf = createCompFixture('
', tcb);
+ () => {
+ TestBed.configureTestingModule({declarations: [PushComponentNeedsChangeDetectorRef]});
+ const cf = createComponentFixture('
');
cf.detectChanges();
- var compEl = cf.debugElement.children[0];
- var comp = compEl.injector.get(PushComponentNeedsChangeDetectorRef);
+ const compEl = cf.debugElement.children[0];
+ const comp = compEl.injector.get(PushComponentNeedsChangeDetectorRef);
comp.counter = 1;
cf.detectChanges();
expect(compEl.nativeElement).toHaveText('0');
comp.changeDetectorRef.markForCheck();
cf.detectChanges();
expect(compEl.nativeElement).toHaveText('1');
- }));
+ });
- it('should inject ChangeDetectorRef of the containing component into directives',
- fakeAsync(() => {
- var cf = createCompFixture(
- '
',
- tcb.overrideTemplate(
- PushComponentNeedsChangeDetectorRef,
- '{{counter}}
'));
- cf.detectChanges();
- var compEl = cf.debugElement.children[0];
- var comp: PushComponentNeedsChangeDetectorRef =
- compEl.injector.get(PushComponentNeedsChangeDetectorRef);
- comp.counter = 1;
- cf.detectChanges();
- expect(compEl.nativeElement).toHaveText('0');
- expect(
- compEl.children[0].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef)
- .toBe(comp.changeDetectorRef);
- expect(
- compEl.children[1].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef)
- .toBe(comp.changeDetectorRef);
- comp.changeDetectorRef.markForCheck();
- cf.detectChanges();
- expect(compEl.nativeElement).toHaveText('1');
- }));
+ it('should inject ChangeDetectorRef of the containing component into directives', () => {
+ TestBed.configureTestingModule(
+ {declarations: [PushComponentNeedsChangeDetectorRef, DirectiveNeedsChangeDetectorRef]});
+ TestBed.overrideComponent(PushComponentNeedsChangeDetectorRef, {
+ set: {
+ template:
+ '{{counter}}
'
+ }
+ });
+ const cf = createComponentFixture('
');
+ cf.detectChanges();
+ const compEl = cf.debugElement.children[0];
+ const comp: PushComponentNeedsChangeDetectorRef =
+ compEl.injector.get(PushComponentNeedsChangeDetectorRef);
+ comp.counter = 1;
+ cf.detectChanges();
+ expect(compEl.nativeElement).toHaveText('0');
+ expect(compEl.children[0].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef)
+ .toBe(comp.changeDetectorRef);
+ expect(compEl.children[1].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef)
+ .toBe(comp.changeDetectorRef);
+ comp.changeDetectorRef.markForCheck();
+ cf.detectChanges();
+ expect(compEl.nativeElement).toHaveText('1');
+ });
- it('should inject ViewContainerRef', fakeAsync(() => {
- var el = createComp('
', tcb);
- expect(el.children[0]
- .injector.get(NeedsViewContainerRef)
- .viewContainer.element.nativeElement)
- .toBe(el.children[0].nativeElement);
- }));
+ it('should inject ViewContainerRef', () => {
+ TestBed.configureTestingModule({declarations: [NeedsViewContainerRef]});
+ const el = createComponent('
');
+ expect(
+ el.children[0].injector.get(NeedsViewContainerRef).viewContainer.element.nativeElement)
+ .toBe(el.children[0].nativeElement);
+ });
- it('should inject TemplateRef', fakeAsync(() => {
- var el = createComp('
', tcb);
- expect(el.childNodes[0].injector.get(NeedsTemplateRef).templateRef.elementRef)
- .toEqual(el.childNodes[0].injector.get(NeedsViewContainerRef).viewContainer.element);
- }));
+ it('should inject TemplateRef', () => {
+ TestBed.configureTestingModule({declarations: [NeedsViewContainerRef, NeedsTemplateRef]});
+ const el = createComponent('
');
+ expect(el.childNodes[0].injector.get(NeedsTemplateRef).templateRef.elementRef)
+ .toEqual(el.childNodes[0].injector.get(NeedsViewContainerRef).viewContainer.element);
+ });
- it('should throw if there is no TemplateRef', fakeAsync(() => {
- expect(() => createComp('
', tcb))
- .toThrowError(/No provider for TemplateRef!/);
- }));
+ it('should throw if there is no TemplateRef', () => {
+ TestBed.configureTestingModule({declarations: [NeedsTemplateRef]});
+ expect(() => createComponent('
'))
+ .toThrowError(/No provider for TemplateRef!/);
+ });
- it('should inject null if there is no TemplateRef when the dependency is optional',
- fakeAsync(() => {
- var el = createComp('
', tcb);
- var instance = el.children[0].injector.get(OptionallyNeedsTemplateRef);
- expect(instance.templateRef).toBeNull();
- }));
+ it('should inject null if there is no TemplateRef when the dependency is optional', () => {
+ TestBed.configureTestingModule({declarations: [OptionallyNeedsTemplateRef]});
+ const el = createComponent('
');
+ const instance = el.children[0].injector.get(OptionallyNeedsTemplateRef);
+ expect(instance.templateRef).toBeNull();
+ });
});
describe('pipes', () => {
- it('should instantiate pipes that have dependencies', fakeAsync(() => {
- var el = createComp(
- '
',
- tcb.overrideProviders(TestComp, [{provide: 'service', useValue: 'pipeService'}]));
- expect(el.children[0].injector.get(SimpleDirective).value.service)
- .toEqual('pipeService');
- }));
+ it('should instantiate pipes that have dependencies', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, PipeNeedsService]});
- it('should overwrite pipes with later entry in the pipes array', fakeAsync(() => {
- var el = createComp('
', tcb);
- expect(el.children[0].injector.get(SimpleDirective).value)
- .toBeAnInstanceOf(DuplicatePipe2);
- }));
+ const el = createComponent(
+ '
',
+ [{provide: 'service', useValue: 'pipeService'}]);
+ expect(el.children[0].injector.get(SimpleDirective).value.service).toEqual('pipeService');
+ });
- it('should inject ChangeDetectorRef into pipes', fakeAsync(() => {
- var el = createComp(
- '
',
- tcb);
- var cdRef =
- el.children[0].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef;
- expect(el.children[0].injector.get(SimpleDirective).value.changeDetectorRef).toBe(cdRef);
- }));
+ it('should overwrite pipes with later entry in the pipes array', () => {
+ TestBed.configureTestingModule(
+ {declarations: [SimpleDirective, DuplicatePipe1, DuplicatePipe2]});
+ const el = createComponent('
');
+ expect(el.children[0].injector.get(SimpleDirective).value).toBeAnInstanceOf(DuplicatePipe2);
+ });
- it('should cache pure pipes', fakeAsync(() => {
- var el = createComp(
- '
' +
- '
',
- tcb);
- var purePipe1 = el.children[0].injector.get(SimpleDirective).value;
- var purePipe2 = el.children[1].injector.get(SimpleDirective).value;
- var purePipe3 = el.children[2].injector.get(SimpleDirective).value;
- var purePipe4 = el.children[3].injector.get(SimpleDirective).value;
- expect(purePipe1).toBeAnInstanceOf(PurePipe);
- expect(purePipe2).toBe(purePipe1);
- expect(purePipe3).toBe(purePipe1);
- expect(purePipe4).toBe(purePipe1);
- }));
+ it('should inject ChangeDetectorRef into pipes', () => {
+ TestBed.configureTestingModule({
+ declarations:
+ [SimpleDirective, PipeNeedsChangeDetectorRef, DirectiveNeedsChangeDetectorRef]
+ });
+ const el = createComponent(
+ '
');
+ const cdRef =
+ el.children[0].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef;
+ expect(el.children[0].injector.get(SimpleDirective).value.changeDetectorRef).toBe(cdRef);
+ });
- it('should not cache impure pipes', fakeAsync(() => {
- var el = createComp(
- '
' +
- '
',
- tcb);
- var purePipe1 = el.children[0].injector.get(SimpleDirective).value;
- var purePipe2 = el.children[1].injector.get(SimpleDirective).value;
- var purePipe3 = el.children[2].injector.get(SimpleDirective).value;
- var purePipe4 = el.children[3].injector.get(SimpleDirective).value;
- expect(purePipe1).toBeAnInstanceOf(ImpurePipe);
- expect(purePipe2).toBeAnInstanceOf(ImpurePipe);
- expect(purePipe2).not.toBe(purePipe1);
- expect(purePipe3).toBeAnInstanceOf(ImpurePipe);
- expect(purePipe3).not.toBe(purePipe1);
- expect(purePipe4).toBeAnInstanceOf(ImpurePipe);
- expect(purePipe4).not.toBe(purePipe1);
- }));
+ it('should cache pure pipes', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, PurePipe]});
+ const el = createComponent(
+ '
' +
+ '
');
+ const purePipe1 = el.children[0].injector.get(SimpleDirective).value;
+ const purePipe2 = el.children[1].injector.get(SimpleDirective).value;
+ const purePipe3 = el.children[2].injector.get(SimpleDirective).value;
+ const purePipe4 = el.children[3].injector.get(SimpleDirective).value;
+ expect(purePipe1).toBeAnInstanceOf(PurePipe);
+ expect(purePipe2).toBe(purePipe1);
+ expect(purePipe3).toBe(purePipe1);
+ expect(purePipe4).toBe(purePipe1);
+ });
+
+ it('should not cache impure pipes', () => {
+ TestBed.configureTestingModule({declarations: [SimpleDirective, ImpurePipe]});
+ const el = createComponent(
+ '
' +
+ '
');
+ const impurePipe1 = el.children[0].injector.get(SimpleDirective).value;
+ const impurePipe2 = el.children[1].injector.get(SimpleDirective).value;
+ const impurePipe3 = el.children[2].injector.get(SimpleDirective).value;
+ const impurePipe4 = el.children[3].injector.get(SimpleDirective).value;
+ expect(impurePipe1).toBeAnInstanceOf(ImpurePipe);
+ expect(impurePipe2).toBeAnInstanceOf(ImpurePipe);
+ expect(impurePipe2).not.toBe(impurePipe1);
+ expect(impurePipe3).toBeAnInstanceOf(ImpurePipe);
+ expect(impurePipe3).not.toBe(impurePipe1);
+ expect(impurePipe4).toBeAnInstanceOf(ImpurePipe);
+ expect(impurePipe4).not.toBe(impurePipe1);
+ });
});
});
}