Skip to content

Commit 2608e62

Browse files
authored
add test (#9917)
1 parent b1efd8c commit 2608e62

File tree

4 files changed

+111
-8
lines changed

4 files changed

+111
-8
lines changed

.changeset/wild-foxes-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: cleanup each block animations on destroy

packages/svelte/src/internal/client/each.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -744,17 +744,27 @@ export function destroy_each_item_block(
744744
const transitions = block.s;
745745

746746
if (apply_transitions && transitions !== null) {
747-
trigger_transitions(transitions, 'out');
748-
if (transition_block !== null) {
749-
transition_block.push(block);
747+
// We might have pending key transitions, if so remove them first
748+
for (let other of transitions) {
749+
if (other.r === 'key') {
750+
transitions.delete(other);
751+
}
750752
}
751-
} else {
752-
const dom = block.d;
753-
if (!controlled && dom !== null) {
754-
remove(dom);
753+
if (transitions.size === 0) {
754+
block.s = null;
755+
} else {
756+
trigger_transitions(transitions, 'out');
757+
if (transition_block !== null) {
758+
transition_block.push(block);
759+
}
760+
return;
755761
}
756-
destroy_signal(/** @type {import('./types.js').EffectSignal} */ (block.e));
757762
}
763+
const dom = block.d;
764+
if (!controlled && dom !== null) {
765+
remove(dom);
766+
}
767+
destroy_signal(/** @type {import('./types.js').EffectSignal} */ (block.e));
758768
}
759769

760770
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { flushSync } from 'svelte';
2+
import { ok, test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target, window }) {
6+
const button = target.querySelector('button');
7+
ok(button);
8+
9+
assert.htmlEqual(
10+
target.innerHTML,
11+
`
12+
<button>Remove last</button><div class="list"><label><input type="checkbox">
13+
write
14+
some
15+
docs</label><label><input type="checkbox">
16+
start
17+
writing
18+
JSConf
19+
talk</label><label><input type="checkbox">
20+
buy
21+
some
22+
milk</label><label><input type="checkbox">
23+
mow
24+
the
25+
lawn</label><label><input type="checkbox">
26+
feed
27+
the
28+
turtle</label><label><input type="checkbox">
29+
fix
30+
some
31+
bugs</label></div>`
32+
);
33+
34+
flushSync(() => {
35+
button.click();
36+
});
37+
38+
assert.htmlEqual(
39+
target.innerHTML,
40+
`
41+
<button>Remove last</button><div class="list"><label><input type="checkbox">
42+
write
43+
some
44+
docs</label><label><input type="checkbox">
45+
start
46+
writing
47+
JSConf
48+
talk</label><label><input type="checkbox">
49+
buy
50+
some
51+
milk</label><label><input type="checkbox">
52+
mow
53+
the
54+
lawn</label><label><input type="checkbox">
55+
feed
56+
the
57+
turtle</label></div>`
58+
);
59+
}
60+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script>
2+
import { flip } from 'svelte/animate';
3+
4+
let todos = [
5+
{ id: 1, done: false, description: 'write some docs' },
6+
{ id: 2, done: false, description: 'start writing JSConf talk' },
7+
{ id: 3, done: true, description: 'buy some milk' },
8+
{ id: 4, done: false, description: 'mow the lawn' },
9+
{ id: 5, done: false, description: 'feed the turtle' },
10+
{ id: 6, done: false, description: 'fix some bugs' }
11+
];
12+
13+
function update() {
14+
todos = [...todos]
15+
todos.pop();
16+
}
17+
</script>
18+
19+
<button on:click={update}>Remove last</button>
20+
21+
<div class="list">
22+
{#each todos as todo (todo.id)}
23+
<label animate:flip>
24+
<input type="checkbox" bind:checked={todo.done} />
25+
{todo.description}
26+
</label>
27+
{/each}
28+
</div>

0 commit comments

Comments
 (0)