Categories
JavaScript Answers

How to simulate keypress with JavaScript?

To simulate keypress with JavaScript, we can call the dispatchEvent method to trigger an event of our choice.

For instance, we can write:

window.addEventListener('keydown', (e) => {
  console.log(e)
})

window.dispatchEvent(new KeyboardEvent('keydown', {
  'key': 'a'
}));

to listen to the keydown event and trigger it.

We call addEventListener with 'keydown' to listen to the keydown event.

Then to trigger the keydown event, we call window.dispatchEvent with a KeyboardEvent instance.

We pass in the type of keyboard event to trigger into the first argument of the constructor.

And we pass in an object with the options to set in the event object into the 2nd argument.

Therefore, after dispatchEvent is run, then e.key should be 'a' when logged in the callback.

We can set more options in the object.

For instance, we can write:

window.addEventListener('keydown', (e) => {
  console.log(e)
})

window.dispatchEvent(new KeyboardEvent('keydown', {
  key: "e",
  keyCode: 69,
  code: "KeyE",
  which: 69,
  shiftKey: false,
  ctrlKey: false,
  metaKey: false
}));

to set more options in the object.

keyCode is the numeric code of the key that we want to set.

code has the name of the key.

which has the keyboard key number.

shiftKey sets whether we want to press the shift key in addition to the key we’re pressing.

ctrlKey sets whether we want to press the Ctrl key in addition to the key we’re pressing.

metaKey sets whether we want to press the meta key in addition to the key we’re pressing.

The meta key is the Windows key on PC keyboards and the command key on Mac keyboards.

When the event is triggered, we should see all the options logged in the addEventListener callback.

They should be in the same properties in the e object as in the options object we pass into the KeyboardEvent constructor.

We can write the same code with the keyup event.

We just replace 'keydown' with 'keyup' everywhere.

Categories
JavaScript Answers

How to fix Jest ‘encountered an unexpected token import’ error with JavaScript?

To fix Jest ‘encountered an unexpected token import’ error with JavaScript, we set the Jest config to allow modules.

For instance, in jest.config.js, we write

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  roots: ["./src"],
  transform: { "\\.ts$": ["ts-jest"] },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
  globals: {
    "ts-jest": {
      tsConfig: {
        allowJs: true,
      },
    },
  },
};

to allow extensions for modules by setting moduleFileExtensions to ["ts", "tsx", "js", "jsx", "json", "node"].

We set allowJs to true to allow JavaScript code to run including modules.

And we set transform to { "\\.ts$": ["ts-jest"] } to transform them with ts-jest so they can be run with Jest.

Categories
TypeScript Answers

How to loop through an array in TypeScript?

To loop through an array in TypeScript, we can use a for-of loop.

For instance, we write

for (const product of products) {
  console.log(product.productDesc);
}

to loop through the products array with a for-of loop.

In it, we log the productDesc property of the products object entry being looped through, which is stored in product.

Categories
Angular Answers

How to make button disabled in Angular?

To make button disabled in Angular, we set the [disabled] attribute.

For instance, we write

<button [disabled]="!editmode ? 'disabled' : null" (click)="loadChart()">
  <div class="btn-primary">Load Chart</div>
</button>

to set the [disabled] attribute to 'disabled' if editmode is false.

Otherwise, we set it to null to keep the button enabled.

Categories
TypeScript Answers

How to import image with TypeScript?

To import image with TypeScript, we can use declare to declare the type for image files.

For instance, we write

declare module "*.png" {
  const value: any;
  export = value;
}

to declare the type for modules with the .png extension.

We just allow any property to be exported with

const value: any;
export = value;

And then we can import .png files without errors since png files are recognized by the TypeScript compiler after we added the module declaration.