Not binding directive not working in spec file

I have a directive that highlights text:

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;
  }
}

      

In my HTML application, I have:

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

      

And it works

hover color change text

Then I started to create some tests and in one test I try to bind another color (like in the example above), but it doesn't bind the value, the field 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');
  });
});

      

The test output displays

fallback color

Am I doing something wrong? Why doesn't he tie the color green

?

+3


source to share


1 answer


I found out that by default Angular doesn't trigger template bindings during tests. Even a simple {{ myVar }}

one won't work unless you trigger the bindings and lifecycle events as described in the Angular detectchanges

documentation
.

There are two options in this scenario, I can manually call

fixture.detectChanges();

      



Right after I receive my game.

Or I can include a provider that automatically starts it.

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

      

+1


source







All Articles