Skip to content

Commit e97847e

Browse files
author
Christian Jeschke
committed
implemented mockClose and automatic listener notification for better testing
1 parent ab83d79 commit e97847e

File tree

6 files changed

+130
-28
lines changed

6 files changed

+130
-28
lines changed

dist/angular-websocket-mock.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (global, factory) {
22
if (typeof define === "function" && define.amd) {
3-
define(['module', 'angular'], factory);
3+
define(["module", "angular"], factory);
44
} else if (typeof exports !== "undefined") {
5-
factory(module, require('angular'));
5+
factory(module, require("angular"));
66
} else {
77
var mod = {
88
exports: {}
@@ -11,7 +11,7 @@
1111
global.angularWebsocketMock = mod.exports;
1212
}
1313
})(this, function (module, _angular) {
14-
'use strict';
14+
"use strict";
1515

1616
var _angular2 = _interopRequireDefault(_angular);
1717

@@ -29,10 +29,17 @@
2929
var sendQueue = [];
3030
var pendingSends = [];
3131
var mock = false;
32+
var existingMocks = {};
3233

3334
function $MockWebSocket(url, protocols) {
35+
this.url = url;
3436
this.protocols = protocols;
3537
this.ssl = /(wss)/i.test(this.url);
38+
if (!existingMocks[url]) {
39+
existingMocks[url] = [this];
40+
} else {
41+
existingMocks[url].push(this);
42+
}
3643
}
3744

3845
$MockWebSocket.prototype.send = function (msg) {
@@ -45,6 +52,14 @@
4552
}
4653
};
4754

55+
this.mockClose = function (url, code) {
56+
if (existingMocks[url]) {
57+
existingMocks[url].map(function (mockSocket) {
58+
mockSocket.close(code);
59+
});
60+
}
61+
};
62+
4863
this.mock = function () {
4964
mock = true;
5065
};
@@ -57,8 +72,8 @@
5772
return connectQueue.indexOf(url) > -1;
5873
};
5974

60-
$MockWebSocket.prototype.close = function () {
61-
pendingCloses.push(true);
75+
$MockWebSocket.prototype.close = function (code) {
76+
pendingCloses.push({ url: this.url, code: code ? code : 1000 });
6277
};
6378

6479
function createWebSocketBackend(url, protocols) {
@@ -76,19 +91,41 @@
7691
this.create = createWebSocketBackend;
7792
this.createWebSocketBackend = createWebSocketBackend;
7893

94+
function callOpenCallbacks(url) {
95+
existingMocks[url].map(function (socketMock) {
96+
if (socketMock.onopen && typeof socketMock.onopen === "function") {
97+
socketMock.onopen();
98+
}
99+
});
100+
}
101+
102+
function callCloseCallbacks(url, code) {
103+
existingMocks[url].map(function (socketMock) {
104+
if (socketMock.onclose && typeof socketMock.onclose === "function") {
105+
socketMock.onclose({ code: code });
106+
}
107+
});
108+
}
109+
79110
this.flush = function () {
80111
var url, msg, config;
81112
while (url = pendingConnects.shift()) {
82113
var i = connectQueue.indexOf(url);
83114
if (i > -1) {
84115
connectQueue.splice(i, 1);
116+
callOpenCallbacks(url);
85117
}
86118
// if (config && config.url) {
87119
// }
88120
}
89121

90-
while (pendingCloses.shift()) {
91-
closeQueue.shift();
122+
var pendingClose;
123+
while (pendingClose = pendingCloses.shift()) {
124+
var i = closeQueue.indexOf(pendingClose.url);
125+
if (i > -1) {
126+
closeQueue.splice(i, 1);
127+
callCloseCallbacks(pendingClose.url, pendingClose.code);
128+
}
92129
}
93130

94131
while (msg = pendingSends.shift()) {
@@ -110,8 +147,8 @@
110147
// connectQueue.push({url: url, protocols: protocols});
111148
};
112149

113-
this.expectClose = function () {
114-
closeQueue.push(true);
150+
this.expectClose = function (url) {
151+
closeQueue.push(url);
115152
};
116153

117154
this.expectSend = function (msg) {

dist/angular-websocket.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
};
2626
}
2727

28-
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
29-
return typeof obj;
30-
} : function (obj) {
31-
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
32-
};
33-
3428
var Socket;
3529

3630
if (typeof window === 'undefined') {
@@ -412,4 +406,4 @@
412406

413407
exports.default = _angular2.default.module('ngWebSocket');
414408
module.exports = exports['default'];
415-
});
409+
});

dist/angular-websocket.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)