Last active
October 20, 2021 13:30
-
-
Save dirv/838d6ec201d21af378154357c0e033d7 to your computer and use it in GitHub Desktop.
Svelte testing harness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JSDOM } from "jsdom"; | |
import { bind, binding_callbacks } from "svelte/internal"; | |
export const setDomDocument = () => { | |
const dom = new JSDOM( | |
"<html><body></body></html>", | |
{ url: "https://localhost" }); | |
global.document = dom.window.document; | |
global.window = { ...global.window, ...dom.window }; | |
global.navigator = dom.window.navigator; | |
global.container = document.createElement("div"); | |
global.document.body.appendChild(global.container); | |
}; | |
export const click = formElement => { | |
const evt = document.createEvent("MouseEvents"); | |
evt.initEvent("click", true, true); | |
formElement.dispatchEvent(evt); | |
return evt; | |
}; | |
let mountedComponents = []; | |
export const mountComponent = (component, props = {}, bindings = {}) => { | |
const mounted = new component({ target: global.container, props }); | |
Object.keys(bindings).forEach(binding => { | |
binding_callbacks.push(() => { | |
bind(mounted, binding, value => { | |
bindings[binding] = value | |
}); | |
}); | |
}); | |
mountedComponents = [ mounted, ...mountedComponents ]; | |
return mounted; | |
}; | |
export const unmountAll = () => { | |
mountedComponents.forEach(component => { | |
component.$destroy() | |
}); | |
mountedComponents = []; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment