Skip to content

Commit a39267c

Browse files
author
Josh Adams
committed
Add websocketService meant for broadcasting over local pubsub
This isn't really *finished* yet per se, but it's the first time I've written tests for the concept that I felt good about, so I figured I'd commit now and keep moving.
1 parent 35e0abf commit a39267c

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ grunt watch
2323
### Finally, add the 'app' directory as an extension in chrome...
2424

2525
I'll put instructions here if needed, later.
26+
27+
### To run the tests:
28+
29+
```bash
30+
karma start
31+
```
32+
33+
You can also just hit <leader>t in vim to run them, as long as there's a karma
34+
session running in the background.

app/scripts/services.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
angular.module('chromechatApp.Services', []);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
angular.module('chromechatApp.Services')
4+
.factory('$websocketService', [function() {
5+
return function(scope) {
6+
return {
7+
onmessage: function(message){
8+
scope.broadcast(message.data);
9+
}
10+
};
11+
};
12+
}]);

karma-shared.conf.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var shared = function(config) {
22
config.set({
33
basePath: '',
44
exclude: [],
5-
port: 8080,
5+
port: 8081,
66
runnerPort: 9100,
77
colors: true,
88
autoWatch: true,
@@ -45,7 +45,6 @@ shared.files = [
4545
'app/bower_components/underscore/underscore.js',
4646
'app/bower_components/angular/angular.js',
4747
'app/bower_components/angular-sanitize/angular-sanitize.js',
48-
//'app/bower_components/angular-resource/angular-resource.js',
4948
'app/bower_components/angular-mocks/angular-mocks.js',
5049
'app/scripts/vendor/foundation/foundation.js',
5150
'app/scripts/*.js',

test/spec/directives/user_list.js test/spec/directives/user_list_spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('UserList', function () {
66

77
beforeEach(function(){
88
module('chromechatApp.Directives', 'templates');
9+
910
inject(function($compile, $rootScope) {
1011
el = angular.element("<user-list></user-list>");
1112
scope = $rootScope;
@@ -15,6 +16,6 @@ describe('UserList', function () {
1516
});
1617

1718
it('has a user in it', function () {
18-
expect(angular.element('ul li', el)[0].innerText).toContain('knewter');
19+
expect(angular.element('ul li', el)[0].innerText).toEqual('knewter');
1920
});
2021
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*global describe, it */
2+
'use strict';
3+
4+
describe('$websocketService', function () {
5+
var subject, mockScope;
6+
7+
beforeEach(function(){
8+
module('chromechatApp.Services');
9+
mockScope = {
10+
broadcast: function(){}
11+
};
12+
13+
inject(function($websocketService) {
14+
subject = new $websocketService(mockScope);
15+
});
16+
});
17+
18+
it('can inject the websocketService', function () {
19+
expect(subject).not.toBe(undefined);
20+
});
21+
22+
it('broadcasts message on the injected scope when onmessage(MessageEvent)', function () {
23+
spyOn(mockScope, 'broadcast');
24+
// MessageEvent just needs a data property, so this acts like it.
25+
subject.onmessage({ data: 'foo' });
26+
expect(mockScope.broadcast).toHaveBeenCalledWith('foo');
27+
});
28+
});

0 commit comments

Comments
 (0)