mirror of https://github.com/sveltejs/svelte
parent
a73a619714
commit
48adf81220
@ -0,0 +1,11 @@
|
|||||||
|
<script>
|
||||||
|
let { checked = $bindable(), count = $bindable() } = $props();
|
||||||
|
|
||||||
|
$effect(() => ()=>{
|
||||||
|
console.log(count, checked);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<p>{count}</p>
|
||||||
|
|
||||||
|
<button onclick={()=> count-- }></button>
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
import { ok, test } from '../../test';
|
||||||
|
import { flushSync } from 'svelte';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target, logs }) {
|
||||||
|
const [btn1, btn2, btn3] = target.querySelectorAll('button');
|
||||||
|
let ps = [...target.querySelectorAll('p')];
|
||||||
|
|
||||||
|
for (const p of ps) {
|
||||||
|
assert.equal(p.innerHTML, '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
btn1.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
// prop update normally if we are not unmounting
|
||||||
|
for (const p of ps) {
|
||||||
|
assert.equal(p.innerHTML, '1');
|
||||||
|
}
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
btn3.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
// binding still works and update the value correctly
|
||||||
|
for (const p of ps) {
|
||||||
|
assert.equal(p.innerHTML, '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
btn1.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
btn1.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
console.warn(logs);
|
||||||
|
|
||||||
|
// the five components guarded by `count < 2` unmount and log
|
||||||
|
assert.deepEqual(logs, [1, true, 1, true, 1, true, 1, true, 1, true]);
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
btn2.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
// the three components guarded by `show` unmount and log
|
||||||
|
assert.deepEqual(logs, [
|
||||||
|
1,
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
true,
|
||||||
|
2,
|
||||||
|
true,
|
||||||
|
2,
|
||||||
|
true,
|
||||||
|
2,
|
||||||
|
true
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
<script>
|
||||||
|
import Component from "./Component.svelte";
|
||||||
|
|
||||||
|
let show = $state(true);
|
||||||
|
let count = $state(0);
|
||||||
|
let spread = $derived({ checked: show, count });
|
||||||
|
|
||||||
|
let Dynamic = $derived(count < 2 ? Component : undefined);
|
||||||
|
let Dynamic2 = $derived(show ? Component : undefined);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={()=> count++ }></button>
|
||||||
|
<button onclick={()=> show = !show }></button>
|
||||||
|
|
||||||
|
<!-- count with bind -->
|
||||||
|
{#if count < 2}
|
||||||
|
<Component bind:count bind:checked={show} />
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- spread syntax -->
|
||||||
|
{#if count < 2}
|
||||||
|
<Component {...spread} />
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- normal prop -->
|
||||||
|
{#if count < 2}
|
||||||
|
<Component {count} checked={show} />
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- prop only accessed in destroy -->
|
||||||
|
{#if show}
|
||||||
|
<Component {count} checked={show} />
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- dynamic component -->
|
||||||
|
<Dynamic {count} checked={show} />
|
||||||
|
|
||||||
|
<!-- dynamic component spread -->
|
||||||
|
<Dynamic {...spread} />
|
||||||
|
|
||||||
|
<!-- dynamic component with prop only accessed on destroy -->
|
||||||
|
<Dynamic2 {count} checked={show} />
|
||||||
|
|
||||||
|
<!-- dynamic component with prop only accessed on destroy spread -->
|
||||||
|
<Dynamic2 {...spread} />
|
||||||
Loading…
Reference in new issue