Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

javascript - error TS2304: Cannot find name 'createNewEvent'

I am writing a Unit Test for my input box in Angular 11.

Here is my spec.ts code:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import 'jasmine';
import { ExpectedConditions } from 'protractor';
import { MicroplateComponent } from './microplate.component';

describe('MicroplateComponent', () => {
  let component: MicroplateComponent;
  let fixture: ComponentFixture<MicroplateComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [MicroplateComponent]
    })
      .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MicroplateComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  describe('Evaluation of input box', () => {
    it('Should return true for "1, 2, 3"', () => {
      const input = fixture.nativeElement.querySelector('input');
      const event = createNewEvent('input'); // I get error at this line

      input.value = '1, 2, 3';
      input.dispatchEvent(event);

      fixture.detectChanges();

      expect(component.columns?.valid).toBe(true);
    });
  });
});

I get the following error, but I don't know where I am wrong.

error TS2304: Cannot find name 'createNewEvent'.

I have used such an approach from angular website.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are trying to invoke an input event that's how you do it:

input.dispatchEvent(new Event('input'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...