Skip to content

Instantly share code, notes, and snippets.

@merlosy
Last active February 9, 2024 15:31
Show Gist options
  • Save merlosy/59dcb1fb76eafc469748ea7e06cdfaa7 to your computer and use it in GitHub Desktop.
Save merlosy/59dcb1fb76eafc469748ea7e06cdfaa7 to your computer and use it in GitHub Desktop.
Angular Jasmine Testing utils
import { DebugElement } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
/** Find an element by integration test id
* @return a debugElement or null when not found
*/
export function findByIntId<T>(fixture: ComponentFixture<T> , intId: string): DebugElement | null {
return fixture.debugElement.query(By.css(`[data-int=${intId}]`));
}
/** Find an HTML element by integration test id
* @return a HTMLElement
*/
export function findNativeElementByIntId<T>(fixture: ComponentFixture<T> , intId: string): any {
try {
return findByIntId(fixture, intId).nativeElement;
} catch (e) {
// Allow a more debuggable info than "Cannot get nativeElement of null"
throw new Error(`Cannot get native element from integration id: ${intId}`);
}
}
/**
* Triggers user click on a select option given the option index
* @param fixture
* @param intId
* @param index
*/
export function clickOptionByIndex<T>(fixture: ComponentFixture<T>, intId: string, index: number): void {
const select: HTMLSelectElement = findNativeElementByIntId(fixture, intId) as HTMLSelectElement;
select.click();
fixture.detectChanges();
const options: DebugElement[] = fixture.debugElement.queryAll(By.css(`[data-int=${intId}] option`));
const event: Event = new Event('change', { bubbles: true, cancelable: true });
options[index].nativeElement.dispatchEvent(event);
fixture.detectChanges();
}
/**
* Type a value in an input field
*/
export function typeValue<T>(fixture: ComponentFixture<T>, intId: string, text: string | null): void {
const input: HTMLInputElement = findNativeElementByIntId(fixture, intId);
input.value = text;
input.dispatchEvent(new Event('input'));
fixture.detectChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment