question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Dialog][Enzyme] No way to open dialog and test actions.

See original GitHub issue

When testing a component that includes a <Dialog />, I can successfully simulate a click and verify that the component’s state is set to open. However, when I then try to find the defined action buttons, I am unable to do so.

// Code

class CustomDialog extends Component {
  ....
    this.state = {
      open: false,
    };

  handleOpen = () => {
    this.setState({ open: true });
  };

  handleDelete =() => {
    ....
  };

  handleClose = () => {
    ....
  };

  render() {
    const actions = [
      <FlatButton label="Cancel" onClick={this.handleClose} />,
      <FlatButton label="Delete" onClick={this.handleDelete} />,
    ];

    return (
      <span>
        <FlatButton label="Delete" onClick={this.handleOpen} />
        <Dialog
          actions={actions}
          open={this.state.open}
          onRequestClose={this.handleClose}
        >
          Delete? This operation cannot be reversed.
        </Dialog>
      </span>
    );
  }
}

// Tests

      expect(mountedComponent).toHaveState('open', false); <--PASS

      mountedComponent
        .find('FlatButton')
        .simulate('click');

      expect(mountedComponent).toHaveState('open', true); <--PASS

        expect(mountedComponent
          .find('FlatButton')
          .find('[label="Cancel"]')).toBePresent(); <<< FAIL

I haven’t done a sandbox because Dialog works fine for me in the browser, it’s only when testing that the issues arise. I have searched the issues of this repository and believe that this is not a duplicate.

Tech Version
Material-UI 0.19.4
React 16.0.0
enzyme 3.1.0

And finally, here is my mountedComponent.debug() output:

<AreaDeleteDialog className="delete" id="Hollywood" handleAreaDelete={[Function]}>
  <span>
    <FlatButton label="Delete" onClick={[Function]} disabled={false} fullWidth={false} labelStyle={{...}} labelPosition="after" onKeyboardFocus={[Function]} onMouseEnter={[Function]} onMouseLeave={[Function]} onTouchStart={[Function]} primary={false} secondary={false}>
      <EnhancedButton onClick={[Function]} onKeyboardFocus={[Function]} onMouseEnter={[Function]} onMouseLeave={[Function]} onTouchStart={[Function]} disabled={false} focusRippleColor="#999999" focusRippleOpacity={0.3} style={{...}} touchRippleColor="#999999" touchRippleOpacity={0.3} containerElement="button" onBlur={[Function]} onFocus={[Function]} onKeyDown={[Function]} onKeyUp={[Function]} tabIndex={0} type="button">
        <button onMouseEnter={[Function]} onMouseLeave={[Function]} onTouchStart={[Function]} style={{...}} disabled={false} onBlur={[Function]} onFocus={[Function]} onKeyUp={[Function]} onKeyDown={[Function]} onClick={[Function]} tabIndex={0} type="button">
          <TouchRipple centerRipple={[undefined]} color="#999999" opacity={0.3} abortOnScroll={true}>
            <div onMouseUp={[Function]} onMouseDown={[Function]} onMouseLeave={[Function]} onTouchStart={[Function]} onTouchEnd={[Function]}>
              <FlatButtonLabel label="Delete" style={{...}}>
                <span style={{...}}>
                  Delete
                </span>
              </FlatButtonLabel>
            </div>
          </TouchRipple>
        </button>
      </EnhancedButton>
    </FlatButton>
    <Dialog actions={{...}} open={true} onRequestClose={[Function]} autoDetectWindowHeight={true} autoScrollBodyContent={false} modal={false} repositionOnUpdate={true}>
      <RenderToLayer render={[Function]} open={true} useLayerForClickAway={false} />
    </Dialog>
  </span>
</AreaDeleteDialog>'

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
tonywong625commented, Oct 16, 2018

I know its closed, but did anyone find the solution to this?

6reactions
kgregorycommented, Nov 17, 2017

This is not an issue with material-ui and is more of a question about using enzyme, which is probably better suited for StackOverflow.

Here are some quick thoughts: If you’re shallow rendering the Dialog, you won’t be able to find the FlatButtons because they haven’t been rendered. Instead, they are nothing more than a value assigned to the actions prop, unless you dive into the Dialog, which should render its children, making them accessible through the shallow wrapper:

wrapper.find(Dialog).dive().find(FlatButton)

Also, you should try using the class definition as the selector rather than its description.

// not this
mountedComponent.find('FlatButton')

// this (of course you'll need to import it into your test)
mountedComponent.find(FlatButton)
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Dialog][Enzyme] No way to open dialog and test actions. #9200
When testing a component that includes a , I can successfully simulate a click and verify that the component's state is set to...
Read more >
Unit testing React component using Material UI Dialog
So I can get the Dialog instance by calling TestUtils.scryRenderedComponentsWithType(Dialog). But I can not get the dialogs content. DOM wise ...
Read more >
Build and Test Modal using React features and DOM events ...
In this issue, we are going to build a Modal (or Dialog) component to get familiar with testing components that rely on Portals,...
Read more >
Continuous integration for React applications using Jest and ...
Learn how to implement continuous integration for React applications using Jest and Enzyme.
Read more >
Modals | Testing Library
import React, {useEffect} from 'react' import ReactDOM from 'react-dom' import {render, fireEvent} from '@testing-library/react'
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found