2012-01-12 11 views
5

Sto scrivendo un plug-in che usa un plugin esistente che mi piacerebbe prendere in giro.Come faccio a prendere in giro un plugin jQuery?

Il plugin che sto scrivendo sembra un po 'come questo:

(function($){ 
    $.widget("myPlugin",{ 
    _create: function(){ 
     var otherControl = $("<div></div>"); 
     otherControl.pluginWhichShouldBeMocked({foo: "bar"}); 
     this.element.append(otherControl); 
    } 
    }); 
})(jQuery); 

E ho una prova di Jasmine che tipo di assomiglia a questo:

describe("When creating", function(){ 
    var element; 
    var passedOptions; 
    beforeEach(function(){ 
    jQuery.pluginWhichShouldBeMocked = function(options){ 
     passedOptions = options; 
    } 
    element = $("<div></div>"); 
    element.myPlugin(); 
    }); 

    it("should create the other plugin and pass 'bar' to it as the foo parameter", function(){ 
    expect(passedOptions.foo).toEqual("bar"); 
    }); 
}); 

Questa linea è dove ho provato per simulare il plug-in:

jQuery.pluginWhichShouldBeMocked = function(options){ 
    passedOptions = options; 
} 

L'istanza del plug-in effettiva viene comunque chiamata.

risposta

0

Ok. Ho appena trovato un modo per farlo, non sono sicuro se sia il migliore ma funziona per i miei scopi.

ho creare un plugin finto nella configurazione di prova in questo modo, con un metodo per recuperare le opzioni che posso usare nel mio affermare:

describe("When creating", function(){ 
    var element; 
    var passedOptions; 
    beforeEach(function(){ 
    $.widget("pluginWhichShouldBeMocked",{ 
     getOptions: function(){ 
     return this.options; 
     } 
    }); 
    element = $("<div id='extenalPlugin'></div>"); 
    element.myPlugin(); 
    }); 

    it("should create the other plugin and pass 'bar' to it as the foo parameter", function(){ 
    expect(element.find("div#externalPlugin").pluginWhichShouldBeMocked("getOptions").foo).toEqual("bar"); 
    }); 
}); 
0

di recente ho passato attraverso questo con Bootstrap modale e fissati con:

beforeEach(()=>{ 
    jQuery.fn.modal =() => {} 
}) 

describe(()=>{ 
    \\ ... do your thing 
}) 

afterEach(()=>{ 
    delete jQuery.fn.modal 
}) 
Problemi correlati