storybook/docs/snippets/react/nextjs-headers-mock.js.mdx
2024-04-30 22:21:31 -06:00

27 lines
781 B
Plaintext

```js
// MyForm.stories.js
import { expect, userEvent, within } from '@storybook/test';
import { cookies, headers } from '@storybook/nextjs/headers.mock';
import MyForm from './my-form';
export default {
component: MyForm,
};
export const LoggedInEurope = {
async beforeEach() {
// 👇 Set mock cookies and headers ahead of rendering
cookies().set('username', 'Sol');
headers().set('timezone', 'Central European Summer Time');
},
async play() {
// 👇 Assert that your component called the mocks
await expect(cookies().get).toHaveBeenCalledOnce();
await expect(cookies().get).toHaveBeenCalledWith('username');
await expect(headers().get).toHaveBeenCalledOnce();
await expect(cookies().get).toHaveBeenCalledWith('timezone');
},
};
```