Giter VIP home page Giter VIP logo

Comments (4)

yannbf avatar yannbf commented on May 28, 2024

Hey @sw-tracker can you show your actual code in a minimal reproduction?

from testing-react.

sw-tracker avatar sw-tracker commented on May 28, 2024

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:
image

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.

yannbf avatar yannbf commented on May 28, 2024

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.

sw-tracker avatar sw-tracker commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.