mirror of https://github.com/sveltejs/svelte
parent
a012b1d1e0
commit
50da9268c3
@ -0,0 +1,33 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
/**
|
||||
* Ensure that mutating an array with push inside an $effect
|
||||
* does not cause an infinite re-execution loop.
|
||||
*/
|
||||
test({ assert, target }) {
|
||||
const button = target.querySelector('button');
|
||||
|
||||
// initial render — effect should have run once
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>inc</button>
|
||||
<p>0</p>
|
||||
`
|
||||
);
|
||||
|
||||
// increment count; effect should append one new entry only
|
||||
flushSync(() => button?.click());
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>inc</button>
|
||||
<p>0</p>
|
||||
<p>1</p>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,17 @@
|
||||
<script>
|
||||
let count = $state(0);
|
||||
let log = $state([]);
|
||||
|
||||
$effect(() => {
|
||||
log.push(count);
|
||||
});
|
||||
|
||||
function inc() {
|
||||
count += 1;
|
||||
}
|
||||
</script>
|
||||
|
||||
<button on:click={inc}>inc</button>
|
||||
{#each log as item}
|
||||
<p>{item}</p>
|
||||
{/each}
|
Loading…
Reference in new issue