2016-12-29 3 views
2

У меня есть образец кода для флажка, написанного на Angular2.Как сдать блок checkbox в Angular2

<div class="col-sm-7 align-left" *ngIf="Some-Condtion"> 
    <input type="checkbox" id="mob_Q1" value="Q1" /> 
    <label for="mob_Q1">Express</label> 
</div> 

Я хочу выполнить установку вышеперечисленного. Например, я хочу распознать флажок и проверить, является ли он проверкой. Как я могу проверить это с помощью Karma Jasmine?

ответ

3

Компонент, например. CheckboxComponent содержит элемент ввода. Единичный тест должен выглядеть так:

import {ComponentFixture, TestBed} from '@angular/core/testing'; 
import {By} from '@angular/platform-browser'; 
import {CheckboxComponent} from './checkbox.component'; 

describe('Checkbox test.',() => { 

let comp: CheckboxComponent; 
let fixture: ComponentFixture<CheckboxComponent>; 
let input: Element; 

beforeEach(() => { 
    TestBed.configureTestingModule(
     { 
      declarations: [CheckboxComponent], 
     }, 
    ); 

    fixture = TestBed.createComponent(CheckboxComponent); 
    comp = fixture.componentInstance; 
    input = fixture.debugElement.query(By.css('#mob_Q1')).nativeElement; 
}); 

it('should click change value',() => { 
    expect(input.checked).toBeFalsy(); // default state 

    input.click(); 
    fixture.detectChanges(); 

    expect(input.checked).toBeTruthy(); // state after click 
}); 
});