Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved testability of socket code #88

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ server in order to test your applications:
```javascript
var $websocketBackend;

beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock');
beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock'));

beforeEach(inject(function (_$websocketBackend_) {
$websocketBackend = _$websocketBackend_;
Expand All @@ -137,8 +137,10 @@ name | arguments | description
-------------------------------|------------|-----------------------------------
flush | | Executes all pending requests
expectConnect | url:String | Specify the url of an expected WebSocket connection
expectClose | | Expect "close" to be called on the WebSocket
expectClose | url:String | Expect "close" to be called on the WebSocket
expectSend | msg:String | Expectation of send to be called, with required message
fakeClose | url:String | Used to fake close a socket and test corresponding close handlers
fakeMessage | url:String, data:object | Used to fake a backend initialized send action, which can be handled in onMessage
verifyNoOutstandingExpectation | | Makes sure all expectations have been satisfied, should be called in afterEach
verifyNoOutstandingRequest | | Makes sure no requests are pending, should be called in afterEach

Expand Down
85 changes: 76 additions & 9 deletions dist/angular-websocket-mock.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'angular'], factory);
define(["module", "angular"], factory);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the quoting style changed. Single-quote seems to be more standard.

} else if (typeof exports !== "undefined") {
factory(module, require('angular'));
factory(module, require("angular"));
} else {
var mod = {
exports: {}
Expand All @@ -11,7 +11,7 @@
global.angularWebsocketMock = mod.exports;
}
})(this, function (module, _angular) {
'use strict';
"use strict";

var _angular2 = _interopRequireDefault(_angular);

Expand All @@ -28,11 +28,19 @@
var pendingCloses = [];
var sendQueue = [];
var pendingSends = [];
var pendingMessages = [];
var mock = false;
var existingMocks = {};

function $MockWebSocket(url, protocols) {
this.url = url;
this.protocols = protocols;
this.ssl = /(wss)/i.test(this.url);
if (!existingMocks[url]) {
existingMocks[url] = [this];
} else {
existingMocks[url].push(this);
}
}

$MockWebSocket.prototype.send = function (msg) {
Expand All @@ -45,6 +53,19 @@
}
};

this.fakeClose = function (url, code) {
closeQueue.push(url);
if (existingMocks[url]) {
existingMocks[url].map(function (mockSocket) {
mockSocket.close(code);
});
}
};

this.fakeMessage = function (url, data) {
pendingMessages.push({ url: url, data: data });
};

this.mock = function () {
mock = true;
};
Expand All @@ -57,8 +78,8 @@
return connectQueue.indexOf(url) > -1;
};

$MockWebSocket.prototype.close = function () {
pendingCloses.push(true);
$MockWebSocket.prototype.close = function (code) {
pendingCloses.push({ url: this.url, code: code ? code : 1000 });
};

function createWebSocketBackend(url, protocols) {
Expand All @@ -76,19 +97,61 @@
this.create = createWebSocketBackend;
this.createWebSocketBackend = createWebSocketBackend;

function callOpenCallbacks(url) {
existingMocks[url].map(function (socketMock) {
if (socketMock.onopen && typeof socketMock.onopen === "function") {
socketMock.onopen();
}
});
}

function callCloseCallbacks(url, code) {
existingMocks[url].map(function (socketMock) {
if (socketMock.onclose && typeof socketMock.onclose === "function") {
socketMock.onclose({ code: code });
}
});
}

function callMessageCallbacks(url, data) {
if (existingMocks[url]) {
existingMocks[url].map(function (socketMock) {
if (socketMock.onmessage && typeof socketMock.onmessage === "function") {
socketMock.onmessage({ data: JSON.stringify(data) });
}
});
}
}

function setReadyState(url, state) {
if (existingMocks[url]) {
existingMocks[url].map(function (socketMock) {
socketMock.readyState = state;
});
}
}

this.flush = function () {
var url, msg, config;
while (url = pendingConnects.shift()) {
var i = connectQueue.indexOf(url);
if (i > -1) {
connectQueue.splice(i, 1);
callOpenCallbacks(url);
setReadyState(url, 1);
}
// if (config && config.url) {
// }
}

while (pendingCloses.shift()) {
closeQueue.shift();
var pendingClose;
while (pendingClose = pendingCloses.shift()) {
var i = closeQueue.indexOf(pendingClose.url);
if (i > -1) {
closeQueue.splice(i, 1);
callCloseCallbacks(pendingClose.url, pendingClose.code);
setReadyState(pendingClose.url, 3);
}
}

while (msg = pendingSends.shift()) {
Expand All @@ -103,15 +166,19 @@
sendQueue.splice(j, 1);
}
}

while (msg = pendingMessages.shift()) {
callMessageCallbacks(msg.url, msg.data);
}
};

this.expectConnect = function (url, protocols) {
connectQueue.push(url);
// connectQueue.push({url: url, protocols: protocols});
};

this.expectClose = function () {
closeQueue.push(true);
this.expectClose = function (url) {
closeQueue.push(url);
};

this.expectSend = function (msg) {
Expand Down
19 changes: 8 additions & 11 deletions dist/angular-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
};
}

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
};

var Socket;

if (typeof window === 'undefined') {
Expand Down Expand Up @@ -148,6 +142,13 @@
};

$WebSocket.prototype._connect = function _connect(force) {
if (this.socket) {
this.socket.onmessage = null;
this.socket.onopen = null;
this.socket.onerror = null;
this.socket.onclose = null;
this.socket.close();
}
if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
this.socket = $websocketBackend.create(this.url, this.protocols);
this.socket.onmessage = _angular2.default.bind(this, this._onMessageHandler);
Expand Down Expand Up @@ -320,10 +321,6 @@
return self;
}

if ($websocketBackend.isMocked && $websocketBackend.isMocked() && $websocketBackend.isConnected(this.url)) {
this._onMessageHandler($websocketBackend.mockSend());
}

return promise;
};

Expand Down Expand Up @@ -412,4 +409,4 @@

exports.default = _angular2.default.module('ngWebSocket');
module.exports = exports['default'];
});
});
Loading