Created
November 16, 2010 21:40
-
-
Save vojtajina/702566 to your computer and use it in GitHub Desktop.
Angular - service example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('service-example', function() { | |
angular.service('notify', function(win) { | |
var msgs = []; | |
return function(msg) { | |
msgs.push(msg); | |
if (msgs.length == 3) { | |
win.alert(msgs.join("\n")); | |
msgs = []; | |
} | |
}; | |
}); | |
var mock, notify; | |
beforeEach(function() { | |
mock = {alert: createSpy()}; | |
notify = angular.service('notify')(mock); | |
}); | |
it('should not alert first two notifications', function() { | |
notify('one'); | |
notify('two'); | |
expect(mock.alert).not.toHaveBeenCalled(); | |
}); | |
it('should alert all after third notification', function() { | |
notify('one'); | |
notify('two'); | |
notify('three'); | |
expect(mock.alert).toHaveBeenCalledWith("one\ntwo\nthree"); | |
}); | |
it('should clear messages after alert', function() { | |
notify('one'); | |
notify('two'); | |
notify('third'); | |
notify('more'); | |
notify('two'); | |
notify('third'); | |
expect(mock.alert.callCount).toEqual(2); | |
expect(mock.alert.mostRecentCall.args).toEqual(["more\ntwo\nthird"]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment