-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.utils.ts
More file actions
51 lines (46 loc) · 1.32 KB
/
jest.utils.ts
File metadata and controls
51 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {DebugElement} from '@angular/core';
import {ComponentFixture} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
/**
* Reads an Angular input value whether it's a regular variable or a Signal (function)
* @param property
*/
export function readInputValue(property: unknown) {
if (typeof property === 'function' ) {
return property();
} else {
return property;
}
}
/**
* Returns the trimmed text contents of a native HTML element
* @param element
*/
export function trimmedContent(element: DebugElement): string {
return element.nativeElement.textContent.trim();
}
/**
* Returns the trimmed HTML contents of a native HTML element
* @param element
*/
export function trimmedHtml(element: DebugElement): string {
return element.nativeElement.innerHTML.trim();
}
/**
* Selects an element by data-test id
* @param id
* @param fixture
*/
export function getByDataTest<T>(id: string, fixture: ComponentFixture<T>): DebugElement {
return fixture.debugElement.query(By.css(`[data-test=${id}]`));
}
/**
* Simulates a click on an element + runs change detection
* @param element
* @param fixture
*/
export function click<T>(element: DebugElement, fixture: ComponentFixture<T>) {
const el = element.nativeElement;
el.dispatchEvent(new Event("click"));
fixture.detectChanges();
}