mirror of https://github.com/sveltejs/svelte
parent
146e7a6310
commit
befbe0ac89
@ -0,0 +1,10 @@
|
|||||||
|
<script>
|
||||||
|
import { onDestroy } from "svelte"
|
||||||
|
onDestroy(() => {
|
||||||
|
const el = document.querySelector('#show')
|
||||||
|
console.log(`Element exist ${!!el}`)
|
||||||
|
console.log("Custom component destroyed.")
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<p>Hello</p>
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
let log;
|
||||||
|
export default {
|
||||||
|
html: `
|
||||||
|
<button>Show/Hide</button><div id="show"><p>Hello</p></div>
|
||||||
|
`,
|
||||||
|
|
||||||
|
before_test() {
|
||||||
|
log = console.log;
|
||||||
|
},
|
||||||
|
after_test() {
|
||||||
|
console.log = log;
|
||||||
|
},
|
||||||
|
|
||||||
|
async test({ assert, target, window }) {
|
||||||
|
const button = target.querySelector('button');
|
||||||
|
const event = new window.MouseEvent('click');
|
||||||
|
const messages = [];
|
||||||
|
console.log = msg => messages.push(msg);
|
||||||
|
await button.dispatchEvent(event);
|
||||||
|
//This means element gets removed
|
||||||
|
assert.htmlEqual(target.innerHTML, `
|
||||||
|
<button>Show/Hide</button>
|
||||||
|
`);
|
||||||
|
//This means element existed on Destroy gets called
|
||||||
|
assert.deepEqual(messages, ['Element exist true', 'Custom component destroyed.']);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
<script>
|
||||||
|
import CustomComponent from './CustomComponent.svelte'
|
||||||
|
let div;
|
||||||
|
let show = true
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button on:click={() => {
|
||||||
|
show = !show
|
||||||
|
}}>Show/Hide</button>
|
||||||
|
|
||||||
|
{#if show}
|
||||||
|
<div id="show">
|
||||||
|
<CustomComponent />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
Loading…
Reference in new issue