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.

Tooltip issue using Jest: TypeError document.createRange is not a function

See original GitHub issue

Hi, I’m using Jest and Enzyme to test my component.

import React, { Component, Fragment } from "react";
import equal from "fast-deep-equal";
import { TextField, Tooltip, Zoom, withStyles } from "@material-ui/core";
import { checkValue, setValue } from "./PickersFunction";
import { customVariant } from "../../MuiTheme";
import {
  valueVerificationPropType,
  cellValPropType,
  classesPropType,
  typePropType
} from "../../../proptypes";

export class TextFieldWrapper extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tooltipOpen: false,
      message: "",
      error: false
    };
  }

  componentDidMount() {
    const { valueVerification } = this.props;
    if (valueVerification) {
      const newState = checkValue({
        ...this.props,
        mounting: true
      });
      if (!equal(this.state, newState)) {
        this.setState(newState);
      }
    }
  }

  onValueChange = e => {
    const { value } = e.target;
    const newState = setValue({
      ...this.props,
      value
    });
    if (!equal(this.state, newState)) {
      this.setState(newState);
    }
  };

  toggleCloseTooltip = open => {
    const { error } = this.state;
    if (error) {
      this.setState({ tooltipOpen: open });
    }
  };

  render() {
    const { type, cellVal, classes } = this.props;
    const { tooltipOpen, message, error } = this.state;

    return (
      <Tooltip
        open={tooltipOpen}
        classes={{
          tooltip: classes.errorTooltip
        }}
        title={message}
        TransitionComponent={Zoom}
        interactive
      >
        <Fragment>
          <TextField
            value={cellVal}
            error={error}
            onFocus={() => this.toggleCloseTooltip(true)}
            onBlur={() => this.setState({ tooltipOpen: false })}
            onChange={this.onValueChange}
            type={type}
            fullWidth
          />
        </Fragment>
      </Tooltip>
    );
  }
}

TextFieldWrapper.propTypes = {
  cellVal: cellValPropType.isRequired,
  classes: classesPropType.isRequired,
  type: typePropType.isRequired,
  valueVerification: valueVerificationPropType
};

export default withStyles(customVariant)(TextFieldWrapper);

My test :

it("should render a Tootltip and Textfield", () => {
    const wrapper = mount(
      <TextFieldWrapperPureComponent
        cellVal={10}
        type="number"
        columnId="age"
        rowId={rowId}
        valueVerification={valueVerification}
        setRowEdited={setRowEdited}
        classes={{ customVariant }}
      />
    );
    wrapper.instance().onValueChange({ target: { value: 105 } });
  });

Result: ``` TypeError: document.createRange is not a function

  41 |     });
  42 |     if (!equal(this.state, newState)) {
> 43 |       this.setState(newState);
     |            ^
  44 |     }
  45 |   };
  46 | 

… console.error node_modules/react-dom/cjs/react-dom.development.js:17117 The above error occurred in the <Portal> component: in Portal (created by Popper) in Popper (created by Tooltip) in Tooltip (created by WithStyles(Tooltip)) in WithStyles(Tooltip) (created by TextFieldWrapper) in TextFieldWrapper (created by WrapperComponent) in WrapperComponent


Any ideas ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

82reactions
oliviertassinaricommented, Jun 9, 2020

@MorganDbs See https://github.com/jsdom/jsdom/issues/317. We use the following patch for our tests:

  global.document.createRange = () => ({
    setStart: () => {},
    setEnd: () => {},
    commonAncestorContainer: {
      nodeName: 'BODY',
      ownerDocument: document,
    },
  });

Does it fix your problem?

Update, no longer required since Jest v26.0.0 and https://github.com/facebook/jest/pull/9606.

4reactions
oliviertassinaricommented, Apr 6, 2020

The issue should have been solved in jsdom@16.0.0 now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tooltip issue using Jest: TypeError document.createRange is ...
Hi, I'm using Jest and Enzyme to test my component. import React, { Component, ... createRange is not a function] Tooltip issue using...
Read more >
document.createRange is not a function` error while testing ...
I have a material-ui TextField which on focus opens a Popper . I am trying to test this behavior using react-testing-library. Component: import ......
Read more >
How to fix `TypeError: document.createRange is not a function ...
The issue is with the underlying implementation of PopperJS calling the document.createRange function when there is no DOM API for it to call....
Read more >
jest typeerror: class extends value - You.com | The AI Search ...
I am using ts-jest to write tests for my ts-node project. The application runs fine ... TypeError: Class extends value undefined is not...
Read more >
How To Mock Function In Setuptests.Js For Range ... - ADocLib
Unit Tests The unit tests use JSDOM to provide a primitive document object API, they in the Puppeteer environment by prepending a `@jest-environment ......
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