zoukankan      html  css  js  c++  java
  • [Angular Unit Testing] Testing component with content projection

    Component to be tested:

    <ng-template #defaultPlaceholder>
        Loading...
    </ng-template>
    
    <div class="loading-container" *ngIf="loading$ | async">
        <ng-container
            *ngTemplateOutlet="outlet ? outlet : defaultPlaceholder"
        ></ng-container>
    </div>
    import {
        Component,
        OnInit,
        Input,
        TemplateRef,
        AfterViewInit
    } from '@angular/core'
    import {LoadingService} from './loading.service'
    import {Observable} from 'rxjs'
    
    @Component({
        selector: 'loading',
        templateUrl: './loading.component.html',
        styleUrls: ['./loading.component.scss']
    })
    export class LoadingComponent implements OnInit, AfterViewInit {
        loading$: Observable<boolean>
    
        @Input() outlet: TemplateRef<any>
    
        ngAfterViewInit() {}
    
        constructor(private loadingService: LoadingService) {}
    
        ngOnInit(): void {
            this.loading$ = this.loadingService.loading$
        }
    
        get loadingContext() {
            return {
                type: 'placeholder'
            }
        }
    }

    Test code:

    import {async, ComponentFixture, TestBed} from '@angular/core/testing'
    
    import {LoadingComponent} from './loading.component'
    import {LoadingService} from './loading.service'
    
    import {ViewChild, Component, DebugElement} from '@angular/core'
    import {of} from 'rxjs'
    import {By} from '@angular/platform-browser'
    
    @Component({
        template: `
            <ng-template #loadingTmp><p>loading...</p></ng-template>
            <loading [outlet]="loadingTmp"></loading>
        `
    })
    class WrapperComponent {
        @ViewChild(LoadingComponent) loadingComponentRef: LoadingComponent
    }
    
    let loadingServiceSpy = {
        loading$: of(true),
        get config() {
            return {showContent: false}
        }
    }
    
    describe('LoadingComponent', () => {
        let component: LoadingComponent
        let wrapperComponent: WrapperComponent
        let fixture: ComponentFixture<WrapperComponent>
        let el: DebugElement
    
        beforeEach(async(() => {
            TestBed.configureTestingModule({
                declarations: [WrapperComponent, LoadingComponent],
                providers: [{provide: LoadingService, useValue: loadingServiceSpy}]
            }).compileComponents()
        }))
    
        beforeEach(() => {
            fixture = TestBed.createComponent(WrapperComponent)
            wrapperComponent = fixture.debugElement.componentInstance
            fixture.detectChanges()
            component = wrapperComponent.loadingComponentRef
            el = fixture.debugElement
            fixture.detectChanges()
        })
    
        it('should create', () => {
            expect(wrapperComponent).toBeDefined()
            expect(component).toBeDefined()
        })
    
        it('should use has p tag with text loading...', () => {
            const p = el.queryAll(By.css('p'))
            expect(p.length).toBe(1)
            expect(p[0].nativeElement.textContent).toBe('loading...')
        })
    })

    Idea is to create a WrapperComponent to include our component. Then we are able to project content which need to be passed into the testing component.

  • 相关阅读:
    Silverlight 游戏开发小“.NET研究”技巧:动感小菜单 狼人:
    版本效果MoonWarrior cocos2dx版本 1
    输入执行GetChar缓存机制剖析
    集成实现Dynamics CRM 2011编程系列(55):Dynamics CRM 集成开发简述
    函数进程APUE学习Posix线程(1)
    进程子进程[Linux]操作系统算法实验一:进程控制实验
    矩阵函数机器学习Matlab 编程常用命令速览(NgMLclass Octave/Matlab Tutorial)
    buildconfigurationHow to use Boost in Visual Studio 2010
    安防硬件WIZnet基于全硬件TCP/IP的安防产品应用及方案
    路由器登录SSH 登录思科路由器RSA modulus too small处理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12568788.html
Copyright © 2011-2022 走看看