Angular2 Inject ElementRef in unit test

I am trying to test a component that gets a reference to ElementRef via DI.

import { Component, OnInit, ElementRef } from '@angular/core';

@Component({
  selector: 'cp',
  templateUrl: '...',
  styleUrls: ['...']
})
export class MyComponent implements OnInit {

  constructor(private elementRef: ElementRef) {
    //stuffs
  }

  ngAfterViewInit() {
    // things
  }

  ngOnInit() {
  }
}

and test:

import {
  beforeEach,
  beforeEachProviders,
  describe,
  expect,
  it,
  inject,
} from '@angular/core/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { Component, Renderer, ElementRef } from '@angular/core';
import { By } from '@angular/platform-browser';

describe('Component: My', () => {
  let builder: TestComponentBuilder;

  beforeEachProviders(() => [MyComponent]);
  beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder) {
    builder = tcb;
  }));

  it('should inject the component', inject([MyComponent],
      (component: MyComponent) => {
    expect(component).toBeTruthy();
  }));

  it('should create the component', inject([], () => {
    return builder.createAsync(MyComponentTestController)
      .then((fixture: ComponentFixture<any>) => {
        let query = fixture.debugElement.query(By.directive(MyComponent));
        expect(query).toBeTruthy();
        expect(query.componentInstance).toBeTruthy();
      });
  }));
});

@Component({
  selector: 'test',
  template: '
    <cp></cp>
  ',
  directives: [MyComponent]
})
class MyTestController {
}

Both the component and the test circuit were created by Angular-cli. Now I can’t find out which provider, if any, I have to add in beforeEachProvidersorder for the implementation of ElementRef to be successful. When I start, ng testI get a message about Error: No provider for ElementRef! (MyComponent β†’ ElementRef) Error: No provider for ElementRef! (MyComponent β†’ ElementRef).

+12
source share
4 answers

On Angular 2.2.3:

export class MockElementRef extends ElementRef {}

Then in the test:

beforeEach(async(() => {
  TestBed.configureTestingModule({
    providers: [
      //more providers
      { provide: ElementRef, useClass: MockElementRef }
    ]
  }).compileComponents();
}));
+6
source

I am facing Can't resolve all parameters for ElementRef: (?)error while using layout from @ gilad-s in angular 2.4

Modified class layout:

export class MockElementRef extends ElementRef {
  constructor() { super(null); }
}

fixes a test error

: https://github.com/angular/angular/blob/master/packages/core/testing/src/component_fixture.ts#L17-L60 elementRef . ElementRef . , TestBed .

+17

ElementRef:

class MockElementRef implements ElementRef {
  nativeElement = {};
}
beforeEachProviders(() => [Component, provide(ElementRef, { useValue: new MockElementRef() })]);

EDIT: rc4. .

+5

spyOn spyOnProperty . spyOnProperty 3 , get set . spyOn .

const div = fixture.debugElement.query(By.css('.ellipsis-overflow'));
// now mock properties
spyOnProperty(div.nativeElement, 'clientWidth', 'get').and.returnValue(1400);
spyOnProperty(div.nativeElement, 'scrollWidth', 'get').and.returnValue(2400);

Here I set the get clientWidth div.nativeElementobject div.nativeElement.

+1
source

Source: https://habr.com/ru/post/1690983/


All Articles