An open API service indexing awesome lists of open source software.

https://github.com/localvoid/jest-hooks-dom


https://github.com/localvoid/jest-hooks-dom

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          



`jest-hooks-dom` is a collection of [hooks](https://github.com/localvoid/jest-hooks) for DOM testing with
[Jest](https://jestjs.io/) library.

## Hooks

### Elements

`useHTMLElement(element, container)` creates a new HTML element before each test and optionally mounts it to the
container. After each test HTML element is removed from container.

`useSVGElement(element, container)` creates a new SVG element before each test and optionally mounts it to the
container. After each test SVG element is removed from container.

`useHTMLTemplate(html, container)` create a new HTML from template before each test and optionally mounts it to the
container. After each test all root nodes are removed from container.

```js
const t = useHTMLTemplate(`


A
B

`);

describe("template", () => {
it("should find all nodes by id", () => {
expect(t.a.textContent).toBe("A");
expect(t.b.textContent).toBe("B");
});
});
```

### Mutation Tracking

`useDOMMutationTracker()` tracks DOM mutations.

Methods and properties tracked:

- `Document.prototype.createElement`
- `Document.prototype.createElementNS`
- `Document.prototype.createTextNode`
- `Node.prototype.appendChild`
- `Node.prototype.insertBefore`
- `Node.prototype.replaceChild`
- `Node.prototype.removeChild`
- `Node.prototype.textContent`
- `Node.prototype.nodeValue`
- `Element.prototype.innerHTML`

```js
import { useModule } from "jest-hooks";
import { useResetDOM, useHTMLElement, useDOMMutationTracker } from "jest-hooks-dom";

useResetDOM();
const root = useHTMLElement();
const mutations = useDOMMutationTracker();
const ivi = useModule("ivi");
const r = (op: Op) => ivi.render(op, root());

describe("fragment", () => {
describe("mount", () => {
test("[]", () => {
r([]);
expect(mutations.stats).toMatchSnapshot();
});
});
});
```

### Events

`useResetDOMEventListeners()` tracks all unregistered event listeners and automatically removes them after each test.

### requestAnimationFrame

`useRequestAnimationFrame()` mocks `requestAnimationFrame()` and `cancelAnimationFrame()`, and resets all tasks after
each test.