mirror of https://github.com/sveltejs/svelte
parent
99ed291434
commit
bc917fe9a4
@ -0,0 +1,31 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
/** @type {HTMLButtonElement | null} */
|
||||
const increment = target.querySelector('#increment');
|
||||
/** @type {HTMLButtonElement | null} */
|
||||
const change_ref = target.querySelector('#change-ref');
|
||||
/** @type {HTMLParagraphElement | null} */
|
||||
const count = target.querySelector('#count');
|
||||
/** @type {HTMLParagraphElement | null} */
|
||||
const fallback_count = target.querySelector('#fallback-count');
|
||||
|
||||
assert.htmlEqual(count?.innerHTML ?? '', 'Count: 0');
|
||||
assert.htmlEqual(fallback_count?.innerHTML ?? '', 'Fallback count: 0');
|
||||
|
||||
await increment?.click();
|
||||
assert.htmlEqual(count?.innerHTML ?? '', 'Count: 1');
|
||||
assert.htmlEqual(fallback_count?.innerHTML ?? '', 'Fallback count: 0');
|
||||
|
||||
await change_ref?.click();
|
||||
await increment?.click();
|
||||
assert.htmlEqual(count?.innerHTML ?? '', 'Count: 1');
|
||||
assert.htmlEqual(fallback_count?.innerHTML ?? '', 'Fallback count: 1');
|
||||
|
||||
await change_ref?.click();
|
||||
await increment?.click();
|
||||
assert.htmlEqual(count?.innerHTML ?? '', 'Count: 2');
|
||||
assert.htmlEqual(fallback_count?.innerHTML ?? '', 'Fallback count: 1');
|
||||
}
|
||||
});
|
@ -0,0 +1,26 @@
|
||||
<script>
|
||||
function box(value) {
|
||||
let state = $state(value);
|
||||
return {
|
||||
get value() {
|
||||
return state
|
||||
},
|
||||
set value(v) {
|
||||
state = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let count = box(0);
|
||||
let fallback_count = box(0);
|
||||
let toggle_state = $state(false);
|
||||
</script>
|
||||
|
||||
{#snippet counter(c = count)}
|
||||
<p id="count">Count: {count.value}</p>
|
||||
<p id="fallback-count">Fallback count: {fallback_count.value}</p>
|
||||
<button id="increment" on:click={() => (c.value += 1)}>Click to change referenced state value</button>
|
||||
<button id="change-ref" on:click={() => toggle_state = !toggle_state}>Click to change state reference</button>
|
||||
{/snippet}
|
||||
|
||||
{@render counter(toggle_state ? fallback_count : undefined)}
|
Loading…
Reference in new issue