typescript 测试DOM中属性的存在

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 测试DOM中属性的存在相关的知识,希望对你有一定的参考价值。

import {
  ViewChild,
  Component,
} from '@angular/core';
import {
  async,
  ComponentFixture,
  TestBed,
} from '@angular/core/testing';
import {
  FormsModule,
  ReactiveFormsModule,
} from '@angular/forms';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { TsInputModule } from './input.module';
import { TsInputComponent } from './input.component';


@Component({
  template: `
    <ts-input [isRequired]="myRequired"></ts-input>
  `,
})
class TestHostComponent {
  myRequired = true;
}


describe(`TsInputComponent-integration`, () => {
  let fixture;
  let testHost;
  let inputEl;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        NoopAnimationsModule,
        FormsModule,
        ReactiveFormsModule,
        TsInputModule,
      ],
      declarations: [
        TestHostComponent,
      ],
    }).compileComponents();
    fixture = TestBed.createComponent(TestHostComponent);
    testHost = fixture.componentInstance;
    inputEl = document.getElementsByClassName('c-input__text')[0];
    fixture.detectChanges();
  }));


  test(`should set the required attr`, () => {
    expect(inputEl.hasAttribute('required')).toEqual(true);
  });


  test(`should remove the required attr`, () => {
    testHost.myRequired = false;
    fixture.detectChanges();
    expect(inputEl.hasAttribute('required')).toEqual(false);
  });

});

以上是关于typescript 测试DOM中属性的存在的主要内容,如果未能解决你的问题,请参考以下文章

React:react-router-dom Typescript 中不存在 useLocation Hook

typescript+reactjs+requirejs:如何将 react-dom 导入 typescript

TypeScript:类型“”上不存在属性

如何简化检查 TypeScript 中是不是存在属性?

VueJS + Typescript:类型上不存在属性

忽略 Typescript 错误“类型的值不存在属性”