Question

I have Ruby on Rails application and in my application.js file there is:

jQuery(function($) {
    submitFormForBlacklistsCsvExport();
}

function submitFormForBlacklistsCsvExport(){
  jQuery('#blacklists_csv_export').bind('click', function(){
   exportBlacklists('/whitelists')
  })
}

I am using jasmine for testing of javascript in the app but I couldn't find how to use spy to check that exportBlacklist is called only when '#blacklists_csv_export' is clicked.

Was it helpful?

Solution

This is a very common scenario. Spy on the function, trigger the event handler, then check that the spy was called.

describe("Blacklist exporting", function () {
    it("exports on click", function () {
        spyOn(window, "exportBlacklists");
        $("#blacklists_csv_export").triggerHandler("click");
        expect(window.exportBlacklists).toHaveBeenCalled();
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top