# `dom-event-testing-library`
A library for unit testing events via high-level interactions, e.g., `pointerdown`,
that produce realistic and complete DOM event sequences.There are number of challenges involved in unit testing modules that work withDOM events.1. Gesture recognizers may need to support environments with and without support forthe `PointerEvent` API.
2. Gesture recognizers may need to support various user interaction modes includingmouse, touch, and pen use.3. Gesture recognizers must account for the actual event sequences browsers produce(e.g., emulated touch and mouse events.)4. Gesture recognizers must work with "virtual" events produced by tools likescreen-readers.Writing unit tests to cover all these scenarios is tedious and error prone. Thisevent testing library is designed to solve these issues by allowing developers tomore easily dispatch events in unit tests, and to more reliably test pointerinteractions using a high-level API based on `PointerEvent`. Here's a basic example:
```js
import {describeWithPointerEvent,testWithPointerType,createEventTarget,setPointerEvent,resetActivePointers} from 'dom-event-testing-library';describeWithPointerEvent('useTap', hasPointerEvent => {beforeEach(() => {// basic PointerEvent mocksetPointerEvent(hasPointerEvent);});afterEach(() => {// clear active pointers between test runsresetActivePointers();});// test all the pointer types supported by the environmenttestWithPointerType('pointer down', pointerType => {const ref = createRef(null);const onTapStart = jest.fn();render(() => {useTap(ref, { onTapStart });return <div ref={ref} />});// create an event targetconst target = createEventTarget(ref.current);// dispatch high-level pointer eventtarget.pointerdown({ pointerType });expect(onTapStart).toBeCalled();});});```This tests the interaction in multiple scenarios. In each case, a realistic DOMevent sequence–with complete mock events–is produced. When running in a mockenvironment without the `PointerEvent` API, the test runs for both `mouse` and
`touch` pointer types. When `touch` is the pointer type it produces emulated mouse
events. When running in a mock environment with the `PointerEvent` API, the test
runs for `mouse`, `touch`, and `pen` pointer types.
It's important to cover all these scenarios because it's very easy to introducebugs – e.g., double calling of callbacks – if not accounting for emulated mouseevents, differences in target capturing between `touch` and `mouse` pointers, and
the different semantics of `button` across event APIs.
Default values are provided for the expected native events properties. They canalso be customized as needed in a test.```js
target.pointerdown({button: 0,buttons: 1,pageX: 10,pageY: 10,pointerType,// NOTE: use x,y instead of clientX,clientYx: 10,y: 10});```Tests that dispatch multiple pointer events will dispatch multi-touch native eventson the target.```js
// first pointer is activetarget.pointerdown({pointerId: 1, pointerType});// second pointer is activetarget.pointerdown({pointerId: 2, pointerType});```