test-event-listeners
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

test-event-listeners

Build Status npm version Downloads

Test-friendly object creator to simulate adding listeners for events. Allows you to add event listeners on an object and fire them at will. Great for simulating window or document events during tests!

No dependencies. Tiny size. Blazingly fast. Easy breezy.

Usage

import { createRegister } from "test-event-listeners";
 
const register = createRegister();
 
register.addEventListener("foo", (data) => console.log(`Got ${data}!`));
 
// Got bar!
register.fireEvent("foo", "bar");

This can be useful if, for example, you're manipulating a window's "click" and "keydown" events with Jest spies:

import { createRegister } from "test-event-listeners";
 
// Arrange
const register = createRegister();
const keyDownSpy = jest.fn();
 
register.addEventListener("keydown", keyDownSpy)
 
// Act
register.fireEvent("keydown");
 
// Assert
expect(keyDownSpy).toHaveBeenCalledTimes(1);

Usage with TypeScript

Good news: test-event-listeners is written in TypeScript! You'll never have to worry about @types mismatches here!

createRegister takes two templated types:

  1. TEventName extends string: Event names that may be fired.
  2. TListener extends Function: Type of functions stored as listeners.
const register = createRegister<"keydown", jasmine.Spy>();

Package Sidebar

Install

npm i test-event-listeners

Weekly Downloads

3

Version

0.1.2

License

MIT

Unpacked Size

9.23 kB

Total Files

11

Last publish

Collaborators

  • joshuakgoldberg