angular - 指令绑定(bind)在规范文件中不起作用

标签 angular unit-testing angular-directive

我有一个突出显示文本的指令:

import { Directive, ElementRef, HostListener, Input } from '@angular/core';

@Directive({ selector: '[appHighlight]' })
export class HighlightDirective {
  @Input('appHighlight') // tslint:disable-line no-input-rename
  highlightColor: string;

  constructor(private el: ElementRef) { }

  @HostListener('mouseenter')
  onMouseEnter() {
    this.highlight(this.highlightColor || 'yellow');
  }

  @HostListener('mouseleave')
  onMouseLeave() {
    this.highlight(null);
  }

  private highlight(color: string) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

在我的应用程序 HTML 上,我有:

This <span [appHighlight]="'pink'">is nice</span>!

它有效

hover text changes color

然后我开始构建一些测试,在一个测试中我尝试绑定(bind)一种不同的颜色(就像上面的例子),但它没有绑定(bind)值,该字段是 undefined .

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';

@Component({
  selector: 'app-test-container',
  template: `
    <div>
      <span id="red" appHighlight>red text</span>
      <span id="green" [appHighlight]="'green'">green text</span>
      <span id="no">no color</span>
    </div>
  `
})
class ContainerComponent { }

const mouseEvents = {
  get enter() {
    const mouseenter = document.createEvent('MouseEvent');
    mouseenter.initEvent('mouseenter', true, true);
    return mouseenter;
  },
  get leave() {
    const mouseleave = document.createEvent('MouseEvent');
    mouseleave.initEvent('mouseleave', true, true);
    return mouseleave;
  },
};

fdescribe('HighlightDirective', () => {
  let fixture: ComponentFixture<ContainerComponent>;
  let container: ContainerComponent;
  let element: HTMLElement;

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

    fixture = TestBed.createComponent(ContainerComponent);
    container = fixture.componentInstance;
    element = fixture.nativeElement;
  });

  fit('should set background-color green when passing green parameter', () => {
    const targetElement = <HTMLSpanElement>element.querySelector('#green');

    targetElement.dispatchEvent(mouseEvents.enter);
    expect(targetElement.style.backgroundColor).toEqual('green');
  });
});

测试输出显示

fallback color

我做错了什么吗?为什么它不绑定(bind) green 颜色?

最佳答案

我发现默认情况下,Angular 在测试期间不会在模板上运行绑定(bind)。即使是简单的 {{ myVar }} 也不会工作,除非您让它运行绑定(bind)和生命周期事件,如 Angular detectchanges documentation 中所述。 .

这个场景有两个选项,我可以手动调用

fixture.detectChanges();

就在我拿到固定装置之后。

或者我可以包含一个将其设置为自动运行的提供程序

providers: [
  { provide: ComponentFixtureAutoDetect, useValue: true },
],

关于angular - 指令绑定(bind)在规范文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256358/

相关文章:

c# - 使用起订量,System.InvalidCastException : Unable to cast object of type 'CaSTLe.Proxies.ObjectProxy' to type

unit-testing - 使用mockito模拟GWT EventBus

javascript - Angular Http Get 在 Chrome/Safari 与 Firefox 中返回不同

unit-testing - 在 console.log 上,ViewChild 变量在单元测试中未定义

python-3.x - Python 中的测试驱动开发

angularjs - 指令 Controller 内的 Angular 集脏形式

javascript - 如何维护每个 ngFor 项目的按钮状态

css - 应用来自父项的样式

Angular 5从web api ClosedXml下载excel文件

angular - 多指令选择器 - 查找模板中使用了哪个选择器