|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {CommonModule} from '@angular/common'; |
| 10 | +import {NgComponentOutlet} from '@angular/common/src/directives/ng_component_outlet'; |
| 11 | +import {Component, ComponentRef, Inject, Injector, NO_ERRORS_SCHEMA, NgModule, OpaqueToken, Optional, Provider, QueryList, ReflectiveInjector, TemplateRef, Type, ViewChild, ViewChildren, ViewContainerRef} from '@angular/core'; |
| 12 | +import {TestBed, async} from '@angular/core/testing'; |
| 13 | +import {expect} from '@angular/platform-browser/testing/matchers'; |
| 14 | + |
| 15 | +export function main() { |
| 16 | + describe('insert/remove', () => { |
| 17 | + |
| 18 | + beforeEach(() => { TestBed.configureTestingModule({imports: [TestModule]}); }); |
| 19 | + |
| 20 | + it('should do nothing if component is null', async(() => { |
| 21 | + const template = `<template *ngComponentOutlet="currentComponent"></template>`; |
| 22 | + TestBed.overrideComponent(TestComponent, {set: {template: template}}); |
| 23 | + let fixture = TestBed.createComponent(TestComponent); |
| 24 | + |
| 25 | + fixture.componentInstance.currentComponent = null; |
| 26 | + fixture.detectChanges(); |
| 27 | + |
| 28 | + expect(fixture.nativeElement).toHaveText(''); |
| 29 | + })); |
| 30 | + |
| 31 | + it('should insert content specified by a component', async(() => { |
| 32 | + let fixture = TestBed.createComponent(TestComponent); |
| 33 | + |
| 34 | + fixture.detectChanges(); |
| 35 | + expect(fixture.nativeElement).toHaveText(''); |
| 36 | + |
| 37 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 38 | + |
| 39 | + fixture.detectChanges(); |
| 40 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 41 | + })); |
| 42 | + |
| 43 | + it('should emit a ComponentRef once a component was created', async(() => { |
| 44 | + let fixture = TestBed.createComponent(TestComponent); |
| 45 | + |
| 46 | + fixture.detectChanges(); |
| 47 | + expect(fixture.nativeElement).toHaveText(''); |
| 48 | + |
| 49 | + fixture.componentInstance.cmpRef = null; |
| 50 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 51 | + |
| 52 | + fixture.detectChanges(); |
| 53 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 54 | + expect(fixture.componentInstance.cmpRef).toBeAnInstanceOf(ComponentRef); |
| 55 | + expect(fixture.componentInstance.cmpRef.instance).toBeAnInstanceOf(InjectedComponent); |
| 56 | + })); |
| 57 | + |
| 58 | + |
| 59 | + it('should clear view if component becomes null', async(() => { |
| 60 | + let fixture = TestBed.createComponent(TestComponent); |
| 61 | + |
| 62 | + fixture.detectChanges(); |
| 63 | + expect(fixture.nativeElement).toHaveText(''); |
| 64 | + |
| 65 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 66 | + |
| 67 | + fixture.detectChanges(); |
| 68 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 69 | + |
| 70 | + fixture.componentInstance.currentComponent = null; |
| 71 | + |
| 72 | + fixture.detectChanges(); |
| 73 | + expect(fixture.nativeElement).toHaveText(''); |
| 74 | + })); |
| 75 | + |
| 76 | + |
| 77 | + it('should swap content if component changes', async(() => { |
| 78 | + let fixture = TestBed.createComponent(TestComponent); |
| 79 | + |
| 80 | + fixture.detectChanges(); |
| 81 | + expect(fixture.nativeElement).toHaveText(''); |
| 82 | + |
| 83 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 84 | + |
| 85 | + fixture.detectChanges(); |
| 86 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 87 | + |
| 88 | + fixture.componentInstance.currentComponent = InjectedComponentAgain; |
| 89 | + |
| 90 | + fixture.detectChanges(); |
| 91 | + expect(fixture.nativeElement).toHaveText('bar'); |
| 92 | + })); |
| 93 | + |
| 94 | + it('should use the injector, if one supplied', async(() => { |
| 95 | + let fixture = TestBed.createComponent(TestComponent); |
| 96 | + |
| 97 | + const uniqueValue = {}; |
| 98 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 99 | + fixture.componentInstance.injector = ReflectiveInjector.resolveAndCreate( |
| 100 | + [{provide: TEST_TOKEN, useValue: uniqueValue}], fixture.componentRef.injector); |
| 101 | + |
| 102 | + fixture.detectChanges(); |
| 103 | + let cmpRef: ComponentRef<InjectedComponent> = fixture.componentInstance.cmpRef; |
| 104 | + expect(cmpRef).toBeAnInstanceOf(ComponentRef); |
| 105 | + expect(cmpRef.instance).toBeAnInstanceOf(InjectedComponent); |
| 106 | + expect(cmpRef.instance.testToken).toBe(uniqueValue); |
| 107 | + |
| 108 | + })); |
| 109 | + |
| 110 | + it('should resolve a with injector', async(() => { |
| 111 | + let fixture = TestBed.createComponent(TestComponent); |
| 112 | + |
| 113 | + fixture.componentInstance.cmpRef = null; |
| 114 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 115 | + fixture.detectChanges(); |
| 116 | + let cmpRef: ComponentRef<InjectedComponent> = fixture.componentInstance.cmpRef; |
| 117 | + expect(cmpRef).toBeAnInstanceOf(ComponentRef); |
| 118 | + expect(cmpRef.instance).toBeAnInstanceOf(InjectedComponent); |
| 119 | + expect(cmpRef.instance.testToken).toBeNull(); |
| 120 | + })); |
| 121 | + |
| 122 | + it('should render projectable nodes, if supplied', async(() => { |
| 123 | + const template = `<template>projected foo</template>${TEST_CMP_TEMPLATE}`; |
| 124 | + TestBed.overrideComponent(TestComponent, {set: {template: template}}) |
| 125 | + .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]}); |
| 126 | + |
| 127 | + TestBed |
| 128 | + .overrideComponent(InjectedComponent, {set: {template: `<ng-content></ng-content>`}}) |
| 129 | + .configureTestingModule({schemas: [NO_ERRORS_SCHEMA]}); |
| 130 | + |
| 131 | + let fixture = TestBed.createComponent(TestComponent); |
| 132 | + |
| 133 | + fixture.detectChanges(); |
| 134 | + expect(fixture.nativeElement).toHaveText(''); |
| 135 | + |
| 136 | + fixture.componentInstance.currentComponent = InjectedComponent; |
| 137 | + fixture.componentInstance.projectables = |
| 138 | + [fixture.componentInstance.vcRef |
| 139 | + .createEmbeddedView(fixture.componentInstance.tplRefs.first) |
| 140 | + .rootNodes]; |
| 141 | + |
| 142 | + |
| 143 | + fixture.detectChanges(); |
| 144 | + expect(fixture.nativeElement).toHaveText('projected foo'); |
| 145 | + })); |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +const TEST_TOKEN = new OpaqueToken('TestToken'); |
| 150 | +@Component({selector: 'injected-component', template: 'foo'}) |
| 151 | +class InjectedComponent { |
| 152 | + constructor(@Optional() @Inject(TEST_TOKEN) public testToken: any) {} |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +@Component({selector: 'injected-component-again', template: 'bar'}) |
| 157 | +class InjectedComponentAgain { |
| 158 | +} |
| 159 | + |
| 160 | +const TEST_CMP_TEMPLATE = |
| 161 | + `<template *ngComponentOutlet="currentComponent; injector: injector; content: projectables"></template>`; |
| 162 | +@Component({selector: 'test-cmp', template: TEST_CMP_TEMPLATE}) |
| 163 | +class TestComponent { |
| 164 | + currentComponent: Type<any>; |
| 165 | + injector: Injector; |
| 166 | + projectables: any[][]; |
| 167 | + |
| 168 | + get cmpRef(): ComponentRef<any> { return this.ngComponentOutlet.componentRef; } |
| 169 | + set cmpRef(value: ComponentRef<any>) { this.ngComponentOutlet.componentRef = value; } |
| 170 | + |
| 171 | + @ViewChildren(TemplateRef) tplRefs: QueryList<TemplateRef<any>>; |
| 172 | + @ViewChild(NgComponentOutlet) ngComponentOutlet: NgComponentOutlet; |
| 173 | + |
| 174 | + constructor(public vcRef: ViewContainerRef) {} |
| 175 | +} |
| 176 | + |
| 177 | +@NgModule({ |
| 178 | + imports: [CommonModule], |
| 179 | + declarations: [TestComponent, InjectedComponent, InjectedComponentAgain], |
| 180 | + exports: [TestComponent, InjectedComponent, InjectedComponentAgain], |
| 181 | + entryComponents: [InjectedComponent, InjectedComponentAgain] |
| 182 | +}) |
| 183 | +export class TestModule { |
| 184 | +} |
0 commit comments