mirror of https://github.com/sveltejs/svelte
parent
b213de2caf
commit
562c9e3c7b
@ -1,11 +0,0 @@
|
|||||||
<script>
|
|
||||||
import { onDestroy } from 'svelte';
|
|
||||||
|
|
||||||
let { checked = $bindable(), count = $bindable() } = $props();
|
|
||||||
|
|
||||||
onDestroy(() => {
|
|
||||||
console.log(`${count} ${checked}`);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<p>Count: {count}</p>
|
|
@ -1,63 +0,0 @@
|
|||||||
import { flushSync } from 'svelte';
|
|
||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
async test({ assert, logs, target }) {
|
|
||||||
/** @type {HTMLButtonElement | null} */
|
|
||||||
const increment_btn = target.querySelector('#increment');
|
|
||||||
/** @type {HTMLButtonElement | null} */
|
|
||||||
const toggle_btn = target.querySelector('#toggle');
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 0');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Click increment: count=1
|
|
||||||
flushSync(() => {
|
|
||||||
increment_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 1');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Click increment again: count=2, components with count < 2 should unmount and log old values
|
|
||||||
flushSync(() => {
|
|
||||||
increment_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 2');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggle show to hide components that depend on show
|
|
||||||
flushSync(() => {
|
|
||||||
toggle_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Should log old values during cleanup from the six components guarded by `count < 2`:
|
|
||||||
// 1. Component with bind: "1 true"
|
|
||||||
// 2. Component with spread: "1 true"
|
|
||||||
// 3. Component with normal props: "1 true"
|
|
||||||
// 4. Runes dynamic component with bind: "1 true"
|
|
||||||
// 5. Runes dynamic component with spread: "1 true"
|
|
||||||
// 6. Runes dynamic component with normal props: "1 true"
|
|
||||||
// Then from the four components guarded by `show`:
|
|
||||||
// 7. Component with bind (show): "2 true" (old value of checked)
|
|
||||||
// 8. Runes dynamic component with bind (show): "2 true"
|
|
||||||
// 9. Runes dynamic component with spread (show): "2 true"
|
|
||||||
// 10. Runes dynamic component with normal props (show): "2 true"
|
|
||||||
assert.deepEqual(logs, [
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'2 true',
|
|
||||||
'2 true',
|
|
||||||
'2 true',
|
|
||||||
'2 true'
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,52 +0,0 @@
|
|||||||
<script>
|
|
||||||
import Component from './Component.svelte';
|
|
||||||
|
|
||||||
let show = $state(true);
|
|
||||||
let count = $state(0);
|
|
||||||
let spread = $derived({ checked: show, count });
|
|
||||||
|
|
||||||
// Runes dynamic component pattern
|
|
||||||
let DynComponent1 = $derived(count < 2 ? Component : undefined);
|
|
||||||
let DynComponent2 = $derived(show ? Component : undefined);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<button id="increment" onclick={() => count++}>Increment count</button>
|
|
||||||
<button id="toggle" onclick={() => (show = !show)}>Toggle 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 bind:count bind:checked={show} />
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern -->
|
|
||||||
<DynComponent1 bind:count bind:checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with spread -->
|
|
||||||
<DynComponent1 {...spread} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with normal props -->
|
|
||||||
<DynComponent1 {count} checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern (show) -->
|
|
||||||
<DynComponent2 bind:count bind:checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with spread (show) -->
|
|
||||||
<DynComponent2 {...spread} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with normal props (show) -->
|
|
||||||
<DynComponent2 {count} checked={show} />
|
|
@ -1,14 +0,0 @@
|
|||||||
<script>
|
|
||||||
import { onDestroy } from 'svelte';
|
|
||||||
|
|
||||||
let { checked = $bindable(), count = $bindable() } = $props();
|
|
||||||
|
|
||||||
onDestroy(() => {
|
|
||||||
console.log(`${count} ${checked}`);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<p>Count: {count}</p>
|
|
||||||
|
|
||||||
<button onclick={() => count--}>Decrement count</button>
|
|
||||||
<button onclick={() => (checked = !checked)}>Toggle checked</button>
|
|
@ -1,63 +0,0 @@
|
|||||||
import { flushSync } from 'svelte';
|
|
||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
async test({ assert, logs, target }) {
|
|
||||||
/** @type {HTMLButtonElement | null} */
|
|
||||||
const increment_btn = target.querySelector('#increment');
|
|
||||||
/** @type {HTMLButtonElement | null} */
|
|
||||||
const toggle_btn = target.querySelector('#toggle');
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 0');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Click increment: count=1
|
|
||||||
flushSync(() => {
|
|
||||||
increment_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 1');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Click increment again: count=2, components with count < 2 should unmount and log old values
|
|
||||||
flushSync(() => {
|
|
||||||
increment_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 2');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggle show to hide components that depend on show
|
|
||||||
flushSync(() => {
|
|
||||||
toggle_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Should log old values during cleanup from the six components guarded by `count < 2`:
|
|
||||||
// 1. Component with bind: "1 true"
|
|
||||||
// 2. Component with spread: "1 true"
|
|
||||||
// 3. Component with normal props: "1 true"
|
|
||||||
// 4. Runes dynamic component with bind: "1 true"
|
|
||||||
// 5. Runes dynamic component with spread: "1 true"
|
|
||||||
// 6. Runes dynamic component with normal props: "1 true"
|
|
||||||
// Then from the four components guarded by `show`:
|
|
||||||
// 7. Component with bind (show): "2 true" (old value of checked)
|
|
||||||
// 8. Runes dynamic component with bind (show): "2 true"
|
|
||||||
// 9. Runes dynamic component with spread (show): "2 true"
|
|
||||||
// 10. Runes dynamic component with normal props (show): "2 true"
|
|
||||||
assert.deepEqual(logs, [
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'1 true',
|
|
||||||
'2 true',
|
|
||||||
'2 true',
|
|
||||||
'2 true',
|
|
||||||
'2 true'
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,52 +0,0 @@
|
|||||||
<script>
|
|
||||||
import Component from './Component.svelte';
|
|
||||||
|
|
||||||
let show = $state(true);
|
|
||||||
let count = $state(0);
|
|
||||||
let spread = $derived({ checked: show, count });
|
|
||||||
|
|
||||||
// Runes dynamic component pattern
|
|
||||||
let DynComponent1 = $derived(count < 2 ? Component : undefined);
|
|
||||||
let DynComponent2 = $derived(show ? Component : undefined);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<button id="increment" onclick={() => count++}>Increment count</button>
|
|
||||||
<button id="toggle" onclick={() => (show = !show)}>Toggle 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 bind:count bind:checked={show} />
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern -->
|
|
||||||
<DynComponent1 bind:count bind:checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with spread -->
|
|
||||||
<DynComponent1 {...spread} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with normal props -->
|
|
||||||
<DynComponent1 {count} checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern (show) -->
|
|
||||||
<DynComponent2 bind:count bind:checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with spread (show) -->
|
|
||||||
<DynComponent2 {...spread} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with normal props (show) -->
|
|
||||||
<DynComponent2 {count} checked={show} />
|
|
@ -1,11 +0,0 @@
|
|||||||
<script>
|
|
||||||
import { onDestroy } from 'svelte';
|
|
||||||
|
|
||||||
let { checked, count } = $props();
|
|
||||||
|
|
||||||
onDestroy(() => {
|
|
||||||
console.log(`${count} ${checked}`);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<p>Count: {count}</p>
|
|
@ -1,49 +0,0 @@
|
|||||||
import { flushSync } from 'svelte';
|
|
||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
async test({ assert, logs, target }) {
|
|
||||||
/** @type {HTMLButtonElement | null} */
|
|
||||||
const increment_btn = target.querySelector('#increment');
|
|
||||||
/** @type {HTMLButtonElement | null} */
|
|
||||||
const toggle_btn = target.querySelector('#toggle');
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 0');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Click increment: count=1
|
|
||||||
flushSync(() => {
|
|
||||||
increment_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 1');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Click increment again: count=2, components with count < 2 should unmount and log old values
|
|
||||||
flushSync(() => {
|
|
||||||
increment_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const p of target.querySelectorAll('p')) {
|
|
||||||
assert.equal(p.innerHTML, 'Count: 2');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggle show to hide components that depend on show
|
|
||||||
flushSync(() => {
|
|
||||||
toggle_btn?.click();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Should log old values during cleanup from the four components guarded by `count < 2`:
|
|
||||||
// 1. Component with normal props: "1 true"
|
|
||||||
// 2. Component with spread: "1 true"
|
|
||||||
// 3. Runes dynamic component with normal props: "1 true"
|
|
||||||
// 4. Runes dynamic component with spread: "1 true"
|
|
||||||
// Then from the three components guarded by `show`:
|
|
||||||
// 5. Component with normal props (show): "2 true" (old value of checked)
|
|
||||||
// 6. Runes dynamic component with normal props (show): "2 true"
|
|
||||||
// 7. Runes dynamic component with spread (show): "2 true"
|
|
||||||
assert.deepEqual(logs, ['1 true', '1 true', '1 true', '1 true', '2 true', '2 true', '2 true']);
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,41 +0,0 @@
|
|||||||
<script>
|
|
||||||
import Component from './Component.svelte';
|
|
||||||
|
|
||||||
let show = $state(true);
|
|
||||||
let count = $state(0);
|
|
||||||
let spread = $derived({ checked: show, count });
|
|
||||||
|
|
||||||
// Runes dynamic component pattern
|
|
||||||
let DynComponent1 = $derived(count < 2 ? Component : undefined);
|
|
||||||
let DynComponent2 = $derived(show ? Component : undefined);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<button id="increment" onclick={() => count++}>Increment count</button>
|
|
||||||
<button id="toggle" onclick={() => (show = !show)}>Toggle show</button>
|
|
||||||
|
|
||||||
<!-- normal props -->
|
|
||||||
{#if count < 2}
|
|
||||||
<Component {count} checked={show} />
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<!-- spread syntax -->
|
|
||||||
{#if count < 2}
|
|
||||||
<Component {...spread} />
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<!-- prop only accessed in destroy -->
|
|
||||||
{#if show}
|
|
||||||
<Component {count} checked={show} />
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with normal props -->
|
|
||||||
<DynComponent1 {count} checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with spread -->
|
|
||||||
<DynComponent1 {...spread} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern (show) with normal props -->
|
|
||||||
<DynComponent2 {count} checked={show} />
|
|
||||||
|
|
||||||
<!-- runes dynamic component pattern with spread (show) -->
|
|
||||||
<DynComponent2 {...spread} />
|
|
Loading…
Reference in new issue