Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to mock e.preventdefault in angular

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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

>Solution :

Just replace void with null.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading