diff --git a/angular-test-runner.d.ts b/angular-test-runner.d.ts index ec8bb0c..a8fdeec 100644 --- a/angular-test-runner.d.ts +++ b/angular-test-runner.d.ts @@ -74,7 +74,8 @@ declare namespace angularTestRunner { navigateTo: (url: string) => IAction; expectElement: (selector: string) => Matchers; listenTo: (eventName: string, handler: (data: any) => void) => IAction; - publishEvent: (eventName: string, data: any) => IAction; + broadcastEvent: (eventName: string, data: any) => IAction; + emitEvent: (eventName: string, data: any) => IAction; } interface Matchers { diff --git a/angular-test-runner.js b/angular-test-runner.js index a3ec41e..0a7cf93 100644 --- a/angular-test-runner.js +++ b/angular-test-runner.js @@ -26941,12 +26941,17 @@ function listenTo(event, handler) { }); }; } -function publishEvent(event, data) { - return function($el) { - angular.element($el).scope().$broadcast(event, data); - }; -} + function broadcastEvent(event, data) { + return function($el) { + angular.element($el).scope().$broadcast(event, data); + }; + } + function emitEvent(event, data) { + return function($el) { + angular.element($el).scope().$emit(event, data); + }; + } function wait(timeout) { return function () { return { @@ -27051,7 +27056,8 @@ module.exports = { apply: apply, expectElement: expectElement, listenTo: listenTo, - publishEvent: publishEvent + broadcastEvent: broadcastEvent, + emitEvent: emitEvent }; @@ -27225,4 +27231,4 @@ function wrap(req) { } },{"lodash":2}]},{},[5])(5) -}); \ No newline at end of file +}); diff --git a/src/actions.js b/src/actions.js index 4922419..7f32df7 100644 --- a/src/actions.js +++ b/src/actions.js @@ -55,12 +55,18 @@ function listenTo(event, handler) { }); }; } -function publishEvent(event, data) { +function broadcastEvent(event, data) { return function($el) { angular.element($el).scope().$broadcast(event, data); }; } +function emitEvent(event, data) { + return function($el) { + angular.element($el).scope().$emit(event, data); + }; +} + function wait(timeout) { return function () { return { @@ -165,6 +171,7 @@ module.exports = { apply: apply, expectElement: expectElement, listenTo: listenTo, - publishEvent: publishEvent + broadcastEvent: broadcastEvent, + emitEvent: emitEvent }; diff --git a/test/sample-test.js b/test/sample-test.js index 5f4527f..d089c9b 100644 --- a/test/sample-test.js +++ b/test/sample-test.js @@ -15,7 +15,7 @@ describe('sample test', function () { name: '=' }, controllerAs: 'vm', - controller: function ($http, $scope, $timeout) { + controller: function ($http, $scope, $timeout, $rootScope) { this.sayHello = function () { $http.post('/greeting', {name: $scope.name}) .then(function (response) { @@ -32,7 +32,11 @@ describe('sample test', function () { $scope.$emit('greeting', $scope.name); }; - $scope.$on('externalGreeting', function(event, greeting) { + $scope.$on('broadcastedGreeting', function(event, greeting) { + $scope.message = greeting; + }); + + $rootScope.$on('emitedGreeting', function(event, greeting) { $scope.message = greeting; }) }, @@ -55,7 +59,8 @@ describe('sample test', function () { var mouseover = testRunner.actions.mouseover; var mouseleave = testRunner.actions.mouseleave; var listenTo = testRunner.actions.listenTo; - var publishEvent = testRunner.actions.publishEvent; + var broadcastEvent = testRunner.actions.broadcastEvent; + var emitEvent = testRunner.actions.emitEvent; beforeEach(function () { @@ -191,14 +196,14 @@ describe('sample test', function () { expect(greeted).toEqual('John'); }); - it('allows event publishing', function () { + it('allows to broadcast event', function () { // given: var html = app.runHtml('', {}); // when: html.perform( - publishEvent('externalGreeting', 'Hello, Jimmy!') + broadcastEvent('broadcastedGreeting', 'Hello, Jimmy!') ); // then: @@ -207,5 +212,21 @@ describe('sample test', function () { ); }); + it('allows to emit event', function () { + + // given: + var html = app.runHtml('', {}); + + // when: + html.perform( + emitEvent('emitedGreeting', 'Hello, Jonny!') + ); + + // then: + html.verify( + expectElement('.greeting').toContainText('Hello, Jonny!') + ); + }); + });