Comments (4)
Hey @sw-tracker can you show your actual code in a minimal reproduction?
from testing-react.
Here is a stand-alone example:
import { StoryFn, StoryObj } from '@storybook/react';
import React, { ReactElement } from 'react';
import { Dialog, DialogContent, DialogTitle, Grid } from '@mui/material';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
const MyTestModal = (): ReactElement => {
return (
<>
<Grid container direction={'row'} justifyContent={'center'}>
<Grid item xs={12}>
<Dialog
open={true}
aria-labelledby="dialog-title"
scroll={'paper'}
PaperProps={{
square: true,
style: {
boxShadow: 'initial',
},
}}
maxWidth={'lg'}
>
<DialogTitle>
<h1>My Title</h1>
</DialogTitle>
<DialogContent>
<Grid container direction="row" spacing={4}>
<Grid item xs={6}>
<Grid
container
direction="row"
spacing={3}
alignItems="flex-start"
>
<Grid item xs={6}>
<h1>Test Content</h1>
</Grid>
</Grid>
</Grid>
</Grid>
</DialogContent>
</Dialog>
</Grid>
</Grid>
</>
);
};
export default {
title: 'Runtime-Integration/TestModal/v1.0.0',
component: MyTestModal,
} as StoryObj<typeof MyTestModal>;
const Template: StoryFn<typeof MyTestModal> = () => <MyTestModal />;
// ----------------------------------------------------------------------------
export const TestModalCase = Template.bind({});
TestModalCase.args = {};
TestModalCase.play = async ({ canvasElement, step }) => {
const canvas = await within(canvasElement);
await step('Verify modal title', async () => {
await expect(canvas.getByText('My Title')).toBeInTheDocument();
});
};
The modal/dialog is not rendered where storybook expects it:
Versions:
"react": "18.2.0",
"@mui/icons-material": "5.11.11",
"@mui/lab": "5.0.0-alpha.122",
"@mui/material": "5.10.10",
"@mui/styles": "5.10.9",
"@mui/x-date-pickers-pro": "5.0.4",
"storybook": "7.0.2",
from testing-react.
Thanks @sw-tracker! I still don't see the implementation of your modal, but I assume it portals to document.body, and therefore it's not present under canvasElement
. In this case, you can use screen
instead, which searches elements under document.body:
import { screen } from '@storybook/testing-library'
TestModalCase.play = async ({ canvasElement, step }) => {
await step('Verify modal title', async () => {
await expect(screen.getByText('My Title')).toBeInTheDocument();
});
};
it's not recommended to do this for other scenarios, so use canvasElement
when possible 👍
from testing-react.
The Modal is done by this element here <Dialog>
: https://mui.com/material-ui/react-dialog/.
The screen approach worked for this scenario! Thank you!
from testing-react.
Related Issues (20)
- @storybook/preview-web stops exporting WebProjectAnnotations at 6.5.0 HOT 8
- `name` is changed when composing stories HOT 1
- Expose types HOT 1
- `StoryContext.title` Is Always An Empty String HOT 1
- What is the TypeScript return type of within()? HOT 2
- Using setGlobalConfig remove window global overrides from test setup HOT 2
- Types unknown, not read by Jest HOT 4
- Default import from `@storybook/addons` breaks in `[email protected]` HOT 1
- Storybook 7.0 alpha.44 removed defaultDecorateStory, breaks testing-react HOT 3
- Run test failed TypeError: Super expression must either be null or a function HOT 3
- Storybook preview hooks can only be called inside decorators and story functions
- `play` with `composeStories` throws type error on @next HOT 2
- Nextjs decorators support HOT 3
- Use the new Framework API HOT 2
- Stories that are excluded cause errors HOT 2
- Mocking UUIDs HOT 1
- This package is deprecated as it is a core functionality of Storybook 7!
- How to capture arguments to an action
- Wrong decorator order HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testing-react.