• [Angular Unit Testing] Testing Component with ChangeDetectionStrategy.OnPush


    Component:

    <div class="loading-placeholder" [ngClass]="extraClass">
        &nbsp;
        <ng-container *ngIf="loadingService.config.showContent">
            <ng-content></ng-content>
        </ng-container>
    </div>
    import {Component, OnInit, Input, ChangeDetectionStrategy} from '@angular/core'
    import {LoadingService} from '../loading.service'
    
    @Component({
        selector: 'loading-placeholder',
        templateUrl: './loading-placeholder.component.html',
        styleUrls: ['./loading-placeholder.component.scss'],
        changeDetection: ChangeDetectionStrategy.OnPush
    })
    export class LoadingPlaceholderComponent implements OnInit {
        @Input() type: string
        @Input() size: string
    
        extraClass: string
    
        constructor(public loadingService: LoadingService) {}
    
        ngOnInit(): void {
            this.extraClass = this.getType()
        }
    
        getType() {
            const whiteList = ['image', 'headline', 'text']
            const sizes = ['small', 'medium', 'large', 'full']
            const alias = ['s', 'm', 'l', 'f']
            let res = ''
    
            if (whiteList.indexOf(this.type) === -1) {
                return res
            }
    
            if (
                sizes.indexOf(this.size) === -1 &&
                alias.indexOf(this.size) === -1
            ) {
                return `loading-placeholder__${this.type}`
            }
    
            return `loading-placeholder__${this.type}--${this.size}`
        }
    }

    Testing:

    import {
        async,
        ComponentFixture,
        TestBed,
        fakeAsync,
        flush
    } from '@angular/core/testing'
    
    import {LoadingPlaceholderComponent} from './loading-placeholder.component'
    import {LoadingService} from '../loading.service'
    import {of} from 'rxjs'
    import {DebugElement, Component, ChangeDetectionStrategy} from '@angular/core'
    import {By} from '@angular/platform-browser'
    
    fdescribe('LoadingPlaceholderComponent', () => {
        let component: LoadingPlaceholderComponent
        let fixture: ComponentFixture<LoadingPlaceholderComponent>
        let el: DebugElement
        let loadingService: LoadingService
        let loadingServiceSpy = {
            loading$: of(true),
            get config() {
                return {showContent: false}
            }
        }
    
        beforeEach(async(() => {
            TestBed.configureTestingModule({
                declarations: [LoadingPlaceholderComponent],
                providers: [{provide: LoadingService, useValue: loadingServiceSpy}]
            })
                .overrideComponent(LoadingPlaceholderComponent, {
                    set: new Component({
                        templateUrl: './loading-placeholder.component.html',
                        changeDetection: ChangeDetectionStrategy.Default
                    })
                })
                .compileComponents()
        }))
    
        beforeEach(() => {
            fixture = TestBed.createComponent(LoadingPlaceholderComponent)
            component = fixture.componentInstance
            el = fixture.debugElement
            loadingService = TestBed.inject(LoadingService)
            fixture.detectChanges()
        })
    
    
            it('should has correct className', () => {
                component.size = 'm'
                component.type = 'headline'
                component.ngOnInit()
                fixture.detectChanges()
                expect(component.extraClass).toBe(
                    'loading-placeholder__headline--m',
                    'extraClass has wrong value'
                )
    
                expect(
                    el.query(By.css('.loading-placeholder')).nativeElement.classList
                ).toContain(
                    'loading-placeholder__headline--m',
                    "ngClass doesn't work"
                )
            })
    
    })
  • 相关阅读:
    在一组降序排列的数组中插入一个数据,插入后,数组中数据依然按降序排列
    轮播图无限滚动
    微软雅黑的Unicode码和英文名
    javascript中的this
    javascript构造函数及原型对象
    object.prototype.call
    Array.prototype.forEach数组遍历
    键盘event.which属性
    Object.prototype.toString()
    parseInt()解析整数与parsetFloat()解析浮点数
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12562533.html
Copyright © 2020-2023  润新知