Angular 2 and jasmine unit testing: unable to get innerHtml

I use one of the examples that test the "WelcomeComponent" component:

import { Component, OnInit } from '@angular/core';
import { UserService }       from './model/user.service';

@Component({
    selector: 'app-welcome',
     template: '<h3>{{welcome}}</h3>'
})
export class WelcomeComponent implements OnInit {
    welcome = '-- not initialized yet --';
    constructor(private userService: UserService) { }

    ngOnInit(): void {
        this.welcome = this.userService.isLoggedIn ?
            'Welcome  ' + this.userService.user.name :
            'Please log in.';
    }
}

This is a test case, I am checking if "h3" contains the username "Bubba":

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By }              from '@angular/platform-browser';
import { DebugElement }    from '@angular/core';

import { UserService }       from './model/user.service';
import { WelcomeComponent } from './welcome.component';


describe('WelcomeComponent', () => {

    let comp: WelcomeComponent;
    let fixture: ComponentFixture<WelcomeComponent>;
    let componentUserService: UserService; // the actually injected service
    let userService: UserService; // the TestBed injected service
    let de: DebugElement;  // the DebugElement with the welcome message
    let el: HTMLElement; // the DOM element with the welcome message

    let userServiceStub: {
        isLoggedIn: boolean;
        user: { name: string }
    };

    beforeEach(() => {
        // stub UserService for test purposes
        userServiceStub = {
            isLoggedIn: true,
            user: { name: 'Test User' }
        };

        TestBed.configureTestingModule({
            declarations: [WelcomeComponent],
            // providers:    [ UserService ]  // NO! Don't provide the real service!
            // Provide a test-double instead
            providers: [{ provide: UserService, useValue: userServiceStub }]
        });

        fixture = TestBed.createComponent(WelcomeComponent);
        comp = fixture.componentInstance;

        // UserService actually injected into the component
        userService = fixture.debugElement.injector.get(UserService);
        componentUserService = userService;
        // UserService from the root injector
        userService = TestBed.get(UserService);
        //  get the "welcome" element by CSS selector (e.g., by class name)
        el = fixture.debugElement.nativeElement; // de.nativeElement;
    });


    it('should welcome "Bubba"', () => {
        userService.user.name = 'Bubba'; // welcome message hasn't been shown yet
        fixture.detectChanges();
        const content = el.querySelector('h3');
        expect(content).toContain('Bubba');
    });
});

When testing and debugging a test case using Karma, if I evaluate "el.querySelector (" h3 ") in the console, it shows the following

<h3>Welcome  Bubba</h3>

How can I get the innerHtml of the header, since it is not resolved when included in the ts file, and the test case is always false.

enter image description here

Here's what he says: "innerHTML" does not exist in the type "HTMLHeadingElement"

+4
source share
1

, content

const content = el.querySelector('h3');
expect(content).toContain('Bubba');

HTMLNode, . , HTMLNode . .

HTML content.innerHTML, content.textContent ( <h3>

const content = el.querySelector('h3');
expect(content.innerHTML).toContain('Bubba');
expect(content.textContent).toContain('Bubba');
+5

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


All Articles