0

I have a requireJS setup where I have an 'imageGallery' module which listens for and handles click events on '.gallery' elements. This in turn uses a plugin 'magnificPopup' to open a gallery of images based on the clicked element.

I want to test this using Jasmine; that is I want to test that the click event is called and that the html for the magnificPopup is in the dom - proving that the click event is successful.

My current click test uses 'waitsFor' but this is always failing. Any ideas on how I can acheive this?

Image Gallery Module

define(

    ['jquery', 'magnificPopup'],

    function($, magnificPopup) {

    return {

        init: function() {

            $('body').on('click', '.gallery', function(e) {
                e.preventDefault();

            //code to generate  items...

            $.magnificPopup.open({
                items: items,
                gallery: { enabled: true }
            });         


            });

        }
    }
})

Image Gallery Jasmine Test

define(['imageGallery'],

    function(imageGallery) {

        jasmine.getFixtures().fixturesPath = '/';

        describe('Image Galleries', function() {

            beforeEach(function(){
                loadFixtures('/path-to-my-fixture');
                spyOn(imageGallery, 'init');
                imageGallery.init();
            });

            it('Should be defined', function() {
                expect(imageGallery).not.toBe(null);
            });            

            it('Should be initialised via imageGallery.init()', function() {
                expect(imageGallery.init).toHaveBeenCalled();                
            });                       

            //fails
            it('Should open a modal after a .gallery element is clicked', function() {                 

                $('.gallery').click();

                waitsFor(function() {
                   return ($('body').find('.mfp-ready').length > 0)
                }, '.gallery to be clicked', 500);

            });



        });

    }

);
jigglyT101
  • 984
  • 1
  • 15
  • 33
  • I'm not sure if it's related to your problem, but when you spy a function it replaces it with a stub that by default doesn't call the original function. What you probably want to do is `spyOn(imageGallery, 'init').andCallThrough();`. – HoLyVieR Jun 06 '13 at 18:04
  • possible duplicate of [how to unit test DOM manipulation (with jasmine)](http://stackoverflow.com/questions/16163852/how-to-unit-test-dom-manipulation-with-jasmine) – Paul Sweatte Apr 23 '14 at 23:03

0 Answers0