'use strict'; describe('ngOn* event binding', function() { it('should add event listener of specified name', inject(function($compile, $rootScope) { $rootScope.name = 'Misko'; var element = $compile('')($rootScope); element.triggerHandler('foo'); expect($rootScope.name).toBe('Misko3'); })); it('should use angular.element(x).on() API to add listener', inject(function($compile, $rootScope) { spyOn(angular.element.prototype, 'on'); var element = $compile('')($rootScope); expect(angular.element.prototype.on).toHaveBeenCalledWith('foo', jasmine.any(Function)); })); it('should allow access to the $event object', inject(function($rootScope, $compile) { var element = $compile('')($rootScope); element.triggerHandler('foo'); expect($rootScope.e.target).toBe(element[0]); })); it('should call the listener synchronously', inject(function($compile, $rootScope) { var element = $compile('')($rootScope); $rootScope.fooEvent = jasmine.createSpy('fooEvent'); element.triggerHandler('foo'); expect($rootScope.fooEvent).toHaveBeenCalledOnce(); })); it('should support multiple events on a single element', inject(function($compile, $rootScope) { var element = $compile('')($rootScope); $rootScope.fooEvent = jasmine.createSpy('fooEvent'); $rootScope.barEvent = jasmine.createSpy('barEvent'); element.triggerHandler('foo'); expect($rootScope.fooEvent).toHaveBeenCalled(); expect($rootScope.barEvent).not.toHaveBeenCalled(); $rootScope.fooEvent.calls.reset(); $rootScope.barEvent.calls.reset(); element.triggerHandler('bar'); expect($rootScope.fooEvent).not.toHaveBeenCalled(); expect($rootScope.barEvent).toHaveBeenCalled(); })); it('should work with different prefixes', inject(function($rootScope, $compile) { var cb = $rootScope.cb = jasmine.createSpy('ng-on cb'); var element = $compile('')($rootScope); element.triggerHandler('test'); expect(cb).toHaveBeenCalledWith(1); element.triggerHandler('test2'); expect(cb).toHaveBeenCalledWith(2); element.triggerHandler('test3'); expect(cb).toHaveBeenCalledWith(3); })); it('should work if they are prefixed with x- or data- and different prefixes', inject(function($rootScope, $compile) { var cb = $rootScope.cb = jasmine.createSpy('ng-on cb'); var element = $compile('')($rootScope); element.triggerHandler('test2'); expect(cb).toHaveBeenCalledWith(2); element.triggerHandler('test3'); expect(cb).toHaveBeenCalledWith(3); element.triggerHandler('test4'); expect(cb).toHaveBeenCalledWith(4); element.triggerHandler('test5'); expect(cb).toHaveBeenCalledWith(5); element.triggerHandler('test6'); expect(cb).toHaveBeenCalledWith(6); })); it('should work independently of attributes with the same name', inject(function($rootScope, $compile) { var element = $compile('')($rootScope); var cb = $rootScope.cb = jasmine.createSpy('ng-on cb'); $rootScope.$digest(); element.triggerHandler('asdf'); expect(cb).toHaveBeenCalled(); expect(element.attr('asdf')).toBe('foo'); })); it('should work independently of (ng-)attributes with the same name', inject(function($rootScope, $compile) { var element = $compile('')($rootScope); var cb = $rootScope.cb = jasmine.createSpy('ng-on cb'); $rootScope.$digest(); element.triggerHandler('asdf'); expect(cb).toHaveBeenCalled(); expect(element.attr('asdf')).toBe('foo'); })); it('should work independently of properties with the same name', inject(function($rootScope, $compile) { var element = $compile('')($rootScope); var cb = $rootScope.cb = jasmine.createSpy('ng-on cb'); $rootScope.$digest(); element.triggerHandler('asdf'); expect(cb).toHaveBeenCalled(); expect(element.prop('asdf')).toBe(123); })); it('should use the full ng-on-* attribute name in $attr mappings', function() { var attrs; module(function($compileProvider) { $compileProvider.directive('attrExposer', valueFn({ link: function($scope, $element, $attrs) { attrs = $attrs; } })); }); inject(function($compile, $rootScope) { $compile('