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.