mirror of https://github.com/sveltejs/svelte
parent
62f09d8546
commit
54db9bf102
@ -0,0 +1,32 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target, logs }) {
|
||||
await tick();
|
||||
const [increment, resolve] = target.querySelectorAll('button');
|
||||
logs.length = 0;
|
||||
|
||||
increment.click();
|
||||
await tick();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<button>increment</button> <button>resolve</button>
|
||||
<ul><li>0 / 0</li><li>0 / loading...</li><li>0 / 0</li></ul>`
|
||||
);
|
||||
|
||||
resolve.click();
|
||||
await tick();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<button>increment</button> <button>resolve</button>
|
||||
<ul><li>0 / 0</li><li>1 / 1</li><li>0 / 0</li></ul>`
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
logs.some((l) => l.toString().includes('0 ') || l.toString().includes('2')),
|
||||
false,
|
||||
'only the second $state.eager should have been evaluated'
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,24 @@
|
||||
<script>
|
||||
let counts = $state([0, 0, 0]);
|
||||
const queued = [];
|
||||
async function delay(v) {
|
||||
if (!v) return v;
|
||||
return new Promise(r => queued.push(() => r(v)));
|
||||
}
|
||||
</script>
|
||||
|
||||
<button onclick={() => counts[1]++}>increment</button>
|
||||
<button onclick={() => queued.shift()?.()}>resolve</button>
|
||||
|
||||
<ul>
|
||||
{#each counts as count, i}
|
||||
<li>
|
||||
{await delay(count)} /
|
||||
{#if console.log(i) || $state.eager(count) !== count}
|
||||
loading...
|
||||
{:else}
|
||||
{count}
|
||||
{/if}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
Loading…
Reference in new issue