import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ; import { UnitHelper } from './../../../../test-config/unit-test-helper' ; import { VerifyKeyComponent } from './verify-key.component' ; describe ( 'Component: VerifyKey' , ( ) => { let component ; let fixture ; const correctMnemonic = 'usage puzzle bottom amused genuine bike brown ripple lend aware symbol genuine neutral tortoise pluck rose brown cliff sing smile appear black occur zero' ; let unitHelper ; beforeEach ( ( ) => { unitHelper = new UnitHelper ( ) ; TestBed . configureTestingModule ( unitHelper . testBed ( { declarations : [ ] } ) ) . compileComponents ( ) . catch ( console . error ) ; } ) ; beforeEach ( async ( ) => { fixture = TestBed . createComponent ( VerifyKeyComponent ) ; component = fixture . componentInstance ; } ) ; it ( 'should validate a regular mnemonic, and emit correct event' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; fixture . detectChanges ( ) ; const words = component . secret . split ( ' ' ) ; component . onContinue . subscribe ( ( event ) => { expect ( event ) . toBeTruthy ( ) ; } ) ; words . forEach ( ( word ) => { expect ( component . isFull ( ) ) . toBeFalsy ( ) ; component . useWord ( word ) ; } ) ; expect ( component . isFull ( ) ) . toBeTruthy ( ) ; expect ( component . isCorrect ( ) ) . toBeTruthy ( ) ; } ) ) ; it ( 'should detect a wrong word in a mnemonic' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; fixture . detectChanges ( ) ; const words = component . secret . split ( ' ' ) ; component . onContinue . subscribe ( ( event ) => { expect ( event ) . toBeFalsy ( ) ; } ) ; words . forEach ( ( word , i ) => { expect ( component . isFull ( ) ) . toBeFalsy ( ) ; if ( i === 5 ) { component . useWord ( 'wrongWord' ) ; } else { component . useWord ( word ) ; } } ) ; expect ( component . isFull ( ) ) . toBeTruthy ( ) ; expect ( component . isCorrect ( ) ) . toBeFalsy ( ) ; } ) ) ; it ( 'should validate a mnemonic where the same word appears 2 times' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; fixture . detectChanges ( ) ; component . ngOnInit ( ) ; const words = component . secret . split ( ' ' ) ; words . forEach ( ( word ) => { component . useWord ( word ) ; } ) ; expect ( component . isCorrect ( ) ) . toBeTruthy ( ) ; } ) ) ; it ( 'should not validate user input that is too short' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; fixture . detectChanges ( ) ; component . ngOnInit ( ) ; const words = component . secret . split ( ' ' ) ; words . forEach ( ( word , i ) => { if ( i === words . length - 1 ) { return ; } component . useWord ( word ) ; } ) ; expect ( component . isFull ( ) ) . toBeFalsy ( ) ; expect ( component . isCorrect ( ) ) . toBeFalsy ( ) ; component . useWord ( words [ words . length - 1 ] ) ; expect ( component . isFull ( ) ) . toBeTruthy ( ) ; expect ( component . isCorrect ( ) ) . toBeTruthy ( ) ; } ) ) ; it ( 'should give the correct empty spots' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; fixture . detectChanges ( ) ; component . ngOnInit ( ) ; const words = component . secret . split ( ' ' ) ; expect ( component . emptySpot ( component . currentWords ) ) . toEqual ( 0 ) ; component . useWord ( words [ 0 ] ) ; expect ( component . emptySpot ( component . currentWords ) ) . toEqual ( 1 ) ; } ) ) ; it ( 'should let users select words to correct them' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; fixture . detectChanges ( ) ; component . ngOnInit ( ) ; const words = component . secret . split ( ' ' ) ; words . forEach ( ( word ) => { component . useWord ( word ) ; } ) ; component . selectWord ( 5 ) ; expect ( component . selectedWordIndex ) . toEqual ( 5 ) ; expect ( component . currentWords [ 5 ] ) . toEqual ( words [ 5 ] ) ; } ) ) ; it ( 'should give users 3 words to choose from' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; component . ngOnInit ( ) ; fixture . detectChanges ( ) ; const wordSelector = fixture . nativeElement . querySelector ( '#wordSelector' ) ; expect ( wordSelector . children . length ) . toBe ( 3 ) ; let foundWord = false ; for ( let i = 0 ; i < wordSelector . children . length ; i ++ ) { if ( wordSelector . children . item ( i ) . textContent . trim ( ) === correctMnemonic . split ( ' ' ) [ 0 ] ) { foundWord = true ; } } expect ( foundWord ) . toBeTruthy ( ) ; } ) ) ; it ( 'should give users 3 words to choose from if selecting a specific one' , waitForAsync ( ( ) => { component . secret = correctMnemonic ; component . ngOnInit ( ) ; fixture . detectChanges ( ) ; component . secret . split ( ' ' ) . forEach ( ( word , i ) => { if ( i > 10 ) { return ; } component . useWord ( word ) ; } ) ; const selectedIndex = 5 ; component . selectWord ( selectedIndex ) ; fixture . detectChanges ( ) ; const wordSelector = fixture . nativeElement . querySelector ( '#wordSelector' ) ; expect ( wordSelector . children . length ) . toBe ( 3 ) ; let foundWord = false ; for ( let i = 0 ; i < wordSelector . children . length ; i ++ ) { if ( wordSelector . children . item ( i ) . textContent . trim ( ) === correctMnemonic . split ( ' ' ) [ selectedIndex ] ) { foundWord = true ; } } expect ( foundWord ) . toBeTruthy ( ) ; } ) ) ; } ) ;