Pregunta

Estoy probando qunit al escribir un plugin de jQuery y me preguntaba cómo puedo comprobar lo siguiente:

(function($){

    $.fn.myPlugin = function(options){
        var defaults = {
            foo: function(){
                return 'bar';
            }
        };

        options = $.extend(defaults, options);

        return this.each(function(){ ... });
    };

})(jQuery);

Esta es una versión simple de mi prueba qunit:

module('MyPlugin: Configuration');

test('Can overwrite foo', function(){
    var mockFoo = function(){ 
        return 'no bar';
    };

    //equals(notsure.myPlugin({ foo: mockFoo }, 'no bar', 'Overwriting failed');
});

Así que me preguntaba cómo podría exponer métodos / miembros internos de mi plug-in dentro de mis pruebas?

¿Fue útil?

Solución

Niza después hice mi generosidad me encontré con un muy buen sitio que explica cómo utilizar .data () para mostrar las propiedades y métodos plubic.

Aquí puede encontrar el mensaje entero blog: construcción orientada a objetos jquery plugin de .

Esto es todo ejemplo desde el enlace anterior por lo que todos los créditos van al autor del blog.

(function($){
   var MyPlugin = function(element, options)
   {
       var elem = $(element);
       var obj = this;
       var settings = $.extend({
           param: 'defaultValue'
       }, options || {});

       // Public method - can be called from client code
       this.publicMethod = function()
       {
           console.log('public method called!');
       };

       // Private method - can only be called from within this object
       var privateMethod = function()
       {
           console.log('private method called!');
       };
   };

   $.fn.myplugin = function(options)
   {
       return this.each(function()
       {
           var element = $(this);

           // Return early if this element already has a plugin instance
           if (element.data('myplugin')) return;

           // pass options to plugin constructor
           var myplugin = new MyPlugin(this, options);

           // Store plugin object in this element's data
           element.data('myplugin', myplugin);
       });
   };
})(jQuery);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top