Jsdom: Implement DOM selection apis

Created on 5 Nov 2014  路  4Comments  路  Source: jsdom/jsdom

https://developer.mozilla.org/en-US/docs/Web/API/Window.getSelection

Even a nonfunctioning shim would go a long way. Currently I just do the following:

window.document.getSelection = function() { return { addRange: function() {}, removeAllRanges:function() { } } };

Just to avoid "cannot call undefined" when running getSelection().

feature

Most helpful comment

@domenic thanks, got the shim I needed by adding a version of @tolmasky's code in a Jest helper:

// spec/javascripts/helpers/jest-env.js
// window.getSelection isn't in jsdom
// https://github.com/tmpvar/jsdom/issues/937
window.getSelection = function() { 
  return { 
    addRange: function() {}, 
    removeAllRanges:function() {} 
  };
};

and linking to the above file via Jest's config.setupEnvScriptFile.

All 4 comments

We're not really in the business of nonfunctioning shims, but you're welcome to add that to your own jsdom windows.

Any ideas on how to bake this into how Jest uses jsdom?

Not sure myself; @Sebmaster is a bit more familiar with jest, but I think the Jest issue tracker will be your best bet.

@domenic thanks, got the shim I needed by adding a version of @tolmasky's code in a Jest helper:

// spec/javascripts/helpers/jest-env.js
// window.getSelection isn't in jsdom
// https://github.com/tmpvar/jsdom/issues/937
window.getSelection = function() { 
  return { 
    addRange: function() {}, 
    removeAllRanges:function() {} 
  };
};

and linking to the above file via Jest's config.setupEnvScriptFile.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khalyomede picture khalyomede  路  3Comments

potapovDim picture potapovDim  路  4Comments

Progyan1997 picture Progyan1997  路  3Comments

drewish picture drewish  路  4Comments

jhegedus42 picture jhegedus42  路  4Comments