Skip to content

Commit 3a191cf

Browse files
author
Christian Jeschke
committed
added some samples on how to write unit tests + fixed minor ugly API handling
1 parent 794964c commit 3a191cf

9 files changed

+159
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ server in order to test your applications:
120120
```javascript
121121
var $websocketBackend;
122122

123-
beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock');
123+
beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock'));
124124

125125
beforeEach(inject(function (_$websocketBackend_) {
126126
$websocketBackend = _$websocketBackend_;

dist/angular-websocket-mock.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
};
5555

5656
this.fakeClose = function (url, code) {
57+
closeQueue.push(url);
5758
if (existingMocks[url]) {
5859
existingMocks[url].map(function (mockSocket) {
5960
mockSocket.close(code);

example/howToTest/controller.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
angular.module("appToTest", ['ngWebSocket']).controller("ControllerToTest", ["$scope", "$websocket", function($scope, $websocket) {
2+
var socket;
3+
$scope.isConnected = false;
4+
5+
function initializeSocket() {
6+
socket.onOpen(function() {
7+
$scope.isConnected = true;
8+
});
9+
socket.onClose(function() {
10+
$scope.isConnected = false;
11+
});
12+
socket.onMessage(function(message) {
13+
$scope.lastReceivedMessage = JSON.parse(message.data);
14+
});
15+
}
16+
17+
$scope.connect = function() {
18+
socket = $websocket("wss://foo");
19+
initializeSocket();
20+
};
21+
22+
$scope.sendMessage = function(sMessage) {
23+
socket.send(sMessage);
24+
};
25+
}]);

example/howToTest/controller.spec.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
describe("ControllerToTest", function() {
2+
var controller, $scope, $websocketBackend;
3+
4+
beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock'));
5+
6+
beforeEach(function() {
7+
module("appToTest");
8+
inject(function(_$websocketBackend_, _$controller_) {
9+
$websocketBackend = _$websocketBackend_;
10+
$scope = {};
11+
controller = _$controller_('ControllerToTest', { $scope: $scope });
12+
});
13+
14+
$websocketBackend.expectConnect("wss://foo");
15+
});
16+
17+
afterEach(function() {
18+
$websocketBackend.verifyNoOutstandingRequest();
19+
$websocketBackend.verifyNoOutstandingExpectation();
20+
});
21+
22+
function connect() {
23+
$scope.connect();
24+
$websocketBackend.flush();
25+
}
26+
27+
it("should initiate a connection on connect", function() {
28+
connect();
29+
});
30+
31+
it("should react on an established connection", function() {
32+
expect($scope.isConnected).toEqual(false);
33+
connect();
34+
expect($scope.isConnected).toEqual(true);
35+
});
36+
37+
it("should react on a closing connection", function() {
38+
connect();
39+
$websocketBackend.fakeClose("wss://foo");
40+
$websocketBackend.flush();
41+
expect($scope.isConnected).toEqual(false);
42+
});
43+
44+
it("should react on an incoming message", function() {
45+
connect();
46+
$websocketBackend.fakeMessage("wss://foo", "hello");
47+
$websocketBackend.flush();
48+
expect($scope.lastReceivedMessage).toEqual("hello");
49+
});
50+
51+
it("should send a message to the socket", function() {
52+
connect();
53+
$websocketBackend.expectSend("wss://foo", "greetings");
54+
$scope.sendMessage("greetings");
55+
$websocketBackend.flush();
56+
});
57+
});

example/howToTest/karma.conf.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Karma configuration
2+
// Generated on Thu Dec 25 2014 14:12:24 GMT-0800 (PST)
3+
4+
module.exports = function(config) {
5+
6+
var configuration = {
7+
8+
// base path, that will be used to resolve files and exclude
9+
basePath: '',
10+
11+
// frameworks to use
12+
frameworks: ['jasmine'],
13+
14+
// list of files / patterns to load in the browser
15+
files: [
16+
'../../bower_components/angular/angular.js',
17+
'../../bower_components/angular-mocks/angular-mocks.js',
18+
'../../dist/angular-websocket.js',
19+
'../../dist/angular-websocket-mock.js',
20+
'./controller.js',
21+
'./controller.spec.js'
22+
],
23+
24+
// list of files to exclude
25+
exclude: [
26+
27+
],
28+
29+
// test results reporter to use
30+
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
31+
reporters: ['progress'],
32+
33+
// web server port
34+
port: 9876,
35+
36+
// enable / disable colors in the output (reporters and logs)
37+
colors: true,
38+
39+
// level of logging
40+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
41+
logLevel: config.LOG_WARN,
42+
43+
// enable / disable watching file and executing tests whenever any file changes
44+
autoWatch: false,
45+
46+
// Start these browsers, currently available:
47+
// - Chrome
48+
// - ChromeCanary
49+
// - Firefox
50+
// - Opera (has to be installed with `npm install karma-opera-launcher`)
51+
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
52+
// - PhantomJS
53+
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
54+
browsers: ['PhantomJS'],
55+
56+
// If browser does not capture in given timeout [ms], kill it
57+
captureTimeout: 60000,
58+
59+
// Continuous Integration mode
60+
// if true, it capture browsers, run tests and exit
61+
singleRun: true
62+
};
63+
64+
config.set(configuration);
65+
};

example/howToTest/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "howToTest",
3+
"scripts": {
4+
"test": "karma start karma.conf.js"
5+
}
6+
}

karma.conf.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ module.exports = function(config) {
1717
'bower_components/angular-mocks/angular-mocks.js',
1818
'dist/angular-websocket.js',
1919
'dist/angular-websocket-mock.js',
20-
'test/angular-websocket.spec.js'
20+
'test/angular-websocket.spec.js',
21+
'example/howToTest/controller.js',
22+
'example/howToTest/controller.spec.js',
2123
],
2224

2325
// list of files to exclude

src/angular-websocket-mock.js

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function $WebSocketBackend() {
3434
};
3535

3636
this.fakeClose = function(url, code) {
37+
closeQueue.push(url);
3738
if(existingMocks[url]) {
3839
existingMocks[url].map(function(mockSocket) {
3940
mockSocket.close(code);

test/angular-websocket.spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,6 @@ describe('angular-websocket', function() {
958958
it("should be automatically called on fake close", function() {
959959
var spy = jasmine.createSpy('callback');
960960
ws.onCloseCallbacks.push(spy);
961-
$websocketBackend.expectClose(url);
962961
$websocketBackend.fakeClose(url);
963962
$websocketBackend.flush();
964963
expect(spy).toHaveBeenCalled();

0 commit comments

Comments
 (0)