I never had to test my angularjs directives before, also the directives I wrote for my current company is uses events to communicated directives to directives and services.
And so I wrote a directive, e.g. a search directive.
<m-search />
This directive broadcasts "searchbox-valuechanged"
event and the key, now I have to write tests for it.
'use strict';
describe('<m-search>', function() {
beforeEach(module('hey.ui'));
var rootScope;
beforeEach(inject(function($injector) {
rootScope = $injector.get('$rootScope');
spyOn(rootScope, '$broadcast');
}));
it("should broadcast something", function() {
expect(rootScope.$broadcast).toHaveBeenCalledWith('searchbox-valuechanged');
});
});
Update On change on the input,
<input class="m-input m-input--no-border" type="search" placeholder="Search"
ng-model="ctrl.searchValue"
ng-model-options="{debounce: 100}"
ng-change="ctrl.onChange({search: ctrl.searchValue})">
It calls a method in the directive's controller
vm.onChange = function (searchValue) {
$rootScope.$broadcast('searchbox-valuechanged', {data: searchValue});
};
How do I test broadcasting?