-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.ts
More file actions
279 lines (257 loc) · 9.78 KB
/
index.ts
File metadata and controls
279 lines (257 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { getQueriesForElement, prettyDOM } from "@testing-library/dom";
import {
Accessor,
createComponent,
createRoot,
createSignal,
getOwner,
lazy,
catchError,
onMount,
Owner,
runWithOwner
} from "solid-js";
import { hydrate as solidHydrate, render as solidRender } from "solid-js/web";
import type {
Ui,
Result,
Options,
Ref,
RenderHookResult,
RenderHookOptions,
RenderDirectiveOptions,
RenderDirectiveResult
} from "./types";
/* istanbul ignore next */
if (typeof process === 'undefined' || !process.env.STL_SKIP_AUTO_CLEANUP) {
//@ts-ignore
if (typeof afterEach === "function") { afterEach(cleanup); }
}
const mountedContainers = new Set<Ref>();
/**
* Renders a component to test it
* @param ui {Ui} a function calling the component
* @param options {Options} test options
* @returns {Result} references and tools to test the component
*
* ```ts
* const { getByText } = render(() => <App />, { wrapper: I18nProvider });
* const button = getByText('Accept');
* ```
* ### Options
* - `options.container` - the HTML element which the UI will be rendered into; otherwise a `<div>` will be created
* - `options.baseElement` - the parent of the container, the default will be `<body>`
* - `options.queries` - custom queries (see https://testing-library.com/docs/queries/about)
* - `options.hydrate` - `true` if you want to test hydration
* - `options.wrapper` - a component that applies a context provider and returns `props.children`
* - `options.location` - wraps the component in a solid-router with memory integration pointing at the given path
*
* ### Result
* - `result.asFragment()` - returns the HTML fragment as string
* - `result.container` - the container in which the component is rendered
* - `result.baseElement` - the parent of the component
* - `result.debug()` - returns helpful debug output on the console
* - `result.unmount()` - unmounts the component, usually automatically called in cleanup
* - `result.`[queries] - testing library queries, see https://testing-library.com/docs/queries/about)
*/
function render(ui: Ui, options: Options = {}): Result {
let { container, baseElement = container, queries, hydrate = false, wrapper, location } = options;
if (!baseElement) {
// Default to document.body instead of documentElement to avoid output of potentially-large
// head elements (such as JSS style blocks) in debug output.
baseElement = document.body;
}
if (!container) {
container = baseElement.appendChild(document.createElement("div"));
}
const wrappedUi: Ui =
typeof wrapper === "function"
? () =>
createComponent(wrapper!, {
get children() {
return createComponent(ui, {});
}
})
: ui;
const routedUi: Ui =
typeof location === "string"
? lazy(async () => {
try {
const { createMemoryHistory, MemoryRouter } = await import("@solidjs/router");
const history = createMemoryHistory();
location && history.set({ value: location, scroll: false, replace: true });
return {
default: () =>
createComponent(MemoryRouter, {
history,
get children() { return createComponent(wrappedUi, {}); }
})
};
} catch (e: unknown) {
console.error(
`Error attempting to initialize @solidjs/router:\n"${
(e instanceof Error && e.message) || e?.toString() || "unknown error"
}"`
);
return { default: () => createComponent(wrappedUi, {}) };
}
})
: wrappedUi;
const dispose = hydrate
? (solidHydrate(routedUi, container) as unknown as () => void)
: solidRender(routedUi, container);
// We'll add it to the mounted containers regardless of whether it's actually
// added to document.body so the cleanup method works regardless of whether
// they're passing us a custom container or not.
mountedContainers.add({ container, dispose });
const queryHelpers = getQueriesForElement(container, queries);
return {
asFragment: () => container?.innerHTML as string,
container,
baseElement,
debug: (el = baseElement, maxLength, options) =>
Array.isArray(el)
? el.forEach(e => console.log(prettyDOM(e, maxLength, options)))
: console.log(prettyDOM(el, maxLength, options)),
unmount: dispose,
...queryHelpers
} as Result;
}
/**
* "Renders" a hook to test it
* @param hook {() => unknown)} a hook or primitive
* @param options {RenderHookOptions} test options
* @returns {RenderHookResult} references and tools to test the hook/primitive
*
* ```ts
* const { result } = renderHook(useI18n, { wrapper: I18nProvider });
* expect(result.t('test')).toBe('works');
* ```
* ### Options
* - `options.initialProps` - an array with the props that the hook will be provided with.
* - `options.wrapper` - a component that applies a context provider and **always** returns `props.children`
*
* ### Result
* - `result.result` - the return value of the hook/primitive
* - `result.owner` - the reactive owner in which the hook is run (in order to run other reactive code in the same context with [`runWithOwner`](https://www.solidjs.com/docs/latest/api#runwithowner))
* - `result.cleanup()` - calls the cleanup function of the hook/primitive
*/
export function renderHook<A extends any[], R>(
hook: (...args: A) => R,
options?: RenderHookOptions<A>
): RenderHookResult<R> {
const initialProps: A | [] = Array.isArray(options) ? options : options?.initialProps || [];
const [dispose, owner, result] = createRoot(dispose => {
if (
typeof options === "object" &&
"wrapper" in options &&
typeof options.wrapper === "function"
) {
let result: ReturnType<typeof hook>;
options.wrapper({
get children() {
return createComponent(() => {
result = hook(...(initialProps as A));
return null;
}, {});
}
});
return [dispose, getOwner(), result!];
}
return [dispose, getOwner(), hook(...(initialProps as A))];
});
mountedContainers.add({ dispose });
return { result, cleanup: dispose, owner };
}
/**
* Applies a directive to a test container
* @param directive {(ref, value: () => unknown)} a reusable custom directive
* @param options {RenderDirectiveOptions} test options
* @returns {RenderDirectiveResult} references and tools to test the directive
*
* ```ts
* const called = vi.fn()
* const { getByText, baseContainer } = renderDirective(onClickOutside, { initialValue: called });
* expect(called).not.toBeCalled();
* fireEvent.click(baseContainer);
* expect(called).toBeCalled();
* ```
* ### Options
* - `options.initialValue` - a value added to the directive
* - `options.targetElement` - the name of a HTML element as a string or a HTMLElement or a function returning a HTMLElement
* - `options.container` - the HTML element which the UI will be rendered into; otherwise a `<div>` will be created
* - `options.baseElement` - the parent of the container, the default will be `<body>`
* - `options.queries` - custom queries (see https://testing-library.com/docs/queries/about)
* - `options.hydrate` - `true` if you want to test hydration
* - `options.wrapper` - a component that applies a context provider and returns `props.children`
*
* ### Result
* - `result.arg()` - the accessor for the value that the directive receives
* - `result.setArg()` - the setter for the value that the directive receives
* - `result.asFragment()` - returns the HTML fragment as string
* - `result.container` - the container in which the component is rendered
* - `result.baseElement` - the parent of the component
* - `result.debug()` - returns helpful debug output on the console
* - `result.unmount()` - unmounts the component, usually automatically called in cleanup
* - `result.`[queries] - testing library queries, see https://testing-library.com/docs/queries/about)
*/
export function renderDirective<A extends any, U extends A, E extends HTMLElement>(
directive: (ref: E, arg: Accessor<U>) => void,
options?: RenderDirectiveOptions<U, E>
): RenderDirectiveResult<U> {
const [arg, setArg] = createSignal(options?.initialValue as U);
return Object.assign(
render(() => {
const targetElement =
(options?.targetElement &&
(options.targetElement instanceof HTMLElement
? options.targetElement
: typeof options.targetElement === "string"
? document.createElement(options.targetElement)
: typeof options.targetElement === "function"
? options.targetElement()
: undefined)) ||
document.createElement("div");
onMount(() => directive(targetElement as E, arg as Accessor<U>));
return targetElement;
}, options),
{ arg, setArg }
);
}
export function testEffect<T extends any = void>(
fn: (done: (result: T) => void) => void,
owner?: Owner
): Promise<T> {
let done: (result: T) => void;
let fail: (error: any) => void;
let promise = new Promise<T>((resolve, reject) => {
done = resolve;
fail = reject;
});
createRoot(dispose => {
catchError(() => {
fn(result => {
done(result);
dispose();
});
}, fail)
}, owner);
return promise
}
function cleanupAtContainer(ref: Ref) {
const { container, dispose } = ref;
if (typeof dispose === 'function') {
dispose();
} else {
console.warn('solid-testing-library: dispose is not a function - maybe your tests include multiple solid versions!');
}
if (container?.parentNode === document.body) {
document.body.removeChild(container);
}
mountedContainers.delete(ref);
}
function cleanup() {
mountedContainers.forEach(cleanupAtContainer);
}
export * from "@testing-library/dom";
export { render, cleanup };