Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "ng-on", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Directive for Angular 1.x to easily bind custom (or not) events", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,3 +21,7 @@ ;(function() { | ||
scope.$evalAsync(function() { | ||
eventsAndCallbacks[eventName](event); | ||
try { | ||
eventsAndCallbacks[eventName](event); | ||
} catch(e) { | ||
throwError(e, eventName); | ||
} | ||
}); | ||
@@ -31,3 +35,9 @@ }); | ||
function throwError(error, eventName) { | ||
if(error instanceof TypeError) { | ||
throw new Error('handler for event "' + eventName + '" is not a function'); | ||
} | ||
} | ||
})(); | ||
@@ -0,1 +1,3 @@ | ||
/* global jasmine, describe, it, beforeEach, expect, module, inject, createCompiler */ | ||
'use strict'; | ||
@@ -20,18 +22,42 @@ | ||
it('should call defined event handler', function() { | ||
var eventObj = { | ||
click: jasmine.createSpy('clickSpy') | ||
var events = { | ||
click: jasmine.createSpy('clickSpy'), | ||
'the-amazing-spiderman': jasmine.createSpy('the-amazing-spiderman') | ||
}; | ||
compile({ events: eventObj }, elementAttrsMock, function (scope, element, driver) { | ||
compile({ events: events }, elementAttrsMock, function (scope, element) { | ||
scope = $rootScope.$new(); | ||
element.triggerHandler('click'); | ||
element.triggerHandler('the-amazing-spiderman'); | ||
scope.$digest(); | ||
expect(scope.$parent.$$childHead.events.click).toHaveBeenCalledWith(jasmine.objectContaining({ | ||
type: 'click' | ||
})); | ||
expect(scope.$parent.$$childHead.events.click) | ||
.toHaveBeenCalledWith(jasmine.objectContaining({ | ||
type: 'click' | ||
})); | ||
expect(scope.$parent.$$childHead.events['the-amazing-spiderman']) | ||
.toHaveBeenCalledWith(jasmine.objectContaining({ | ||
type: 'the-amazing-spiderman' | ||
})); | ||
}); | ||
}); | ||
}); | ||
describe('when given incorrect event object', function() { | ||
it('should throw error', function() { | ||
var events = { 'shitsOnFireYo': undefined }; | ||
compile({ events: events }, elementAttrsMock, function (scope, element) { | ||
scope = $rootScope.$new(); | ||
expect(function() { | ||
element.triggerHandler('shitsOnFireYo'); | ||
scope.$digest(); | ||
}).toThrow(new Error('handler for event "shitsOnFireYo" is not a function')); | ||
}); | ||
}); | ||
}); | ||
}); | ||
8427
137