Greetings! I am currently working on writing unit test cases in Angular5 Jasmine related to submitting a form. Below is the structure of my form:
<form *ngIf="formResetToggle" class="form-horizontal" name="scopesEditorForm" #f="ngForm" novalidate (ngSubmit)="f.form.valid && isScopeValid() ? saveScope() : showErrorAlert('Please enter mandatory fields')">
<input autofocus type="text" id="scopename" name="scopename" placeholder="Enter scope name" class="form-control" [(ngModel)]="scopeEdit.scopevalue" #scopename="ngModel" required />
<button type="button" (click)="editorModal.hide()" class="btn btn-default" data-dismiss="modal">Cancel</button>
</form>
Now, let's take a look at my spec.ts file:
describe('ScopeEditorComponent', () => {
let component: ScopeEditorComponent;
let fixture: ComponentFixture<ScopeEditorComponent>;
let submitEl: DebugElement;
let scopeValue: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule,
RouterTestingModule,
],
declarations: [
ScopeEditorComponent,
SearchBoxComponent,
GroupByPipe,
]
fixture = TestBed.createComponent(ScopeEditorComponent);
component = fixture.componentInstance;
service = new PermissionEndpointMock();
fixture.detectChanges();
submitEl = fixture.debugElement.query(By.css('button')).nativeElement;
scopeValue = fixture.debugElement.query(By.css('#scopename'));
}));
it('should create the scope component', async(() => {
expect(component).toBeTruthy();
}));
it('add scope', () => {
let scope: Scope;
scopeValue.nativeElement.value = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dca8b9afa89cb9a4bdb1acb0b9f2bfb3b1">[email protected]</a>";
// Subscribe to the Observable and store the user in a local variable.
component.addscopeEventEmitter.subscribe((value) => scope = value);
// This sync emits the event and the subscribe callback gets executed above
submitEl.triggerEventHandler('click', null);
// Now we can check to make sure the emitted value is correct
expect(scope.scopeid).toBe("123456");
});
However, when running the code, an error stating "submitEl.triggerEventHandler is not a function" is encountered. Any assistance on resolving this issue will be greatly appreciated. Thank you!