我在组件类的开头初始化了一个HTMLElement类型,如下所示: 我的组件类:
import { Component, AfterViewInit, Renderer, ElementRef, OnInit} from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import { JhiEventManager } from 'ng-jhipster';
@Component({
selector: 'jhi-login-modal',
templateUrl: './login.component.html'
})
export class JhiLoginModalComponent implements AfterViewInit, OnInit {
domElement: HTMLElement;
constructor(
private login1: ElementRef
) {
}
ngOnInit() {
this.accessDomElement();
}
accessDomElement() {
this.domElement = this.login1.nativeElement.querySelector(`#login123`);
}
我的测试课程:
import { ComponentFixture, TestBed, async, inject, fakeAsync, tick } from '@angular/core/testing';
describe('LoginComponent', () => {
let comp: JhiLoginModalComponent;
let fixture: ComponentFixture<JhiLoginModalComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
............
}));
beforeEach(() => {
fixture = TestBed.createComponent(JhiLoginModalComponent);
comp = fixture.componentInstance;
....
});
//MY TEST
it ('The form login should have the field Nombre', async(() => {
console.log('lalalalla' + comp.domElement);
expect(comp.domElement.innerHTML).toContain('Login');
}));
现在我想在测试类中测试domElement。
但是当我打印domElement时它返回undefined。
我的html eleemnt:
<label id="login123" for="username" jhiTranslate="global.form.username">Login</label>
我想测试这种类型HTMLELEMENT的元素DOMeLEMET。 注射时会出错吗?因为它给了我这个错误: 无法读取未定义的prperty innerHTML,并在控制台中输出undefined。