Skip to content

Instantly share code, notes, and snippets.

@hauntedhost
Created October 18, 2013 17:12
Show Gist options
  • Save hauntedhost/7044683 to your computer and use it in GitHub Desktop.
Save hauntedhost/7044683 to your computer and use it in GitHub Desktop.
jasmine spy cheatsheet
//= require jquery
//= require track-events
describe('TrackEvents', function () {
beforeEach(function () {
loadFixtures('track-events_fixture');
});
it('triggers a Test.fire', function() {
Test = function () { }
Test.fire = function () { }
spyOn(Test, 'fire');
Test.fire();
expect(Test.fire).toHaveBeenCalled();
});
it('triggers a click', function () {
$link = $('js-test-link1');
spyOn($link, 'click');
$link.click();
expect($link.click).toHaveBeenCalled();
});
it('triggers a click handler', function () {
callback = function (foo) { }
spyOn(window, 'callback');
$link = $('.js-test-link1');
$link.click(function () {
callback('bar');
});
$link.click();
expect(window.callback).toHaveBeenCalled();
expect(window.callback).toHaveBeenCalledWith('bar');
// expect(test).toEqual('hello');
});
it('triggers a TrackEvents callback', function () {
callback = function (message, properties) { }
spyOn(window, 'callback');
TrackEvents.onClickEvents(function (message, properties) {
callback('foo', 'bar');
});
$link = $('[data-track-onclick-event]').first();
$link.click();
expect(window.callback).toHaveBeenCalled();
expect(window.callback).toHaveBeenCalledWith('foo', 'bar');
});
it('triggers an Analytical.event when a watched link is clicked', function () {
Analytical = {
event: function () {}
}
spyOn(Analytical, 'event');
TrackEvents.onClickEvents();
$link = $('[data-track-onclick-event]').first();
$link.click();
expect(Analytical.event).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment