-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpure.js
134 lines (119 loc) · 3.77 KB
/
pure.js
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
import {
fireEvent as baseFireEvent,
getQueriesForElement,
prettyDOM,
} from '@testing-library/dom'
import { tick } from 'svelte'
import { mount, prepare } from './core/index.js'
/**
* Customize how Svelte renders the component.
*
* @template {import('./core/types.js').Component} C
* @typedef {import('./core/types.js').PropsOrMountOptions<C>} SvelteComponentOptions
*/
/**
* Customize how Testing Library sets up the document and binds queries.
*
* @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries]
* @typedef {{
* baseElement?: HTMLElement
* queries?: Q
* }} RenderOptions
*/
/**
* The rendered component and bound testing functions.
*
* @template {import('./core/types.js').Component} C
* @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries]
*
* @typedef {{
* container: HTMLElement
* baseElement: HTMLElement
* component: import('./core/types.js').Exports<C>
* debug: (el?: HTMLElement | DocumentFragment) => void
* rerender: (props: Partial<import('./core/types.js').Props<C>>) => Promise<void>
* unmount: () => void
* } & {
* [P in keyof Q]: import('@testing-library/dom').BoundFunction<Q[P]>
* }} RenderResult
*/
/**
* Render a component into the document.
*
* @template {import('./core/types.js').Component} C
* @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries]
*
* @param {import('./core/types.js').ComponentType<C>} Component - The component to render.
* @param {SvelteComponentOptions<C>} propsOrOptions - Customize how Svelte renders the component.
* @param {RenderOptions<Q>} renderOptions - Customize how Testing Library sets up the document and binds queries.
* @returns {RenderResult<C, Q>} The rendered component and bound testing functions.
*/
const render = (Component, propsOrOptions = {}, renderOptions = {}) => {
const { baseElement, target, mountOptions } = prepare(
propsOrOptions,
renderOptions
)
const { component, unmount, rerender } = mount(
Component.default ?? Component,
mountOptions
)
const queries = getQueriesForElement(baseElement, renderOptions.queries)
return {
baseElement,
component,
container: target,
debug: (el = baseElement) => console.log(prettyDOM(el)),
rerender: async (props) => {
if (props.props) {
console.warn(
'rerender({ props: {...} }) deprecated, use rerender({...}) instead'
)
props = props.props
}
await rerender(props)
},
unmount,
...queries,
}
}
/**
* Call a function and wait for Svelte to flush pending changes.
*
* @param {() => unknown} [fn] - A function, which may be `async`, to call before flushing updates.
* @returns {Promise<void>}
*/
const act = async (fn) => {
if (fn) {
await fn()
}
return tick()
}
/**
* @typedef {(...args: Parameters<import('@testing-library/dom').FireFunction>) => Promise<ReturnType<import('@testing-library/dom').FireFunction>>} FireFunction
*/
/**
* @typedef {{
* [K in import('@testing-library/dom').EventType]: (...args: Parameters<import('@testing-library/dom').FireObject[K]>) => Promise<ReturnType<import('@testing-library/dom').FireObject[K]>>
* }} FireObject
*/
/**
* Fire an event on an element.
*
* Consider using `@testing-library/user-event` instead, if possible.
* @see https://testing-library.com/docs/user-event/intro/
*
* @type {FireFunction & FireObject}
*/
const fireEvent = async (...args) => {
const event = baseFireEvent(...args)
await tick()
return event
}
Object.keys(baseFireEvent).forEach((key) => {
fireEvent[key] = async (...args) => {
const event = baseFireEvent[key](...args)
await tick()
return event
}
})
export { act, fireEvent, render }