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

https://github.com/nashaddams/ddom

Testing utilities for React components and vanilla HTML/JS
https://github.com/nashaddams/ddom

deno jsdom react react-testing-library

Last synced: about 2 months ago
JSON representation

Testing utilities for React components and vanilla HTML/JS

Awesome Lists containing this project

README

          

# DDOM

[![JSR](https://jsr.io/badges/@nashaddams/ddom)](https://jsr.io/@nashaddams/ddom)
[![JSR score](https://jsr.io/badges/@nashaddams/ddom/score)](https://jsr.io/@nashaddams/ddom)
[![main](https://github.com/nashaddams/ddom/actions/workflows/tests.yml/badge.svg)](https://github.com/nashaddams/ddom/actions)

Testing utilities for React components and vanilla HTML/JS, powered by
[`JSDOM`](https://github.com/jsdom/jsdom).

## Usage

```tsx
import { useState } from "react";

export function Counter({ initialCount }: { initialCount: number }) {
const [count, setCount] = useState(initialCount);
const increment = () => setCount((prev) => prev + 1);

return (


Current value: 
{count}

Increment


);
}
```

```tsx
import { DDOM, getByTestId, waitFor } from "@nashaddams/ddom";
import { createRoot } from "react-dom/client";
import { Counter } from "./counter.tsx";

Deno.test("should render and interact with a react component", async () => {
using _ = DDOM.register();

const root = document.getElementById("root")!;
assertEquals(root.children.length, 0);

createRoot(root).render();
await waitFor(() => assertGreater(root.children.length, 0));

const count = getByTestId("count");

getByTestId("increment").click();
await waitFor(() => assertEquals(count.textContent, "4712"));
});
```

### `JSDOM` only

If you prefer to only add [`JSDOM`](https://github.com/jsdom/jsdom) as a
dependency, you can register the DOM globals manually:

```ts
// @ts-types="npm:@types/jsdom@27.0.0"
import { JSDOM } from "npm:jsdom@27.0.0";

const dom = new JSDOM(``);

(globalThis as any).window = dom.window;
(globalThis as any).document = dom.window.document;
```

See [the docs](https://jsr.io/@nashaddams/ddom/doc) for further details.