|
1 | 1 | (function (global, factory) {
|
2 | 2 | if (typeof define === "function" && define.amd) {
|
3 |
| - define(['module', 'angular'], factory); |
| 3 | + define(["module", "angular"], factory); |
4 | 4 | } else if (typeof exports !== "undefined") {
|
5 |
| - factory(module, require('angular')); |
| 5 | + factory(module, require("angular")); |
6 | 6 | } else {
|
7 | 7 | var mod = {
|
8 | 8 | exports: {}
|
|
11 | 11 | global.angularWebsocketMock = mod.exports;
|
12 | 12 | }
|
13 | 13 | })(this, function (module, _angular) {
|
14 |
| - 'use strict'; |
| 14 | + "use strict"; |
15 | 15 |
|
16 | 16 | var _angular2 = _interopRequireDefault(_angular);
|
17 | 17 |
|
|
29 | 29 | var sendQueue = [];
|
30 | 30 | var pendingSends = [];
|
31 | 31 | var mock = false;
|
| 32 | + var existingMocks = {}; |
32 | 33 |
|
33 | 34 | function $MockWebSocket(url, protocols) {
|
| 35 | + this.url = url; |
34 | 36 | this.protocols = protocols;
|
35 | 37 | this.ssl = /(wss)/i.test(this.url);
|
| 38 | + if (!existingMocks[url]) { |
| 39 | + existingMocks[url] = [this]; |
| 40 | + } else { |
| 41 | + existingMocks[url].push(this); |
| 42 | + } |
36 | 43 | }
|
37 | 44 |
|
38 | 45 | $MockWebSocket.prototype.send = function (msg) {
|
|
45 | 52 | }
|
46 | 53 | };
|
47 | 54 |
|
| 55 | + this.mockClose = function (url, code) { |
| 56 | + if (existingMocks[url]) { |
| 57 | + existingMocks[url].map(function (mockSocket) { |
| 58 | + mockSocket.close(code); |
| 59 | + }); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
48 | 63 | this.mock = function () {
|
49 | 64 | mock = true;
|
50 | 65 | };
|
|
57 | 72 | return connectQueue.indexOf(url) > -1;
|
58 | 73 | };
|
59 | 74 |
|
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 }); |
62 | 77 | };
|
63 | 78 |
|
64 | 79 | function createWebSocketBackend(url, protocols) {
|
|
76 | 91 | this.create = createWebSocketBackend;
|
77 | 92 | this.createWebSocketBackend = createWebSocketBackend;
|
78 | 93 |
|
| 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 | + |
79 | 110 | this.flush = function () {
|
80 | 111 | var url, msg, config;
|
81 | 112 | while (url = pendingConnects.shift()) {
|
82 | 113 | var i = connectQueue.indexOf(url);
|
83 | 114 | if (i > -1) {
|
84 | 115 | connectQueue.splice(i, 1);
|
| 116 | + callOpenCallbacks(url); |
85 | 117 | }
|
86 | 118 | // if (config && config.url) {
|
87 | 119 | // }
|
88 | 120 | }
|
89 | 121 |
|
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 | + } |
92 | 129 | }
|
93 | 130 |
|
94 | 131 | while (msg = pendingSends.shift()) {
|
|
110 | 147 | // connectQueue.push({url: url, protocols: protocols});
|
111 | 148 | };
|
112 | 149 |
|
113 |
| - this.expectClose = function () { |
114 |
| - closeQueue.push(true); |
| 150 | + this.expectClose = function (url) { |
| 151 | + closeQueue.push(url); |
115 | 152 | };
|
116 | 153 |
|
117 | 154 | this.expectSend = function (msg) {
|
|
0 commit comments