Skip to content

Commit f432ac2

Browse files
author
Christian Jeschke
committed
added fakeMessage method and rename mockClose to fakeClose to make intention more clear
1 parent 43d5ed3 commit f432ac2

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ flush | | Executes all pending requests
139139
expectConnect | url:String | Specify the url of an expected WebSocket connection
140140
expectClose | url:String | Expect "close" to be called on the WebSocket
141141
expectSend | msg:String | Expectation of send to be called, with required message
142-
mockClose | url:String | Used to fake close a socket and test corresponding close handlers
142+
fakeClose | url:String | Used to fake close a socket and test corresponding close handlers
143+
fakeMessage | url:String, data:object | Used to fake a backend initialized send action, which can be handled in onMessage
143144
verifyNoOutstandingExpectation | | Makes sure all expectations have been satisfied, should be called in afterEach
144145
verifyNoOutstandingRequest | | Makes sure no requests are pending, should be called in afterEach
145146

dist/angular-websocket-mock.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
var pendingCloses = [];
2929
var sendQueue = [];
3030
var pendingSends = [];
31+
var pendingMessages = [];
3132
var mock = false;
3233
var existingMocks = {};
3334

@@ -52,14 +53,18 @@
5253
}
5354
};
5455

55-
this.mockClose = function (url, code) {
56+
this.fakeClose = function (url, code) {
5657
if (existingMocks[url]) {
5758
existingMocks[url].map(function (mockSocket) {
5859
mockSocket.close(code);
5960
});
6061
}
6162
};
6263

64+
this.fakeMessage = function (url, data) {
65+
pendingMessages.push({ url: url, data: data });
66+
};
67+
6368
this.mock = function () {
6469
mock = true;
6570
};
@@ -107,6 +112,16 @@
107112
});
108113
}
109114

115+
function callMessageCallbacks(url, data) {
116+
if (existingMocks[url]) {
117+
existingMocks[url].map(function (socketMock) {
118+
if (socketMock.onmessage && typeof socketMock.onmessage === "function") {
119+
socketMock.onmessage(data);
120+
}
121+
});
122+
}
123+
}
124+
110125
this.flush = function () {
111126
var url, msg, config;
112127
while (url = pendingConnects.shift()) {
@@ -140,6 +155,10 @@
140155
sendQueue.splice(j, 1);
141156
}
142157
}
158+
159+
while (msg = pendingMessages.shift()) {
160+
callMessageCallbacks(msg.url, msg.data);
161+
}
143162
};
144163

145164
this.expectConnect = function (url, protocols) {

src/angular-websocket-mock.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function $WebSocketBackend() {
77
var pendingCloses = [];
88
var sendQueue = [];
99
var pendingSends = [];
10+
var pendingMessages = [];
1011
var mock = false;
1112
var existingMocks = {};
1213

@@ -32,14 +33,18 @@ function $WebSocketBackend() {
3233
}
3334
};
3435

35-
this.mockClose = function(url, code) {
36+
this.fakeClose = function(url, code) {
3637
if(existingMocks[url]) {
3738
existingMocks[url].map(function(mockSocket) {
3839
mockSocket.close(code);
3940
});
4041
}
4142
};
4243

44+
this.fakeMessage = function(url, data) {
45+
pendingMessages.push({url: url, data: data});
46+
};
47+
4348
this.mock = function() {
4449
mock = true;
4550
};
@@ -87,6 +92,16 @@ function $WebSocketBackend() {
8792
});
8893
}
8994

95+
function callMessageCallbacks(url, data) {
96+
if(existingMocks[url]) {
97+
existingMocks[url].map(function(socketMock) {
98+
if(socketMock.onmessage && typeof socketMock.onmessage === "function") {
99+
socketMock.onmessage(data);
100+
}
101+
});
102+
}
103+
}
104+
90105
this.flush = function () {
91106
var url, msg, config;
92107
while (url = pendingConnects.shift()) {
@@ -120,6 +135,10 @@ function $WebSocketBackend() {
120135
sendQueue.splice(j, 1);
121136
}
122137
}
138+
139+
while (msg = pendingMessages.shift()) {
140+
callMessageCallbacks(msg.url, msg.data);
141+
}
123142
};
124143

125144
this.expectConnect = function (url, protocols) {

test/angular-websocket.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,11 +943,19 @@ describe('angular-websocket', function() {
943943
expect(spy).toHaveBeenCalled();
944944
});
945945

946-
it("should be automatically called on mock close", function() {
946+
it("should be automatically called on fake close", function() {
947947
var spy = jasmine.createSpy('callback');
948948
ws.onCloseCallbacks.push(spy);
949949
$websocketBackend.expectClose(url);
950-
$websocketBackend.mockClose(url);
950+
$websocketBackend.fakeClose(url);
951+
$websocketBackend.flush();
952+
expect(spy).toHaveBeenCalled();
953+
});
954+
955+
it("should be automatically called on fake send", function() {
956+
var spy = jasmine.createSpy('callback');
957+
ws.onMessage(spy);
958+
$websocketBackend.fakeMessage(url, {data: "test"});
951959
$websocketBackend.flush();
952960
expect(spy).toHaveBeenCalled();
953961
});

0 commit comments

Comments
 (0)