import { Component , DebugElement , ElementRef } from '@angular/core' ; import { FormsModule } from '@angular/forms' ; import { ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ; import { By } from '@angular/platform-browser' ; import { ThyStrengthModule } from '../strength.module' ; import { ThyStrengthComponent } from '../strength.component' ; import { ThyButtonModule } from 'ngx-tethys/button' ; @ Component ( { selector : 'thy-strength-basic-test' , template : `
        <thy-strength [(ngModel)]="value"></thy-strength>
    ` } ) class StrengthBasicTestComponent { public value = 1 ; } describe ( 'Strength basic component' , ( ) => { let fixture ; let testStrengthBasicComponent ; let strengthBasicDebugComponent ; let strengthBasicElement ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ ThyStrengthModule , FormsModule , ThyButtonModule ] , declarations : [ StrengthBasicTestComponent ] , providers : [ ] } ) ; TestBed . compileComponents ( ) ; } ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( StrengthBasicTestComponent ) ; testStrengthBasicComponent = fixture . debugElement . componentInstance ; strengthBasicDebugComponent = fixture . debugElement . query ( By . directive ( ThyStrengthComponent ) ) ; strengthBasicElement = strengthBasicDebugComponent . nativeElement ; } ) ; it ( 'should create' , ( ) => { expect ( testStrengthBasicComponent ) . toBeTruthy ( ) ; expect ( strengthBasicElement ) . toBeTruthy ( ) ; } ) ; it ( 'should have correct class when ngModel value change' , fakeAsync ( ( ) => { testStrengthBasicComponent . value = 4 ; fixture . detectChanges ( ) ; tick ( 500 ) ; fixture . detectChanges ( ) ; expect ( strengthBasicElement . children [ 0 ] . children [ 0 ] . classList . contains ( 'strength-text-highest' ) ) . toBe ( true ) ; expect ( strengthBasicElement . children [ 1 ] . classList . contains ( 'strength-level-highest' ) ) . toBe ( true ) ; } ) ) ; } ) ; @ Component ( { selector : 'thy-strength-test' , template : `
        <thy-strength
            [(ngModel)]="strength"
            [titleKey]="text[currentTextType].title"
            [highestKey]="text[currentTextType].highestKey"
            [highKey]="text[currentTextType].highKey"
            [averageKey]="text[currentTextType].averageKey"
            [lowKey]="text[currentTextType].lowKey"
        ></thy-strength>
    ` } ) class StrengthTestComponent { public strength = 2 ; public currentTextType = 'custom' ; public text = { default : { title : '密码强度' , highestKey : '最高' , highKey : '高' , averageKey : '中' , lowKey : '低' } , custom : { title : '评分' , highestKey : '🌟🌟🌟🌟' , highKey : '🌟🌟🌟' , averageKey : '🌟🌟' , lowKey : '🌟' } } ; } describe ( 'Strength component' , ( ) => { let fixture ; let testStrengthComponent ; let strengthDebugComponent ; let strengthElement ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ ThyStrengthModule , FormsModule , ThyButtonModule ] , declarations : [ StrengthTestComponent ] , providers : [ ] } ) ; TestBed . compileComponents ( ) ; } ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( StrengthTestComponent ) ; testStrengthComponent = fixture . debugElement . componentInstance ; strengthDebugComponent = fixture . debugElement . query ( By . directive ( ThyStrengthComponent ) ) ; strengthElement = strengthDebugComponent . nativeElement ; } ) ; it ( 'should create' , ( ) => { expect ( testStrengthComponent ) . toBeTruthy ( ) ; expect ( strengthElement ) . toBeTruthy ( ) ; } ) ; it ( 'should have correct class when change strength' , fakeAsync ( ( ) => { testStrengthComponent . strength = 3 ; fixture . detectChanges ( ) ; tick ( 500 ) ; fixture . detectChanges ( ) ; expect ( ( strengthElement . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '评分 🌟🌟🌟' ) ; expect ( strengthElement . children [ 0 ] . children [ 0 ] . classList . contains ( 'strength-text-high' ) ) . toBe ( true ) ; expect ( strengthElement . children [ 1 ] . classList . contains ( 'strength-level-high' ) ) . toBe ( true ) ; } ) ) ; it ( 'should have correct class when change type' , fakeAsync ( ( ) => { testStrengthComponent . currentTextType = 'default' ; fixture . detectChanges ( ) ; tick ( 500 ) ; fixture . detectChanges ( ) ; expect ( ( strengthElement . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '密码强度 中' ) ; expect ( strengthElement . children [ 0 ] . children [ 0 ] . classList . contains ( 'strength-text-average' ) ) . toBe ( true ) ; expect ( ( strengthElement . children [ 0 ] . children [ 0 ] as HTMLElement ) . innerText ) . toBe ( '中' ) ; expect ( strengthElement . children [ 1 ] . classList . contains ( 'strength-level-average' ) ) . toBe ( true ) ; } ) ) ; } ) ;