首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角2/4组件单元测试单击事件不会触发更改。

角2/4组件单元测试单击事件不会触发更改。
EN

Stack Overflow用户
提问于 2017-07-11 11:16:43
回答 1查看 9.6K关注 0票数 2

我正在尝试单元测试一个角度为2/4的组件,并希望查看单击按钮元素是否会导致所需的更改。但是,我无法触发单击事件。

组件

代码语言:javascript
复制
import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } 
from '@angular/core';
@Component({
    selector: 'input-filter',
    template: `
<div class="input-widget">
    <div class="icon-filter"></div>
    <input
        type="text"
        [value]="value"
        (input)="filter.emit($event.target.value)"
        placeholder="... filter"/>
    <span (click)="clear()" class="clear icon-clear-field_S"></span>
</div>
    `,
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class InputFilterComponent {
    @Input() value;
    @Output() filter = new EventEmitter(false);
    clear() {
        this.value = '';
        this.filterBoxes = [];
        this.filter.emit(this.value);
    }
}

测试

代码语言:javascript
复制
import { TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { InputFilterComponent } from './input-filter.component';
import { Component} from '@angular/core';
const testValue = 'test1234';
@Component({
 selector  : 'test-cmp',
 template  : `<input-filter [value]="testValueC"></input-filter>`,
})
class TestCmpWrapper {
    testValueC = testValue; // mock input
}
describe('input-filter.component', () => {
    let fixture;
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [FormsModule],
            declarations: [TestCmpWrapper, InputFilterComponent],
        });
        fixture = TestBed.createComponent(TestCmpWrapper);
    });
    it('should clear on click', () => {
        let testHostComponent = fixture.componentInstance;
        const el = fixture.debugElement.nativeElement;

        // both methods to trigger click do not work
        el.querySelector('.clear').click(); 
        el.querySelector('.clear').dispatchEvent(new Event('click'));

        fixture.detectChanges();
        fixture.whenStable().then(() => {
            expect(el.querySelector('input').value).toBe('');
        })
    });
});

HeadlessChrome 0.0.0 (Linux0.0.0)输入-filter.Component应该清除单击FAILED,期望'test1234‘为'’。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-11 12:00:03

试试下面的代码。您也可以在不添加fakeAsync的情况下这样做。只需在测试代码之前添加fixture.detectChanges();

代码语言:javascript
复制
import { TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { InputFilterComponent } from './input-filter.component';
import { Component } from '@angular/core';
const testValue = 'test1234';
@Component({
  selector: 'test-cmp',
  template: `<input-filter [value]="testValueC"></input-filter>`,
})
class TestCmpWrapper {
  testValueC = testValue; // mock input
}
fdescribe('input-filter.component', () => {
  let fixture;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [FormsModule],
      declarations: [TestCmpWrapper, InputFilterComponent],
    });
    fixture = TestBed.createComponent(TestCmpWrapper);
  });
  it('should clear on click', () => {
    fixture.detectChanges();
    const testHostComponent = fixture.componentInstance;
    const el = fixture.debugElement.nativeElement;

    // both methods to trigger click do not work
    el.querySelector('.clear').click();
    el.querySelector('.clear').dispatchEvent(new Event('click'));

    fixture.detectChanges();

    expect(el.querySelector('input').value).toBe('');

  });
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45033027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档