|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import {CommonModule} from '@angular/common';
|
10 |
| -import {ChangeDetectorRef, Component, Pipe, PipeTransform} from '@angular/core'; |
| 10 | +import {ChangeDetectorRef, Component, Directive, Inject, LOCALE_ID, Optional, Pipe, PipeTransform, SkipSelf, ViewChild} from '@angular/core'; |
11 | 11 | import {ViewRef} from '@angular/core/src/render3/view_ref';
|
12 | 12 | import {TestBed} from '@angular/core/testing';
|
13 | 13 |
|
| 14 | + |
14 | 15 | describe('di', () => {
|
15 | 16 | describe('ChangeDetectorRef', () => {
|
16 | 17 | it('should inject host component ChangeDetectorRef into directives on templates', () => {
|
@@ -39,4 +40,94 @@ describe('di', () => {
|
39 | 40 | expect((pipeInstance !.cdr as ViewRef<MyApp>).context).toBe(fixture.componentInstance);
|
40 | 41 | });
|
41 | 42 | });
|
| 43 | + |
| 44 | + it('should not cause cyclic dependency if same token is requested in deps with @SkipSelf', () => { |
| 45 | + @Component({ |
| 46 | + selector: 'my-comp', |
| 47 | + template: '...', |
| 48 | + providers: [{ |
| 49 | + provide: LOCALE_ID, |
| 50 | + useFactory: () => 'ja-JP', |
| 51 | + // Note: `LOCALE_ID` is also provided within APPLICATION_MODULE_PROVIDERS, so we use it here |
| 52 | + // as a dep and making sure it doesn't cause cyclic dependency (since @SkipSelf is present) |
| 53 | + deps: [[new Inject(LOCALE_ID), new Optional(), new SkipSelf()]] |
| 54 | + }] |
| 55 | + }) |
| 56 | + class MyComp { |
| 57 | + constructor(@Inject(LOCALE_ID) public localeId: string) {} |
| 58 | + } |
| 59 | + |
| 60 | + TestBed.configureTestingModule({declarations: [MyComp]}); |
| 61 | + const fixture = TestBed.createComponent(MyComp); |
| 62 | + fixture.detectChanges(); |
| 63 | + expect(fixture.componentInstance.localeId).toBe('ja-JP'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('module-level deps should not access Component/Directive providers', () => { |
| 67 | + @Component({ |
| 68 | + selector: 'my-comp', |
| 69 | + template: '...', |
| 70 | + providers: [{ |
| 71 | + provide: 'LOCALE_ID_DEP', // |
| 72 | + useValue: 'LOCALE_ID_DEP_VALUE' |
| 73 | + }] |
| 74 | + }) |
| 75 | + class MyComp { |
| 76 | + constructor(@Inject(LOCALE_ID) public localeId: string) {} |
| 77 | + } |
| 78 | + |
| 79 | + TestBed.configureTestingModule({ |
| 80 | + declarations: [MyComp], |
| 81 | + providers: [{ |
| 82 | + provide: LOCALE_ID, |
| 83 | + // we expect `localeDepValue` to be undefined, since it's not provided at a module level |
| 84 | + useFactory: (localeDepValue: any) => localeDepValue || 'en-GB', |
| 85 | + deps: [[new Inject('LOCALE_ID_DEP'), new Optional()]] |
| 86 | + }] |
| 87 | + }); |
| 88 | + const fixture = TestBed.createComponent(MyComp); |
| 89 | + fixture.detectChanges(); |
| 90 | + expect(fixture.componentInstance.localeId).toBe('en-GB'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should skip current level while retrieving tokens if @SkipSelf is defined', () => { |
| 94 | + @Component({ |
| 95 | + selector: 'my-comp', |
| 96 | + template: '...', |
| 97 | + providers: [{provide: LOCALE_ID, useFactory: () => 'en-GB'}] |
| 98 | + }) |
| 99 | + class MyComp { |
| 100 | + constructor(@SkipSelf() @Inject(LOCALE_ID) public localeId: string) {} |
| 101 | + } |
| 102 | + |
| 103 | + TestBed.configureTestingModule({declarations: [MyComp]}); |
| 104 | + const fixture = TestBed.createComponent(MyComp); |
| 105 | + fixture.detectChanges(); |
| 106 | + // takes `LOCALE_ID` from module injector, since we skip Component level with @SkipSelf |
| 107 | + expect(fixture.componentInstance.localeId).toBe('en-US'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should work when injecting dependency in Directives', () => { |
| 111 | + @Directive({ |
| 112 | + selector: '[dir]', // |
| 113 | + providers: [{provide: LOCALE_ID, useValue: 'ja-JP'}] |
| 114 | + }) |
| 115 | + class MyDir { |
| 116 | + constructor(@SkipSelf() @Inject(LOCALE_ID) public localeId: string) {} |
| 117 | + } |
| 118 | + @Component({ |
| 119 | + selector: 'my-comp', |
| 120 | + template: '<div dir></div>', |
| 121 | + providers: [{provide: LOCALE_ID, useValue: 'en-GB'}] |
| 122 | + }) |
| 123 | + class MyComp { |
| 124 | + @ViewChild(MyDir) myDir !: MyDir; |
| 125 | + constructor(@Inject(LOCALE_ID) public localeId: string) {} |
| 126 | + } |
| 127 | + |
| 128 | + TestBed.configureTestingModule({declarations: [MyDir, MyComp, MyComp]}); |
| 129 | + const fixture = TestBed.createComponent(MyComp); |
| 130 | + fixture.detectChanges(); |
| 131 | + expect(fixture.componentInstance.myDir.localeId).toBe('en-GB'); |
| 132 | + }); |
42 | 133 | });
|
0 commit comments