failing test + fix

pull/16641/head
Simon Holthausen 3 weeks ago
parent 14a4f6c09c
commit b975914267

@ -1,14 +0,0 @@
import { suspend } from './reactivity/batch.js';
/**
* @param {() => Promise<void>} fn
*/
export async function async_body(fn) {
const unsuspend = suspend();
try {
await fn();
} finally {
unsuspend();
}
}

@ -1,6 +1,5 @@
export { createAttachmentKey as attachment } from '../../attachments/index.js'; export { createAttachmentKey as attachment } from '../../attachments/index.js';
export { FILENAME, HMR, NAMESPACE_SVG } from '../../constants.js'; export { FILENAME, HMR, NAMESPACE_SVG } from '../../constants.js';
export { async_body } from './async_body.js';
export { push, pop, add_svelte_meta } from './context.js'; export { push, pop, add_svelte_meta } from './context.js';
export { assign, assign_and, assign_or, assign_nullish } from './dev/assign.js'; export { assign, assign_and, assign_or, assign_nullish } from './dev/assign.js';
export { cleanup_styles } from './dev/css.js'; export { cleanup_styles } from './dev/css.js';
@ -100,6 +99,7 @@ export {
with_script with_script
} from './dom/template.js'; } from './dom/template.js';
export { export {
async_body,
for_await_track_reactivity_loss, for_await_track_reactivity_loss,
save, save,
track_reactivity_loss track_reactivity_loss

@ -11,7 +11,7 @@ import {
set_active_effect, set_active_effect,
set_active_reaction set_active_reaction
} from '../runtime.js'; } from '../runtime.js';
import { current_batch } from './batch.js'; import { current_batch, suspend } from './batch.js';
import { import {
async_derived, async_derived,
current_async_effect, current_async_effect,
@ -19,6 +19,7 @@ import {
derived_safe_equal, derived_safe_equal,
set_from_async_derived set_from_async_derived
} from './deriveds.js'; } from './deriveds.js';
import { aborted } from './effects.js';
/** /**
* *
@ -170,3 +171,21 @@ export function unset_context() {
set_component_context(null); set_component_context(null);
if (DEV) set_from_async_derived(null); if (DEV) set_from_async_derived(null);
} }
/**
* @param {() => Promise<void>} fn
*/
export async function async_body(fn) {
const unsuspend = suspend();
const active = /** @type {Effect} */ (active_effect);
try {
await fn();
} catch (error) {
if (!aborted(active)) {
invoke_error_boundary(error, active);
}
} finally {
unsuspend();
}
}

@ -0,0 +1,9 @@
<script>
import { route } from "./main.svelte";
await new Promise(async (_, reject) => {
await Promise.resolve();
route.current = 'other'
route.reject = reject;
});
</script>

@ -0,0 +1,15 @@
import { tick } from 'svelte';
import { test } from '../../test';
export default test({
html: `<button>reject</button> <p>pending</p>`,
async test({ assert, target }) {
const [reject] = target.querySelectorAll('button');
await tick();
reject.click();
await tick();
assert.htmlEqual(target.innerHTML, '<button>reject</button> <p>route: other</p>');
}
});

@ -0,0 +1,18 @@
<script module>
import Child from './Child.svelte';
export let route = $state({ current: 'home' });
</script>
<button onclick={() => route.reject()}>reject</button>
<svelte:boundary>
{#if route.current === 'home'}
<Child />
{:else}
<p>route: {route.current}</p>
{/if}
{#snippet pending()}
<p>pending</p>
{/snippet}
</svelte:boundary>

@ -2,8 +2,6 @@
import { route } from "./main.svelte"; import { route } from "./main.svelte";
await new Promise(async (_, reject) => { await new Promise(async (_, reject) => {
await Promise.resolve();
route.current = 'other'
route.reject = reject; route.reject = reject;
}); });
</script> </script>

@ -7,9 +7,8 @@ export default test({
async test({ assert, target }) { async test({ assert, target }) {
const [reject] = target.querySelectorAll('button'); const [reject] = target.querySelectorAll('button');
await tick();
reject.click(); reject.click();
await tick(); await tick();
assert.htmlEqual(target.innerHTML, '<button>reject</button> <p>route: other</p>'); assert.htmlEqual(target.innerHTML, '<button>reject</button> <p>failed</p>');
} }
}); });

@ -1,18 +1,18 @@
<script module> <script module>
import Child from './Child.svelte'; import Child from './Child.svelte';
export let route = $state({ current: 'home' }); export let route = $state({});
</script> </script>
<button onclick={() => route.reject()}>reject</button> <button onclick={() => route.reject()}>reject</button>
<svelte:boundary> <svelte:boundary>
{#if route.current === 'home'}
<Child /> <Child />
{:else}
<p>route: {route.current}</p>
{/if}
{#snippet pending()} {#snippet pending()}
<p>pending</p> <p>pending</p>
{/snippet} {/snippet}
{#snippet failed()}
<p>failed</p>
{/snippet}
</svelte:boundary> </svelte:boundary>

Loading…
Cancel
Save