-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Svelte: Improve support for async components #31476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
24021fd
call tick() after every mount of Svelte components, add (disabled) st…
JReinhold 4d4e6fd
Merge branch 'next' into jeppe/svelte-async-component-support
JReinhold b091f3b
update snapshots
JReinhold 8c8f904
Merge branch 'jeppe/svelte-async-component-support' of github.com:sto…
JReinhold c997203
Merge branch 'next' into jeppe/svelte-async-component-support
JReinhold 99f33b2
Merge branch 'next' of github.com:storybookjs/storybook into jeppe/sv…
JReinhold 6615e45
update svelte related packages
JReinhold 7f7d51f
enable async stories
JReinhold 0dd561e
cleanup
JReinhold e71e871
Merge branch 'next' into jeppe/svelte-async-component-support
JReinhold 4d14ff3
clear yarn.lock content
JReinhold 0c7723f
Merge branch 'jeppe/svelte-async-component-support' of github.com:sto…
JReinhold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| //! this config isn't actually used, it's just here to tell svelte-check that we have async enabled | ||
|
|
||
| const config = { | ||
| kit: { | ||
| experimental: { | ||
| remoteFunctions: true, | ||
| }, | ||
| }, | ||
| compilerOptions: { | ||
| experimental: { | ||
| async: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
17 changes: 17 additions & 0 deletions
17
code/renderers/svelte/template/stories/AsyncComponent.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <script lang="ts"> | ||
| let shown = $state(false); | ||
|
|
||
| let { onEffect }: { onEffect?: () => void } = $props(); | ||
|
|
||
| await new Promise<void>((resolve) => { | ||
| setTimeout(() => { | ||
| onEffect?.(); | ||
| shown = true; | ||
| resolve(); | ||
| }, 100); | ||
| }); | ||
| </script> | ||
|
|
||
| {#if shown} | ||
| <div data-testid="after-effect">This element is shown after the component's effect runs</div> | ||
| {/if} |
14 changes: 14 additions & 0 deletions
14
code/renderers/svelte/template/stories/SyncComponent.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <script lang="ts"> | ||
| let shown = $state(false); | ||
|
|
||
| let { onEffect }: { onEffect?: () => void } = $props(); | ||
|
|
||
| $effect(() => { | ||
| onEffect?.(); | ||
| shown = true; | ||
| }); | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </script> | ||
|
|
||
| {#if shown} | ||
| <div data-testid="after-effect">This element is shown after the component's effect runs</div> | ||
| {/if} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { StoryObj } from '@storybook/svelte'; | ||
|
|
||
| import { expect, fn, waitFor } from 'storybook/test'; | ||
|
|
||
| import AsyncComponent from './AsyncComponent.svelte'; | ||
| import SyncComponent from './SyncComponent.svelte'; | ||
|
|
||
| export default { | ||
| title: 'stories/renderers/svelte/settled', | ||
| }; | ||
|
|
||
| export const Sync: StoryObj<typeof SyncComponent> = { | ||
| render: (args) => ({ | ||
| Component: SyncComponent, | ||
| props: args, | ||
| }), | ||
| args: { | ||
| onEffect: fn(), | ||
| }, | ||
| play: async ({ canvas, args }) => { | ||
| await expect(args.onEffect).toHaveBeenCalledOnce(); | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await expect(canvas.getByTestId('after-effect')).toBeInTheDocument(); | ||
| }, | ||
| }; | ||
|
|
||
| export const Async: StoryObj<typeof AsyncComponent> = { | ||
| render: (args) => ({ | ||
| Component: AsyncComponent, | ||
| props: args, | ||
| }), | ||
| args: { | ||
| onEffect: fn(), | ||
| }, | ||
| play: async ({ canvas, canvasElement, args }) => { | ||
| await expect(args.onEffect).not.toHaveBeenCalled(); | ||
| await expect( | ||
| canvasElement.querySelector('#sb-pending-async-component-notice') | ||
| ).toBeInTheDocument(); | ||
|
|
||
| // TODO: Ideally we should be able to call await svelte.settled() here instead of waitFor, but currently there's a bug making it never resolve | ||
| // await svelte.settled(); | ||
|
|
||
| await waitFor(async () => { | ||
| await expect(args.onEffect).toHaveBeenCalledOnce(); | ||
| await expect(canvas.getByTestId('after-effect')).toBeInTheDocument(); | ||
| await expect( | ||
| canvasElement.querySelector('#sb-pending-async-component-notice') | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.