2

I wish to test an anguar5 component using a host component as described in angular.io doc.

But my test keep failing because of

TypeError: Cannot read property 'query' of null
    at UserContext.<anonymous> (http://localhost:9876/base/config-webpack/spec-bundle.js:293832:39)
    at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/base/config-webpack/spec-bundle.js:288418:26)
    at ProxyZoneSpec../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/base/config-webpack/spec-bundle.js:287920:39)
    at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/base/config-webpack/spec-bundle.js:288417:32)
    at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/base/config-webpack/spec-bundle.js:288168:43)
    at UserContext.<anonymous> (http://localhost:9876/base/config-webpack/spec-bundle.js:287799:34)
    at attempt (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4289:46)
    at ZoneQueueRunner.QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4217:20)
    at ZoneQueueRunner.QueueRunner.execute (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:4199:10)
    at ZoneQueueRunner../node_modules/zone.js/dist/jasmine-patch.js.jasmine.QueueRunner.ZoneQueueRunner.execute (http://localhost:9876/base/config-webpack/spec-bundle.js:287827:42)

Indeed, when I log my fixture.debugElement, it return null.

my test code is :

import {} from 'jasmine';

import { Component } from '@angular/core';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { By }              from '@angular/platform-browser';
import { DropDownListComponent } from './dropDownList.component';

@Component({
    template: '<dropdown-list [valuesList]="valuesList" [selectedValue]="valuesSelected" ></dropdown-list>'
})
class TestComponent {
    valuesList = [{label: 'test_select', value:'test'}, {label: 'test_select2', value:'test2'}];
    valuesSelected = {label: 'test_select', value:'test'};
}

describe('DropDownListComponent', () => {
    let fixture, component;

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

    it('should display selectedValue', () => {
        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        console.log(fixture.isStable());
        console.log(fixture);
        console.log(component);
        console.log(fixture.debugElement);
        //const fixture = TestBed.createComponent(TestComponent);
        let de = fixture.debugElement.query(By.css(".value-container"));
        let el = de.nativeElement;
        expect(el.textContent).toContain('test_select');
    });
});
GPif
  • 543
  • 2
  • 6
  • 22
  • Did you find any solution for this issue. I am also facing the same issue with Angular 4.0.0 version. for me also fixture.debugElement returns null whereas fixture.nativeElement prints the root html component – zulu Feb 11 '18 at 17:21
  • Unfortunately no. I think I will rewrite my test from scratch, and solve it get better. – GPif Feb 12 '18 at 13:42

1 Answers1

0

When you write a test you need to test only your component / service / pipe / directive etc, but not its dependencies.

From the code above I assume DropDownListComponent has a DI dependency that wasn't declared in providers of TestBed and it causes the issue. Anyway in this context it should be a mock, not a real component.

If you want to test DropDownListComponent - then please share its code. Without understanding its interface it's hard to guess how to write tests for it.

You can use ng-mocks to mock it and then you would only need to test that [valuesList] and [selectedValue] got right values.

Also to compile all components correctly you need to handle compileComponents's promise.

describe('TestComponent', () => {
    beforeEach(() => {
        return TestBed.configureTestingModule({
            declarations: [TestComponent,  MockComponent(DropDownListComponent)],
        }).compileComponents();
    });

    it('should display selectedValue', () => {
        const fixture = MockRender(TestComponent);
        const dropdown = MockHelper.findOrFail(fixture.debugElement, DropDownListComponent);
        expect(dropdown.valuesList).toEqual(fixture.point.valuesList);
        expect(dropdown.selectedValue).toEqual(fixture.point.valuesSelected);
    });
});

Profit.

satanTime
  • 12,631
  • 1
  • 25
  • 73