lundi 29 février 2016

Creating a stub of document.getSelection using Sinon

My function, which you can find below, uses document.getSelection to get the currently selected text on the screen. I need to be able to specify the value of document.getSelection so that I test my function.

I tried creating the stub like so:

document.getSelection = sinon.stub(document, "getSelection", function() { return "Hello world!" } );

var selection = wysiwyg.getCurrentRange();

However, It just get undefined for selection. Can someone tell me what I'm doing wrong?

Here's the method I'm testing:

 Wysiwyg.prototype.getCurrentRange = function() {
    var sel, range;
    if ( window.getSelection ) {
        sel = window.getSelection();
        if ( sel.getRangeAt && sel.rangeCount ) {
            range = sel.getRangeAt( 0 );
        }
    } else if ( document.selection ) {
        range = document.selection.createRange();
    }

    return range;
 };

Aucun commentaire:

Enregistrer un commentaire