Skip to content

Commit bacddf6

Browse files
committed
Removed requirejs wrapper
Added documentation
1 parent d04fb52 commit bacddf6

3 files changed

Lines changed: 133 additions & 33 deletions

File tree

README.md

100644100755
Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,106 @@
11
angular-socket.io-mock
22
======================
33

4-
Mock Socket.io bindings for AngularJS, useful for testing as a drop in replacement
4+
Mock Socket.io bindings for AngularJS, useful for testing as a drop in replacement for btford/angular-socket-io
5+
which allows testing against a fake server.
6+
7+
## Usage
8+
9+
### Standard usage
10+
11+
* Make sure AngularJS is loaded prior to inclusion.
12+
13+
karma.conf.js
14+
```js
15+
module.exports = function(config) {
16+
config.set({
17+
files: [
18+
{pattern: "bower_components/angular-socket.io-mock.js", included: false}
19+
]
20+
})
21+
}
22+
```
23+
24+
## With RequireJS
25+
26+
In all our projects we utilize RequireJS to use this test library with RequireJS is very simple. Just replace the entry
27+
that points to the standard angular-socket-io with this library. Below is an example.
28+
29+
main.js
30+
```js
31+
require.config({
32+
baseUrl: "/base/js",
33+
shim: {
34+
"socketio": {exports: "io"},
35+
"angular": {exports: "angular"},
36+
"ng-socket-io": ["angular","socketio"],
37+
"ng-mocks": ["angular"]
38+
},
39+
paths: {
40+
"angular": "/base/bower_components/angular/angular",
41+
"socketio": "/base/bower_components/socket.io-client/dist/socket.io",
42+
"ng-socket-io": "/base/bower_components/angular-socket.io-mock/angular-socket.io-mock",
43+
"ng-mocks": "/base/bower_components/angular-mocks/angular-mocks"
44+
},
45+
callback: window.__karma__.start
46+
})
47+
```
48+
49+
Your bower.json should look something like this.
50+
51+
bower.json
52+
```json
53+
{
54+
"name": "my-app",
55+
"version": "0.0.1",
56+
"dependencies": {
57+
"angular": "latest",
58+
"angular-socket-io": "latest"
59+
},
60+
"devDependencies": {
61+
"socket.io-client": "latest",
62+
"angular-mocks": "latest",
63+
"angular-socket.io-mock": "latest"
64+
}
65+
}
66+
```
67+
68+
### Example Test
69+
70+
This example uses Mocha Test as the testing framework and ChaiJS for the assertion library. It also uses RequireJS to
71+
load the environment.
72+
73+
test.js
74+
```js
75+
define(["chai","angular","angular-mocks","angular-socket-io"],function(chai){
76+
describe("Test Suite",function(){
77+
beforeEach(module("app"))
78+
it("should be using the mock ng-socket.io",$inject(function($rootScope,$controller){
79+
var expect = chai.expect
80+
, scope = $rootScope.$new()
81+
, ctrl = $controller("MyController",{$scope: scope})
82+
expect(scope.save).to.be.a("function")
83+
expect(scope.items).to.be.a("array")
84+
expect(scope.items.length).to.equal(3)
85+
}))
86+
})
87+
})
88+
```
89+
90+
MyController.js
91+
```js
92+
define(["angular","ng-socket-io"],function(ng){
93+
var app = ng.module("app",["btford.socket-io"])
94+
app.controller("MyController",["$scope","socket",function($scope,socket){
95+
//load items now
96+
$scope.items = []
97+
socket.emit("loadItems",function({},function(res){
98+
$scope.items = res.res
99+
})
100+
//setup functions
101+
$scope.save = function(){}
102+
}])
103+
})
104+
```
105+
106+
*Note* Please see https://github.com/btford/angular-socket-io for library usage.

angular-socket.io-mock.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
define(["bower_components/angular/angular"],function(angular){
2-
var ng = angular.module("btford.socket-io",[])
3-
ng.provider("socket",function(){
4-
this.$get = function($rootScope){
1+
/* global angular: false */
2+
var ng = angular.module("btford.socket-io",[])
3+
ng.provider("socket",function(){
4+
this.$get = function($rootScope){
55

6-
var obj = {}
7-
obj.events = {}
8-
obj.emits = {}
6+
var obj = {}
7+
obj.events = {}
8+
obj.emits = {}
99

10-
// intercept 'on' calls and capture the callbacks
11-
obj.on = function(eventName, callback){
12-
if(!this.events[eventName]) this.events[eventName] = []
13-
this.events[eventName].push(callback)
14-
}
10+
// intercept 'on' calls and capture the callbacks
11+
obj.on = function(eventName, callback){
12+
if(!this.events[eventName]) this.events[eventName] = []
13+
this.events[eventName].push(callback)
14+
}
1515

16-
// intercept 'emit' calls from the client and record them to assert against in the test
17-
obj.emit = function(eventName){
18-
var args = Array.prototype.slice.call(arguments,1)
16+
// intercept 'emit' calls from the client and record them to assert against in the test
17+
obj.emit = function(eventName){
18+
var args = Array.prototype.slice.call(arguments,1)
1919

20-
if(!this.emits[eventName])
21-
this.emits[eventName] = []
22-
this.emits[eventName].push(args)
23-
}
20+
if(!this.emits[eventName])
21+
this.emits[eventName] = []
22+
this.emits[eventName].push(args)
23+
}
2424

25-
//simulate an inbound message to the socket from the server (only called from the test)
26-
obj.receive = function(eventName){
27-
var args = Array.prototype.slice.call(arguments,1)
25+
//simulate an inbound message to the socket from the server (only called from the test)
26+
obj.receive = function(eventName){
27+
var args = Array.prototype.slice.call(arguments,1)
2828

29-
if(this.events[eventName]){
30-
angular.forEach(this.events[eventName], function(callback){
31-
$rootScope.$apply(function() {
32-
callback.apply(this, args)
33-
})
29+
if(this.events[eventName]){
30+
angular.forEach(this.events[eventName], function(callback){
31+
$rootScope.$apply(function() {
32+
callback.apply(this, args)
3433
})
35-
}
34+
})
3635
}
37-
return obj
3836
}
39-
})
37+
return obj
38+
}
4039
})

bower.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "0.0.1",
44
"dependencies": {
55
"angular": "latest",
6-
"angular-socket-io": "latest",
7-
"requirejs": "latest"
6+
"angular-socket-io": "latest"
87
}
98
}

0 commit comments

Comments
 (0)