In my codebase, there exists a file called utils
which houses various utility functions that are utilized by different classes and components.
Currently, there is a test-utils
class designed specifically for karma unit tests to perform common tasks.
One of the methods in this test-utils class handles sending input to a native element:
export function sendInput(fixture: ComponentFixture<any>, inputElement: any, text: string, formControl?: AbstractControl): Promise<any> {
inputElement.value = text;
inputElement.dispatchEvent(new Event('input'));
if (!isNullOrUndefined(formControl)) {
formControl.markAsTouched();
}
fixture.detectChanges();
return fixture.whenStable();
}
The method can be invoked as shown below:
sendInput(fixture, myElement, 'testValue', formElement).then(() => { ... });
However, I'm looking for a way to access the calling context without having to pass parameters like fixture
. I am aware that fixture
is available in the calling context.
Question: How can I achieve access to the calling context in this situation? I attempted using an arrow function to pass the context through as follows:
export const sendInput = (inputElement: any, value: string, formControl?: AbstractControl): Promise<any> => {
// method body
const context = this;
// this, context is undefined
}
Despite this attempt, this
remains undefined. It seems like my understanding of JavaScript's intricacies may not be sufficient to address why this approach is unsuccessful.