import 'reflect-metadata'; import { ElementRef, } from '@angular/core'; import { DebounceOnChangeDirective } from './input-debounce.directive'; const initDebounceOnChangeDirective = ( elementRef: ElementRef, ) => { return new DebounceOnChangeDirective(elementRef); }; describe('onChange', () => { test('Keyup emits the onChange after the debounce time', () => { jest.useFakeTimers(); const elementRef = { nativeElement: document.createElement('input'), } as ElementRef; const debounceOnChangeDirective = initDebounceOnChangeDirective(elementRef); debounceOnChangeDirective.onChange.emit = jest.fn(); debounceOnChangeDirective.ngOnInit(); debounceOnChangeDirective.onKeyUp(); const keyupEvent = new Event('keyup'); elementRef.nativeElement.dispatchEvent(keyupEvent); expect( debounceOnChangeDirective.onChange.emit, ).not.toHaveBeenCalled(); jest.runAllTimers(); expect( debounceOnChangeDirective.onChange.emit, ).toHaveBeenCalled(); }); test('Change emits the onChange after the debounce time', () => { jest.useFakeTimers(); const elementRef = { nativeElement: document.createElement('input'), } as ElementRef; const debounceOnChangeDirective = initDebounceOnChangeDirective(elementRef); debounceOnChangeDirective.onChange.emit = jest.fn(); debounceOnChangeDirective.ngOnInit(); const changeEvent = new Event('change'); elementRef.nativeElement.dispatchEvent(changeEvent); expect( debounceOnChangeDirective.onChange.emit, ).not.toHaveBeenCalled(); jest.runAllTimers(); expect( debounceOnChangeDirective.onChange.emit, ).toHaveBeenCalled(); }); });