Test a function defined in application.js and called in jQuery(function($){} with jasmine

StackOverflow https://stackoverflow.com/questions/22408005

سؤال

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.

هل كانت مفيدة؟

المحلول

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();
    });
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top