| 1 |
import { JSDOM } from 'jsdom'; |
| 2 |
import { tick } from 'svelte'; |
| 3 |
|
| 4 |
const { window } = new JSDOM(''); |
| 5 |
|
| 6 |
export function setup() { |
| 7 |
// @ts-ignore |
| 8 |
global.window = window; |
| 9 |
global.document = window.document; |
| 10 |
global.navigator = window.navigator; |
| 11 |
global.getComputedStyle = window.getComputedStyle; |
| 12 |
global.requestAnimationFrame = null; |
| 13 |
} |
| 14 |
|
| 15 |
export function reset() { |
| 16 |
window.document.title = ''; |
| 17 |
window.document.head.innerHTML = ''; |
| 18 |
window.document.body.innerHTML = ''; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @typedef RenderOutput |
| 23 |
* @property container {HTMLElement} |
| 24 |
* @property component {import('svelte').SvelteComponent} |
| 25 |
*/ |
| 26 |
|
| 27 |
/** |
| 28 |
* @return {RenderOutput} |
| 29 |
*/ |
| 30 |
export function render(Tag, props = {}) { |
| 31 |
Tag = Tag.default || Tag; |
| 32 |
const container = window.document.body; |
| 33 |
const component = new Tag({ props, target: container }); |
| 34 |
return { container, component }; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param {HTMLElement} elem |
| 39 |
* @param {String} event |
| 40 |
* @param {any} [details] |
| 41 |
* @returns Promise<void> |
| 42 |
*/ |
| 43 |
export function fire(elem, event, details) { |
| 44 |
let evt = new window.Event(event, details); |
| 45 |
elem.dispatchEvent(evt); |
| 46 |
return tick(); |
| 47 |
} |