mswjs / msw-storybook-addon

Mock API requests in Storybook with Mock Service Worker.
https://msw-sb.vercel.app
MIT License
408 stars 39 forks source link

Passing msw returned resuts into specific storybook component args #35

Closed BennaceurHichem closed 3 years ago

BennaceurHichem commented 3 years ago

I'm trying to pass the result of the MSW request added in parameters to a specific story component arg, the approach described in all examples shown in the doc is passing the endpoint URL as props into the component, then the component treats the result. In my case, I want to pass the result of the MSW request into the story component as props via args, which means that the data will be ready for the component directly when the fetching from MSW is finished. How can I pass the MSW returned data directly into args in the story component? I'm using Nuxt.js and Nuxt-storybook module.

yannbf commented 3 years ago

Hey @BennaceurHichem, thanks for using this addon!

I'm trying to understand your use case, but I'm a bit confused. MSW is reactive, so it will only proxy requests once they are done, and then intercept the request and manipulate its response. It's very handy when you want to manipulate data that is not readily available.

It seems like you want your component's args to already have the data. If you want the data to be passed via args, then you should just pass the mock data directly to args, without even using the addon:

export const UserList = Template.bind({});
UserList.args = {
  data: someMockedData,
};

Could you elaborate a bit more? I might have misunderstood you.

yannbf commented 3 years ago

Hey @BennaceurHichem sorry but it's been quite some time and we haven't had an answer with more info. I'll close this issue if you don't mind. Feel free to open it again in case you have more information that could help us help you out!

Once again, thanks for using this library!