Skip to main content
added 7 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

review jasmine tests: spyOn click events and check call function

I tryI'm trying to write jasmineJasmine tests and I need a review and some advices to spyadvice on spying on events and theretheir behavior.

review jasmine tests: spyOn click events and check call function

I try to write jasmine tests and I need a review and some advices to spy on events and there behavior.

spyOn click events and check call function

I'm trying to write Jasmine tests and I need a review and some advice on spying on events and their behavior.

Tweeted twitter.com/#!/StackCodeReview/status/528865720969355265
Source Link

review jasmine tests: spyOn click events and check call function

I try to write jasmine tests and I need a review and some advices to spy on events and there behavior.

// my example source code
;(function () {
  var app = {

    exampleModuleName: {
      navigateTo: function () { /* do something */ }
    },

    navigation: {
      init: function () {
        this.routeHandler = this.handleRouteTo.bind(this);
        
        // bind all events
        this.bind();
      },
      bind: function () {            
        $(document).on('click', '.js-route-to', this.routeHandler);
      },
      teardown: function () {
        $(document).off('click', this.routeHandler);
      },

      handleRouteTo: function (event) {
        event.preventDefault();
        this.routeTo($(event.currentTarget).data('target'));
      },

      routeTo: function (target) {
        var module = app[target];
        if (module && $.isFunction(module.navigateTo)) {
            module.navigateTo();
        }
      },
    }
  };
}());

And that is my uncompleted spec file.

// my test code
describe('App Navigation', function () {

  describe('initiation the navigation module', function() {

    it ('spy on behavior', function () {
        var spy = spyOn(app.navigation, 'init');
        app.navigation.init();
        expect(spy).toHaveBeenCalled();
    });

    // which tests are missing to be sure about well tested properties and methodes?
   });

  describe('test routeTo handler', function() {
    var nav = app.navigation;

    beforeEach(function () {
        this.routeHandler = nav.handleRouteTo.bind(this);
    });

    it ('should be defined', function () {
        expect(this.routeHandler).toBeDefined();
    });

    it ('click at a link', function () {
        var node = $('<a href="" class="js-route-to" data-target="exampleModuleName">some text</a>');

        expect(node.length).toBe(1);

        spyOn(nav, 'handleRouteTo');
        node.click();
        
        // why is it failed?
        expect(nav.handleRouteTo).toHaveBeenCalled();
    });

  });
});

Please take a look and let us talk about this example. I want to learn more about testing.