Skip to content

Commit 2ae5869

Browse files
committed
add test for mutations on hydration
1 parent 1cbb44b commit 2ae5869

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
// This test count mutations on hydration
5+
// set_class() shoult not mutate class on hydration, except if mismatch
6+
export default test({
7+
server_props: {
8+
browser: false
9+
},
10+
11+
props: {
12+
browser: true
13+
},
14+
15+
html: `
16+
<main id="main" class="browser">
17+
<div class="custom svelte-1cjqok6 foo bar"></div>
18+
<span class="svelte-1cjqok6 foo bar"></span>
19+
<b class="custom foo bar"></b>
20+
<i class="foo bar"></i>
21+
</main>
22+
`,
23+
24+
ssrHtml: `
25+
<main id="main">
26+
<div class="custom svelte-1cjqok6 foo bar"></div>
27+
<span class="svelte-1cjqok6 foo bar"></span>
28+
<b class="custom foo bar"></b>
29+
<i class="foo bar"></i>
30+
</main>
31+
`,
32+
33+
async test({ assert, component, instance, variant }) {
34+
// only on hydration
35+
if (variant === 'hydrate') {
36+
flushSync();
37+
assert.deepEqual(instance.get_and_clear_mutations(), ['MAIN']);
38+
39+
component.foo = false;
40+
flushSync();
41+
assert.deepEqual(instance.get_and_clear_mutations(), ['DIV', 'SPAN', 'B', 'I']);
42+
}
43+
}
44+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<script>
2+
let {
3+
clazz = 'custom',
4+
foo = true,
5+
bar = true,
6+
browser
7+
} = $props();
8+
9+
let mutations = [];
10+
let observer;
11+
12+
if (browser) {
13+
observer = new MutationObserver(updateMutationRecords);
14+
const main = document.querySelector('main#main');
15+
if (main) {
16+
observer.observe(main, { attributes: true, subtree: true });
17+
}
18+
19+
}
20+
21+
22+
function updateMutationRecords(results) {
23+
for (const r of results) {
24+
mutations.push(r.target.nodeName);
25+
}
26+
}
27+
28+
export function get_and_clear_mutations() {
29+
updateMutationRecords(observer.takeRecords());
30+
const result = mutations;
31+
mutations = [];
32+
return result;
33+
}
34+
35+
</script>
36+
37+
<main id="main" class:browser>
38+
<div class={clazz} class:foo class:bar></div>
39+
<span class:foo class:bar></span>
40+
<b class={clazz} class:foo class:bar></b>
41+
<i class:foo class:bar></i>
42+
</main>
43+
44+
<style>
45+
div,
46+
span {
47+
color: red;
48+
}
49+
</style>

0 commit comments

Comments
 (0)