Skip to content

Commit a064d03

Browse files
committed
first commit
0 parents  commit a064d03

18 files changed

Lines changed: 7393 additions & 0 deletions

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
["@babel/preset-env", { "targets": { "node": "current" } }],
4+
"solid",
5+
"@babel/preset-typescript"
6+
]
7+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": false,
6+
"arrowParens": "avoid",
7+
"printWidth": 100
8+
}

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<div align="center">
2+
<h1>Solid Testing Library</h1>
3+
4+
<p>Simple and complete Solid DOM testing utilities that encourage good testing
5+
practices.</p>
6+
7+
> Inspired completely by [preact-testing-library][preact-testing-library]
8+
9+
</div>
10+
11+
<hr />
12+
13+
## Table of Contents
14+
15+
- [The Problem](#the-problem)
16+
- [The Solution](#the-solution)
17+
- [Installation](#installation)
18+
- [Docs](#docs)
19+
20+
## The Problem
21+
22+
You want to write tests for your Solid components so that they avoid including implementation
23+
details, and are maintainable in the long run.
24+
25+
## The Solution
26+
27+
The Solid Testing Library is a very lightweight solution for testing Solid components. Its primary guiding principle is:
28+
29+
> [The more your tests resemble the way your software is used, the more confidence they can give you.](https://twitter.com/kentcdodds/status/977018512689455106)
30+
31+
## Installation
32+
33+
This module is distributed via npm which is bundled with node and should be installed
34+
as one of your project's `devDependencies`:
35+
36+
```
37+
npm install --save-dev solid-testing-library
38+
```
39+
40+
💡 You may also be interested in installing `@testing-library/jest-dom` so you can use
41+
[the custom jest matchers](https://github.com/testing-library/jest-dom).
42+
43+
## Docs
44+
45+
TODO
46+
<!-- See the [docs](https://testing-library.com/docs/preact-testing-library/intro) over at the Testing
47+
Library website. -->

dist/index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Queries, BoundFunction } from "@testing-library/dom";
2+
import { JSX } from "solid-js";
3+
import type { OptionsReceived } from "pretty-format";
4+
declare function render(ui: () => JSX.Element, { container, baseElement, queries, hydrate }?: {
5+
container?: HTMLElement;
6+
baseElement?: HTMLElement;
7+
queries?: Queries;
8+
hydrate?: boolean;
9+
}): {
10+
container: HTMLElement;
11+
baseElement: HTMLElement;
12+
debug: (baseElement?: HTMLElement | HTMLElement[], maxLength?: number, options?: OptionsReceived) => void;
13+
unmount: () => void;
14+
asFragment: () => DocumentFragment;
15+
} & {
16+
[P in keyof Queries]: BoundFunction<Queries[P]>;
17+
};
18+
declare function cleanup(): void;
19+
export * from "@testing-library/dom";
20+
export { render, cleanup };

dist/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { getQueriesForElement, prettyDOM } from "@testing-library/dom";
2+
import { hydrate as solidHydrate, render as solidRender } from "solid-js/web";
3+
if (!process.env.STL_SKIP_AUTO_CLEANUP) {
4+
if (typeof afterEach === 'function') {
5+
afterEach(async () => {
6+
await cleanup();
7+
});
8+
// @ts-ignore
9+
}
10+
else if (typeof teardown === 'function') {
11+
// @ts-ignore
12+
teardown(async () => {
13+
await cleanup();
14+
});
15+
}
16+
}
17+
const mountedContainers = new Set();
18+
function render(ui, { container, baseElement = container, queries, hydrate = false } = {}) {
19+
if (!baseElement) {
20+
// Default to document.body instead of documentElement to avoid output of potentially-large
21+
// head elements (such as JSS style blocks) in debug output.
22+
baseElement = document.body;
23+
}
24+
if (!container) {
25+
container = baseElement.appendChild(document.createElement("div"));
26+
}
27+
const dispose = hydrate
28+
? solidHydrate(ui, container)
29+
: solidRender(ui, container);
30+
// We'll add it to the mounted containers regardless of whether it's actually
31+
// added to document.body so the cleanup method works regardless of whether
32+
// they're passing us a custom container or not.
33+
mountedContainers.add({ container, dispose });
34+
return {
35+
container,
36+
baseElement,
37+
debug: (el = baseElement, maxLength, options) => Array.isArray(el)
38+
? el.forEach(e => console.log(prettyDOM(e, maxLength, options)))
39+
: console.log(prettyDOM(el, maxLength, options)),
40+
unmount: dispose,
41+
asFragment: () => {
42+
if (typeof document.createRange === "function") {
43+
return document.createRange().createContextualFragment(container.innerHTML);
44+
}
45+
else {
46+
const template = document.createElement("template");
47+
template.innerHTML = container.innerHTML;
48+
return template.content;
49+
}
50+
},
51+
...getQueriesForElement(baseElement, queries)
52+
};
53+
}
54+
function cleanupAtContainer(ref) {
55+
const { container, dispose } = ref;
56+
dispose();
57+
if (container.parentNode === document.body) {
58+
document.body.removeChild(container);
59+
}
60+
mountedContainers.delete(ref);
61+
}
62+
function cleanup() {
63+
mountedContainers.forEach(cleanupAtContainer);
64+
}
65+
export * from "@testing-library/dom";
66+
export { render, cleanup };

0 commit comments

Comments
 (0)