gpt4 book ai didi

angular - Angular 2+中带有参数的单元测试指令

转载 作者:行者123 更新时间:2023-12-05 01:37:07 24 4
gpt4 key购买 nike

我有一个指令,它在鼠标悬停期间更改元素的背景颜色,如下所示。

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

@Directive({
selector: '[appHighlightme]'
})
export class HighlightmeDirective {

@Input('appHighlightme') color : string
constructor(private el:ElementRef) { }

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

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

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

我正在尝试为该指令编写一个单元测试用例,如下所示

import { HighlightmeDirective } from './highlightme.directive';
import {ChangeDetectionStrategy, Component, DebugElement, ViewChild} from '@angular/core';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';

@Component({
selector: 'my-test-component',
template: '<a [appHighlightme]="color" >test</a>'
})
export class TestComponent {
color:string = 'blue';
}

describe('HighlightmeDirective', () => {

let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let inputEl: DebugElement;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
HighlightmeDirective
]
})

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
inputEl = fixture.debugElement.query(By.directive(HighlightmeDirective));

});

it('detect hover changes', () => {
inputEl.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
expect(inputEl.nativeElement.style.backgroundColor).toBe(component.color);
inputEl.triggerEventHandler('mouseleave', {});
fixture.detectChanges();
expect(inputEl.nativeElement.style.backgroundColor).toBe('inherit');
});

it('should create an instance', () => {
const directive = new HighlightmeDirective(inputEl);
expect(directive).toBeTruthy();
});
});

该指令在其他组件中运行良好。它接受在 ts 文件中定义的颜色参数变量,并在悬停元素时将其用作 bg 颜色。但是,当我尝试对其进行单元测试时,指令未检测到从 TestComponent 传递的颜色参数,并且测试用例失败并显示以下错误消息。

错误:预期“黄色”为“蓝色”。 - 因为黄色被设置为默认悬停颜色

最佳答案

您不需要通过指令跟踪事件处理程序。您需要在 native 元素(在您的情况下为 anchor 标记)上发出一个事件,指令处理程序将被自动调用。这是测试指令的实际方法。

假设我们有一个带有类的 anchor 标记,因此我们需要在该标记上创建一个事件。

@Component({
selector: 'my-test-component',
template: '<a class="mytag" [appHighlightme]="color" >test</a>'
})
export class TestComponent {
color:string = 'blue';
}

describe('HighlightmeDirective', () => {

let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let inputEl: DebugElement;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
HighlightmeDirective
]
})

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges(); // trigger change detection so that UI renders and you can access element in next step.
inputEl = fixture.debugElement.query(By.css('.mytag'));

});

it('detect hover changes', () => {
inputEl.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
expect(inputEl.nativeElement.style.backgroundColor).toBe(component.color);
inputEl.triggerEventHandler('mouseleave', {});
fixture.detectChanges();
expect(inputEl.nativeElement.style.backgroundColor).toBe('inherit');
});

it('should create an instance', () => {
const directive = new HighlightmeDirective(inputEl);
expect(directive).toBeTruthy();
});
});

希望对您有所帮助。

关于angular - Angular 2+中带有参数的单元测试指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61359262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com