Skip to content

Commit

Permalink
chore: reset uid between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed Feb 14, 2025
1 parent 868e3fa commit 7ffc25e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/svelte/src/internal/client/dom/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ export function append(anchor, dom) {

let uid = 1;

export function reset_props_id() {
uid = 1;
}

/**
* Create (or hydrate) an unique UID for the component instance.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/svelte/tests/runtime-legacy/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { setup_html_equal } from '../html_equal.js';
import { raf } from '../animation-helpers.js';
import type { CompileOptions } from '#compiler';
import { suite_with_variants, type BaseTest } from '../suite.js';
import { reset_props_id } from '../../src/internal/client/dom/template.js';

type Assert = typeof import('vitest').assert & {
htmlEqual(a: string, b: string, description?: string): void;
Expand Down Expand Up @@ -345,6 +346,7 @@ async function run_test_variant(

if (runes) {
props = proxy({ ...(config.props || {}) });
reset_props_id();
if (manual_hydrate) {
hydrate_fn = () => {
instance = hydrate(mod.default, {
Expand Down
60 changes: 46 additions & 14 deletions packages/svelte/tests/runtime-runes/samples/props-id/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,57 @@ import { test } from '../../test';

export default test({
test({ assert, target, variant }) {
const ps = [...target.querySelectorAll('p')].map((p) => p.innerHTML);
const unique = new Set(ps);
assert.equal(ps.length, unique.size);

if (variant === 'hydrate') {
const start = ps.map((p) => p.substring(0, 1));
assert.deepEqual(start, ['s', 's', 's', 's']);
if (variant === 'dom') {
assert.htmlEqual(
target.innerHTML,
`
<button>toggle</button>
<h1>c1</h1>
<p>c2</p>
<p>c3</p>
<p>c4</p>
`
);
} else {
assert.htmlEqual(
target.innerHTML,
`
<button>toggle</button>
<h1>s1</h1>
<p>s2</p>
<p>s3</p>
<p>s4</p>
`
);
}

let button = target.querySelector('button');
flushSync(() => button?.click());

const ps_after = [...target.querySelectorAll('p')].map((p) => p.innerHTML);
const unique_after = new Set(ps_after);
assert.equal(ps_after.length, unique_after.size);

if (variant === 'hydrate') {
const start = ps_after.map((p) => p.substring(0, 1));
assert.deepEqual(start, ['s', 's', 's', 's', 'c']);
if (variant === 'dom') {
assert.htmlEqual(
target.innerHTML,
`
<button>toggle</button>
<h1>c1</h1>
<p>c2</p>
<p>c3</p>
<p>c4</p>
<p>c5</p>
`
);
} else {
assert.htmlEqual(
target.innerHTML,
`
<button>toggle</button>
<h1>s1</h1>
<p>s2</p>
<p>s3</p>
<p>s4</p>
<p>c1</p>
`
);
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<button onclick={() => show = !show}>toggle</button>

<p>{id}</p>
<h1>{id}</h1>

<Child />
<Child />
Expand Down

0 comments on commit 7ffc25e

Please sign in to comment.