Skip to content

Commit b985eea

Browse files
committed
add test
1 parent 4ce5f37 commit b985eea

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target, logs }) {
6+
const [b1, b2, b3] = target.querySelectorAll('button');
7+
8+
flushSync(() => {
9+
b1.click();
10+
b2.click();
11+
});
12+
13+
assert.deepEqual(logs, [0, 1]);
14+
15+
flushSync(() => {
16+
b3.click();
17+
});
18+
19+
assert.deepEqual(logs, [0, 1, 'cleanup 1', 'cleanup 2']);
20+
21+
flushSync(() => {
22+
b1.click();
23+
b2.click();
24+
});
25+
26+
assert.deepEqual(logs, [0, 1, 'cleanup 1', 'cleanup 2']);
27+
}
28+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { with_root } from './root.svelte.js';
3+
let x = $state(0);
4+
let y = $state(0);
5+
6+
const cleanup = with_root(() => x)
7+
</script>
8+
9+
<button onclick={() => x++}>{x}</button>
10+
<button onclick={() => y++}>{y}</button>
11+
<button onclick={cleanup}>cleanup</button>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function with_root(get_x) {
2+
const cleanup = $effect.root(() => {
3+
$effect(() => {
4+
console.log(get_x());
5+
});
6+
7+
const nested_cleanup = $effect.root(() => {
8+
return () => {
9+
console.log('cleanup 2');
10+
};
11+
});
12+
13+
return () => {
14+
console.log('cleanup 1');
15+
nested_cleanup();
16+
};
17+
});
18+
19+
return cleanup;
20+
}

0 commit comments

Comments
 (0)