I am using MockAdapter with axios to mock api response in storybook
export const defaultAccountMockAPI = () => {
const mock = new MockAdapter(axiosInstance);
const defaultAccountDetails = objectKnob('Default Account Details', DefaultAccountDetails);
mock
.onGet(
'/services/api/account/1902124261/account-details',
)
.reply(() => {
return [200, defaultAccountDetails];
});
};
export const accoutMockAPI = () => {
const mock = new MockAdapter(axiosInstance);
const accountDetails = objectKnob('Account Details', AccountDetails);
mock
.onGet(
'/services/api/account/1902124221/account-details',
)
.reply(() => {
return [200, accountDetails];
});
};
Let's say I have above two methods which mock default account details and account details. The only difference between these two methods is different account id (1902124261/1902124221). I need to show 2 stories based on these 2 different accounts, how I can abstract the mock api method instead of for each story I need to write these duplicated codes(Apart from account id, I also have other parameters which have this issue as well.)
The README file of MockAdapter says that its possible to use a regular expression as url.
So with your code it should look like this:
mock
.onGet(/\/services\/api\/account\/.*?\/account-details/g)
.reply(() => {
return [200, accountDetails];
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.