Pregunta

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.

¿Fue útil?

Solución

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();
    });
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top