I'm trying to test a directive which doesn't let me paste any data into an input element.
The directive looks like this:
import { Directive, HostListener } from '@angular/core';
/**
* Applied to an input, it blocks the paste functionality
*/
@Directive({
selector: '[kycBlockPaste]'
})
export class BlockPasteDirective {
/**
* Hooks for paste event and suppress it
*
* @param e
*/
@HostListener('paste', ['$event']) blockPaste(e: KeyboardEvent) {
e.preventDefault();
}
}
The way I thought of testing it is that the value of the input should change when adding a paste InputEvent but it doesn't. In both cases when applying the directive and not, the value is represented by an empty string.
Any ideas on how to test it? Thanks in advance :)
The test looks like this:
@Component({
template: '<input type="text" kycBlockPaste>'
})
class TestBlockPasteDirectiveComponent {}
fdescribe('Block Paste directive', () => {
let component: TestBlockPasteDirectiveComponent;
let fixture: ComponentFixture<TestBlockPasteDirectiveComponent>;
let inputEl: DebugElement;
let nativeEl: HTMLInputElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestBlockPasteDirectiveComponent, BlockPasteDirective]
});
fixture = TestBed.createComponent(TestBlockPasteDirectiveComponent);
component = fixture.componentInstance;
inputEl = fixture.debugElement.query(By.css('input'));
nativeEl = inputEl.nativeElement;
});
it('should hook for paste event and suppress it', () => {
const inputEvent = new InputEvent('paste', {
data: 'test input',
});
nativeEl.dispatchEvent(inputEvent);
fixture.detectChanges();
expect(nativeEl.value).toEqual('');
});
});type here
You can mock clipboard data. I am using Jest.