Home > Enterprise >  how to mock e.preventdefault in angular
how to mock e.preventdefault in angular

Time:04-23

HTML file

<input type="number" min="10" max="100" (keydown)="checkLength1($event,inputNumber)"#inputNumber/>

ts file

  checkLength1(e: { key: string | number; keyCode: number; preventDefault: () => void; }, input: { value: string; }) {
    const keyValue =  e.key; const numberOnlyPattern = '[0-9] ';
    const newValue = input.value   (isNaN(keyValue) ? '' : keyValue.toString());
    const match = newValue.match(numberOnlyPattern);
    if ( newValue > 12 || !match || newValue === '') {e.preventDefault();} }

spec file

   it("checkLength1", () => { const e ={ key: "1", keyCode: 1, preventDefault : () => void }
    const input = {value:"1" }
   component.onKeyPressHours(e, input); });

I am trying to write test cases for "checkLength1" method. But I am getting an error while constructing object for parameter "e". Please help me to resolve this

CodePudding user response:

Just replace void with null.

it("checkLength1", () => { 
  const e = { key: "1", keyCode: 1, preventDefault : () => null };
  ...
  • Related