Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Object [object global] has no method 'getSelection' #75

Closed
n1k0 opened this issue Jun 15, 2014 · 6 comments
Closed

TypeError: Object [object global] has no method 'getSelection' #75

n1k0 opened this issue Jun 15, 2014 · 6 comments

Comments

@n1k0
Copy link

n1k0 commented Jun 15, 2014

This one's very odd. It seems you can't run two jest tests involving a component which uses the focus() method on a <textarea> element. (I warned you, very odd.)

Sample react component:

/** @jsx React.DOM */
var React = require("react");
var Plop = React.createClass({
  componentDidMount: function() {
    this.getDOMNode().querySelector("textarea").focus();
  },
  render: function() {
    return <div><textarea /></div>;
  }
});
module.exports = Plop;

Sample test:

/** @jsx React.DOM */

var TestUtils = require('react/addons').addons.TestUtils;

jest.dontMock('../plop');
var Plop = require('../plop');

describe("test", function() {
  it("should run a first time", function() {
    TestUtils.renderIntoDocument(<Plop />);
  });
});

This one passes just fine:

Found 1 matching tests...
 PASS  __tests__/bug-test.js (0.909s)
1 tests passed (1 total)

Now update the test suite adding another test:

/** @jsx React.DOM */
var TestUtils = require('react/addons').addons.TestUtils;
jest.dontMock('../plop');
var Plop = require('../plop');
describe("test", function() {
  it("should run a first time", function() {
    TestUtils.renderIntoDocument(<Plop />);
  });
  it("should run a second time", function() {
    TestUtils.renderIntoDocument(<Plop />);
  });
});

This new suite fails:

Found 1 matching tests...
 FAIL  __tests__/bug-test.js (0.924s)
● test › it should run a second time
  - TypeError: Object [object global] has no method 'getSelection'
        at getModernOffsets (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactDOMSelection.js:62:26)
        at Object.ReactDOMSelection.getOffsets (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactDOMSelection.js:176:12)
        at Object.ReactInputSelection.getSelection (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactInputSelection.js:107:37)
        at ReactReconcileTransaction.ReactInputSelection.getSelectionInformation (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactInputSelection.js:53:31)
        at ReactReconcileTransaction.Mixin.initializeAll (/Users/niko/tmp/jest-bug/node_modules/react/lib/Transaction.js:197:30)
        at ReactReconcileTransaction.Mixin.perform (/Users/niko/tmp/jest-bug/node_modules/react/lib/Transaction.js:158:12)
        at ReactComponent.Mixin.mountComponentIntoNode (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactComponent.js:537:19)
        at Object.ReactMount._renderNewRootComponent (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactMount.js:309:25)
        at Object._renderNewRootComponent (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactPerf.js:57:21)
        at Object.ReactMount.renderComponent (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactMount.js:359:32)
        at Object.renderComponent (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactPerf.js:57:21)
        at Object.ReactTestUtils.renderIntoDocument (/Users/niko/tmp/jest-bug/node_modules/react/lib/ReactTestUtils.js:57:18)
        at Spec.<anonymous> (/Users/niko/tmp/jest-bug/__tests__/bug-test.js:15:15)
        at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Side note, after investigating a little, it seems that ReactDOMSelection relies on window.getSelection for some stuff, but that jsdom doesn't support it (hence the error we get.)

Maybe you should start thinking of implementing required polyfills in jest/jsdom to match modern environments? (yeah, I know, tedious.)

Note: I've titled this bug with the exception string to help indexing this issue for people searching for the exact error message, feel free to rephrase if needed.

@PrototypeAlex
Copy link

As odd as it is, I've encountered this bug too, the workaround I'm using is to manually blur the element in the teardown step of the test.

instance.refs.input.getDOMNode().blur()

@bttmly
Copy link

bttmly commented Oct 24, 2014

@PrototypeAlex thanks for the workaround

@darcyadams
Copy link

also encountering this, @n1k0 - thanks for the title, this one was driving me crazy, I appreciate how easy it was to find this issue.

@ghost
Copy link

ghost commented Aug 4, 2015

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

@cpojer
Copy link
Member

cpojer commented Oct 16, 2015

Seems like there is a workaround and this is specific to React tests? Also might have been fixed in jsdom.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants