From 9d9b64b94eb4a1b700d15da9fa08e793ed6ded06 Mon Sep 17 00:00:00 2001 From: raythurnvoid <53383860+raythurnvoid@users.noreply.github.com> Date: Sun, 29 Jun 2025 16:46:58 +0100 Subject: [PATCH] Add tests for props old values in onDestroy hooks to prevent regressions --- .../Component.svelte | 11 ++++ .../_config.js | 63 +++++++++++++++++++ .../main.svelte | 52 +++++++++++++++ .../Component.svelte | 14 +++++ .../_config.js | 63 +++++++++++++++++++ .../main.svelte | 52 +++++++++++++++ .../Component.svelte | 11 ++++ .../_config.js | 49 +++++++++++++++ .../main.svelte | 41 ++++++++++++ 9 files changed, 356 insertions(+) create mode 100644 packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/Component.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/main.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/Component.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/main.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/Component.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/main.svelte diff --git a/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/Component.svelte b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/Component.svelte new file mode 100644 index 0000000000..40d6b1c2fb --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/Component.svelte @@ -0,0 +1,11 @@ + + +
Count: {count}
diff --git a/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/_config.js b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/_config.js new file mode 100644 index 0000000000..07ea5af153 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/_config.js @@ -0,0 +1,63 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + async test({ assert, logs, target }) { + /** @type {HTMLButtonElement | null} */ + const increment_btn = target.querySelector('#increment'); + /** @type {HTMLButtonElement | null} */ + const toggle_btn = target.querySelector('#toggle'); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 0'); + } + + // Click increment: count=1 + flushSync(() => { + increment_btn?.click(); + }); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 1'); + } + + // Click increment again: count=2, components with count < 2 should unmount and log old values + flushSync(() => { + increment_btn?.click(); + }); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 2'); + } + + // Toggle show to hide components that depend on show + flushSync(() => { + toggle_btn?.click(); + }); + + // Should log old values during cleanup from the six components guarded by `count < 2`: + // 1. Component with bind: "1 true" + // 2. Component with spread: "1 true" + // 3. Component with normal props: "1 true" + // 4. Runes dynamic component with bind: "1 true" + // 5. Runes dynamic component with spread: "1 true" + // 6. Runes dynamic component with normal props: "1 true" + // Then from the four components guarded by `show`: + // 7. Component with bind (show): "2 true" (old value of checked) + // 8. Runes dynamic component with bind (show): "2 true" + // 9. Runes dynamic component with spread (show): "2 true" + // 10. Runes dynamic component with normal props (show): "2 true" + assert.deepEqual(logs, [ + '1 true', + '1 true', + '1 true', + '1 true', + '1 true', + '1 true', + '2 true', + '2 true', + '2 true', + '2 true' + ]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/main.svelte b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/main.svelte new file mode 100644 index 0000000000..592c46ff42 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-no-setter/main.svelte @@ -0,0 +1,52 @@ + + + + + + +{#if count < 2} +Count: {count}
+ + + diff --git a/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/_config.js b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/_config.js new file mode 100644 index 0000000000..07ea5af153 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/_config.js @@ -0,0 +1,63 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + async test({ assert, logs, target }) { + /** @type {HTMLButtonElement | null} */ + const increment_btn = target.querySelector('#increment'); + /** @type {HTMLButtonElement | null} */ + const toggle_btn = target.querySelector('#toggle'); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 0'); + } + + // Click increment: count=1 + flushSync(() => { + increment_btn?.click(); + }); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 1'); + } + + // Click increment again: count=2, components with count < 2 should unmount and log old values + flushSync(() => { + increment_btn?.click(); + }); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 2'); + } + + // Toggle show to hide components that depend on show + flushSync(() => { + toggle_btn?.click(); + }); + + // Should log old values during cleanup from the six components guarded by `count < 2`: + // 1. Component with bind: "1 true" + // 2. Component with spread: "1 true" + // 3. Component with normal props: "1 true" + // 4. Runes dynamic component with bind: "1 true" + // 5. Runes dynamic component with spread: "1 true" + // 6. Runes dynamic component with normal props: "1 true" + // Then from the four components guarded by `show`: + // 7. Component with bind (show): "2 true" (old value of checked) + // 8. Runes dynamic component with bind (show): "2 true" + // 9. Runes dynamic component with spread (show): "2 true" + // 10. Runes dynamic component with normal props (show): "2 true" + assert.deepEqual(logs, [ + '1 true', + '1 true', + '1 true', + '1 true', + '1 true', + '1 true', + '2 true', + '2 true', + '2 true', + '2 true' + ]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/main.svelte b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/main.svelte new file mode 100644 index 0000000000..592c46ff42 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/bindable-props-cleanup-with-setter/main.svelte @@ -0,0 +1,52 @@ + + + + + + +{#if count < 2} +Count: {count}
diff --git a/packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/_config.js b/packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/_config.js new file mode 100644 index 0000000000..3d73bfd1a9 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/_config.js @@ -0,0 +1,49 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + async test({ assert, logs, target }) { + /** @type {HTMLButtonElement | null} */ + const increment_btn = target.querySelector('#increment'); + /** @type {HTMLButtonElement | null} */ + const toggle_btn = target.querySelector('#toggle'); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 0'); + } + + // Click increment: count=1 + flushSync(() => { + increment_btn?.click(); + }); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 1'); + } + + // Click increment again: count=2, components with count < 2 should unmount and log old values + flushSync(() => { + increment_btn?.click(); + }); + + for (const p of target.querySelectorAll('p')) { + assert.equal(p.innerHTML, 'Count: 2'); + } + + // Toggle show to hide components that depend on show + flushSync(() => { + toggle_btn?.click(); + }); + + // Should log old values during cleanup from the four components guarded by `count < 2`: + // 1. Component with normal props: "1 true" + // 2. Component with spread: "1 true" + // 3. Runes dynamic component with normal props: "1 true" + // 4. Runes dynamic component with spread: "1 true" + // Then from the three components guarded by `show`: + // 5. Component with normal props (show): "2 true" (old value of checked) + // 6. Runes dynamic component with normal props (show): "2 true" + // 7. Runes dynamic component with spread (show): "2 true" + assert.deepEqual(logs, ['1 true', '1 true', '1 true', '1 true', '2 true', '2 true', '2 true']); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/main.svelte b/packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/main.svelte new file mode 100644 index 0000000000..8bc6abfa4d --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/regular-props-cleanup-old-value/main.svelte @@ -0,0 +1,41 @@ + + + + + + +{#if count < 2} +