Repository navigation
SummaryI didn't see any other post on this, and I don't really know the full scope to fix the issue. I have been trying to pass page data to a story for a form I have using the parameters.sveltekit_experimental.stores per the documentation. After digging for a while, I identified the page context was updating, but the store wasn't. It has been driving me crazy for a day or so. I think I finally found the issue. After reading through the add-on files, the mockSvelteKitStores export from storybook/code/frameworks/src/plugins/mock-sveltekit-stores.ts file is using $app/stores as the alias. I changed my form to use $app/stores instead of $app/state and the page context updated the page store. $app/stores is depreciated in Svelte 5, so is there a way to alias both $app/stores and $app/state? I don't want to break the add-on for anyone else by just trying to push an update to the alias. import { getContext } from 'svelte'; const context = getContext('page-ctx'); Console output: Thanks! Additional informationNo response Create a reproductionNo response |
Replies: 2 comments 1 reply
|
So, I went through the SvelteKit documentation and repository to try to understand the implementation of $app/state in svelte5. I then took the storybook/svelte repository and implemented mocking of state. It works, but I have a sticking point on the implementation that I would like to figure out if someone on here can help. I made the following updates: I added the file state.ts to the @storybook/sveltekit/mocks folder. I did the state objects without $state.raw() because I kept getting a bundling error about using $state.raw() outside of a .svelte. I had the file originally named state.svelte.ts, but it still gave the error. I added the alias for $app/state to the mock-sveltekit-stores.ts file in @storybook/sveltekit/plugins folder:
From there, I built a wrapper to handle updating the state from context for display in the story. I tried this two ways, and don't know why one works but the other doesn't. I assume it has something to do with when the item is loading, but am not sure. The first method to update page, I used a component in my storybook, that I imported in my preview.ts file and added to decorators. ContextDecorator.svelte: preview.ts in .storybook: const preview: Preview = { When I run the story, the state updates and reflects correctly. However, if I make the component exportable from @storybook/sveltekit and import the component, it doesn't work. Similarly, my initial attempt to update the items in @storybook/sveltekit/preview.ts fails. I can't import the component to export from preview without getting the error "Invalid compiler option: The boolean options have been removed from the css option. Use "external" instead of false and "injected" instead of true". I also wrote the component as below and imported it into @storybook/sveltekit/preview.ts and added it to the decorator export array. However, state was not updated in that use. I can't tell if it is not creating the appropriate wrapper, or if it is running at the wrong time. ` const defaultPageData = { export const stateMockDecorator: Decorator = (Story, ctx) => { const Wrapper = { }; return { All of that is to say, I can get state updated from a component in storybook with a custom component added to the storybook preview, but believe their is a better way to do this so that it is transparent and doesn't require special configuration. Any insight is appreciated. Thank you in advance. |
|
You are correct, mocking It's pretty close to getting done, so I think the best approach would be to follow that PR :) There's a canary release of that PR (see the description) you can try out and let us know if it solves your use case. 🙏 |
You are correct, mocking
$app/stateisn't currently supported, but we have an open PR for exactly that here: #31369It's pretty close to getting done, so I think the best approach would be to follow that PR :)
There's a canary release of that PR (see the description) you can try out and let us know if it solves your use case. 🙏