Skip to content

Commit 2fe539a

Browse files
author
Ben Monro
committed
feat: support events & cleanup
1 parent f0ac94b commit 2fe539a

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
import { getQueriesForElement } from "dom-testing-library";
22

3+
export * from "dom-testing-library";
4+
let mountedContainers = new Set();
35
export const render = (Component, props) => {
4-
const rendered = new Component({
5-
target: document.body,
6+
const container = document.body.appendChild(document.createElement("div"));
7+
8+
let rendered = new Component({
9+
target: container,
610
props
711
});
812

13+
mountedContainers.add(rendered);
914
return {
1015
...getQueriesForElement(document.body)
1116
};
1217
};
18+
export const cleanup = () => {
19+
mountedContainers.forEach(cleanupAtContainer);
20+
};
21+
22+
const cleanupAtContainer = container => {
23+
container.$destroy();
24+
mountedContainers.delete(container);
25+
};

tests/example/App.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<script>
22
export let name;
3+
4+
let buttonText = "Button Text";
5+
6+
function handleClick() {
7+
buttonText = "Button Clicked";
8+
}
39
</script>
410

511
<style>
@@ -9,3 +15,5 @@
915
</style>
1016

1117
<h1>Hello {name}!</h1>
18+
19+
<button on:click={handleClick}>{buttonText}</button>

tests/queries.spec.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import App from "./example/App.svelte";
2-
import { render } from "../src";
3-
describe("App", () => {
4-
test("should render", () => {
2+
import { render, fireEvent, waitForElement, cleanup } from "../src";
3+
4+
afterEach(cleanup);
5+
describe("queries", () => {
6+
test("getByText", () => {
57
const { getByText } = render(App, { name: "world" });
68

79
expect(getByText("Hello world!"));
810
});
11+
12+
test("click button", async () => {
13+
const { getByText } = render(App, { name: "world" });
14+
15+
fireEvent.click(getByText("Button Text"));
16+
17+
const button = await waitForElement(() => getByText("Button Clicked"));
18+
19+
expect(button);
20+
});
921
});

0 commit comments

Comments
 (0)