Reputation: 57
Below is my code snippet
code
applyFilter(event: Event): void {
const filterValue = (event.target as HTMLInputElement).value;
this.filterBy = filterValue.trim().toLowerCase();
this.dataSource.filter = filterValue.trim().toLowerCase();
}
Test cases
fit('applyFilter', () => {
const event = new Event('click');
component.applyFilter(event);
});
Error i am getting is "TypeError: Cannot read property 'value' of null" while this conversion "(event.target as HTMLInputElement).value;" meentioned error is occuring
Upvotes: 2
Views: 2754
Reputation: 18859
I would just mock the object.
it('should change filterBy and dataSource.filter', () => {
const event = { target: { value: 'hello' } } as any;
component.applyFilter(event);
expect(component.filterBy).toBe('hello');
expect(component.dataSource.filter).toBe('hello');
});
Upvotes: 4