Skip to content

Commit f55db35

Browse files
committed
Extract types to seperate file and clean up the code
1 parent 1d28e0a commit f55db35

2 files changed

Lines changed: 47 additions & 32 deletions

File tree

src/index.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,31 @@
1-
import { getQueriesForElement, prettyDOM, Queries, BoundFunction } from "@testing-library/dom";
2-
import { JSX } from "solid-js";
1+
import { getQueriesForElement, prettyDOM } from "@testing-library/dom";
32
import { hydrate as solidHydrate, render as solidRender } from "solid-js/web";
4-
import type { OptionsReceived } from "pretty-format";
3+
4+
import type { Ui, Result, Options, Ref } from "./types";
5+
6+
/**
7+
* Shim for the teardown function
8+
*/
9+
declare var teardown: Function | undefined;
510

611
/* istanbul ignore next */
712
if (!process.env.STL_SKIP_AUTO_CLEANUP) {
813
if (typeof afterEach === "function") {
914
afterEach(async () => {
1015
await cleanup();
1116
});
12-
// @ts-ignore
1317
} else if (typeof teardown === "function") {
14-
// @ts-ignore
1518
teardown(async () => {
1619
await cleanup();
1720
});
1821
}
1922
}
2023

21-
const mountedContainers = new Set<{ container: HTMLElement; dispose: () => void }>();
24+
const mountedContainers = new Set<Ref>();
25+
26+
function render(ui: Ui, options: Options = {}): Result {
27+
let { container, baseElement = container, queries, hydrate = false } = options;
2228

23-
function render(
24-
ui: () => JSX.Element,
25-
{
26-
container,
27-
baseElement = container,
28-
queries,
29-
hydrate = false
30-
}: {
31-
container?: HTMLElement;
32-
baseElement?: HTMLElement;
33-
queries?: Queries;
34-
hydrate?: boolean;
35-
} = {}
36-
): {
37-
container: HTMLElement;
38-
baseElement: HTMLElement;
39-
debug: (
40-
baseElement?: HTMLElement | HTMLElement[],
41-
maxLength?: number,
42-
options?: OptionsReceived
43-
) => void;
44-
unmount: () => void;
45-
} & { [P in keyof Queries]: BoundFunction<Queries[P]> } {
4629
if (!baseElement) {
4730
// Default to document.body instead of documentElement to avoid output of potentially-large
4831
// head elements (such as JSS style blocks) in debug output.
@@ -65,16 +48,16 @@ function render(
6548
return {
6649
container,
6750
baseElement,
68-
debug: (el = baseElement, maxLength?: number, options?: OptionsReceived) =>
51+
debug: (el = baseElement, maxLength, options) =>
6952
Array.isArray(el)
7053
? el.forEach(e => console.log(prettyDOM(e, maxLength, options)))
7154
: console.log(prettyDOM(el, maxLength, options)),
7255
unmount: dispose,
7356
...getQueriesForElement(baseElement, queries)
74-
} as any;
57+
} as Result;
7558
}
7659

77-
function cleanupAtContainer(ref: { container: HTMLElement; dispose: () => void }) {
60+
function cleanupAtContainer(ref: Ref) {
7861
const { container, dispose } = ref;
7962
dispose();
8063

src/types.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { JSX } from "solid-js";
2+
import type { OptionsReceived } from "pretty-format";
3+
import type { Queries, BoundFunction } from "@testing-library/dom";
4+
5+
export interface Ref {
6+
container: HTMLElement;
7+
dispose: () => void;
8+
}
9+
10+
export type Ui = () => JSX.Element;
11+
12+
export interface Options {
13+
container?: HTMLElement;
14+
baseElement?: HTMLElement;
15+
queries?: Queries;
16+
hydrate?: boolean;
17+
}
18+
19+
export type Extra = { [P in keyof Queries]: BoundFunction<Queries[P]> };
20+
21+
export type DebugFn = (
22+
baseElement?: HTMLElement | HTMLElement[],
23+
maxLength?: number,
24+
options?: OptionsReceived
25+
) => void;
26+
27+
export type Result = {
28+
container: HTMLElement;
29+
baseElement: HTMLElement;
30+
debug: DebugFn;
31+
unmount: () => void;
32+
} & Extra;

0 commit comments

Comments
 (0)