Skip to content
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
22 changes: 11 additions & 11 deletions ng-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
return wsp;
};

wsp.$get = ['$http', function ($http) {
return new $websocketService(wsp.$$config, $http);
wsp.$get = ['$http', '$timeout', '$interval', function ($http, $timeout, $interval) {
return new $websocketService(wsp.$$config, $http, $timeout, $interval);
}];
}

Expand All @@ -39,7 +39,7 @@
* @description
* HTML5 Websocket service for AngularJS
*/
function $websocketService (cfg, $http) {
function $websocketService (cfg, $http, $timeout, $interval) {
var wss = this;

wss.$$websocketList = {};
Expand Down Expand Up @@ -68,7 +68,7 @@
if (typeof ws === 'undefined') {
var wsCfg = angular.extend({}, wss.$$config, cfg);

ws = new $websocket(wsCfg, $http);
ws = new $websocket(wsCfg, $http, $timeout, $interval);
wss.$$websocketList[wsCfg.url] = ws;
}

Expand All @@ -83,7 +83,7 @@
* @description
* HTML5 Websocket wrapper class for AngularJS
*/
function $websocket (cfg, $http) {
function $websocket (cfg, $http, $timeout, $interval) {
var me = this;

if (typeof cfg === 'undefined' || (typeof cfg === 'object' && typeof cfg.url === 'undefined')) throw new Error('An url must be specified for WebSocket');
Expand Down Expand Up @@ -119,7 +119,7 @@
};

me.$$init = function (cfg) {
me.$$ws = cfg.mock ? new $$mockWebsocket(cfg.mock, $http) : new WebSocket(cfg.url, cfg.protocols);
me.$$ws = cfg.mock ? new $$mockWebsocket(cfg.mock, $http, $timeout, $interval) : new WebSocket(cfg.url, cfg.protocols);

me.$$ws.onmessage = function (message) {
try {
Expand Down Expand Up @@ -265,7 +265,7 @@
return me;
}

function $$mockWebsocket (cfg, $http) {
function $$mockWebsocket (cfg, $http, $timeout, $interval) {
cfg = cfg || {};

var me = this,
Expand Down Expand Up @@ -294,7 +294,7 @@
if (me.readyState === me.OPEN) {
me.readyState = me.CLOSING;

setTimeout(function () {
$timeout(function () {
me.readyState = me.CLOSED;

me.onclose();
Expand All @@ -309,7 +309,7 @@
me.onopen = function () {};
me.onclose = function () {};

setInterval(function () {
$interval(function () {
if (messageQueue.length > 0) {
var message = messageQueue.shift(),
msgObj = JSON.parse(message);
Expand Down Expand Up @@ -340,7 +340,7 @@

fixtures = fixs;

setTimeout(function () {
$timeout(function () {
me.readyState = me.OPEN;
me.onopen();
}, openTimeout);
Expand All @@ -367,4 +367,4 @@
angular
.module('ngWebsocket', [])
.provider('$websocket', $websocketProvider);
})();
})();
8 changes: 5 additions & 3 deletions test/unit/ng-websocket-provider-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var $websocketProvider, $http;
var $websocketProvider, $http, $timeout, $interval;

describe('Testing ng-websocket-provider', function () {
beforeEach(function () {
Expand All @@ -12,8 +12,10 @@ describe('Testing ng-websocket-provider', function () {

module('ngWebsocket', 'test');

inject(function (_$http_) {
inject(function (_$http_, _$timeout_, _$interval_) {
$http = _$http_;
$timeout = _$timeout_;
$interval = _$interval_;
});
});

Expand All @@ -40,7 +42,7 @@ describe('Testing ng-websocket-provider', function () {
mock: true
});

var $websocket = $websocketProvider.$get[$websocketProvider.$get.length - 1]($http),
var $websocket = $websocketProvider.$get[$websocketProvider.$get.length - 1]($http,$timeout,$interval),
ws = $websocket.$new('ws://localhost:12345'),
ws2 = $websocket.$new('ws://localhost:44444');

Expand Down
91 changes: 66 additions & 25 deletions test/unit/ng-websocket-spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

var $websocket, $httpBackend;
var $websocket, $httpBackend, $timeout, $interval;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;

describe('Testing ng-websocket', function () {
beforeEach(module('ngWebsocket'));
beforeEach(inject(function (_$websocket_, _$httpBackend_) {
beforeEach(inject(function (_$websocket_, _$httpBackend_, _$timeout_, _$interval_) {
$websocket = _$websocket_;
$httpBackend = _$httpBackend_;
$timeout = _$timeout_;
$interval = _$interval_;
}));

describe('Testing an offline $websocket', function () {
Expand Down Expand Up @@ -57,6 +59,8 @@ describe('Testing ng-websocket', function () {
ws.$on('$open', function () {
done();
});
$timeout.flush();
$interval.flush();
});

afterEach(function () {
Expand Down Expand Up @@ -107,6 +111,8 @@ describe('Testing ng-websocket', function () {
ws.$on('$open', function () {
done();
});
$timeout.flush();
$interval.flush();
});

afterEach(function () {
Expand All @@ -122,6 +128,7 @@ describe('Testing ng-websocket', function () {

done();
});
$interval.flush(2001);
});

it('should have received the same message on the same event', function (done) {
Expand All @@ -132,6 +139,7 @@ describe('Testing ng-websocket', function () {

done();
});
$interval.flush(2001);
});
});

Expand All @@ -147,6 +155,8 @@ describe('Testing ng-websocket', function () {
ws.$on('$open', function () {
done();
});
$timeout.flush();
$interval.flush();
});

afterEach(function () {
Expand All @@ -155,12 +165,13 @@ describe('Testing ng-websocket', function () {

it('should set a listener on $close general event', function (done) {
ws.$close();

ws.$on('$close', function () {
expect(ws.$status()).toEqual(ws.$CLOSED);

done();
});
$timeout.flush();
$interval.flush(2001);
});

it('should set a list of listeners on $close general event', function (done) {
Expand All @@ -185,8 +196,11 @@ describe('Testing ng-websocket', function () {
counter++;

expect(counter).toEqual(listeners);

done();
});
$timeout.flush();
$interval.flush(2001);
});

it('should unset a listener on $close general event', function (done) {
Expand All @@ -197,22 +211,23 @@ describe('Testing ng-websocket', function () {
noUpdate = false;
});

setTimeout(function () {
expect(noUpdate).toBeTruthy();
done();
}, 4000);

ws.$un('$close');
$timeout.flush();
$interval.flush(2001);

expect(noUpdate).toBeTruthy();
done();
});

it('should emit a custom event', function (done) {
ws.$emit('custom event', 'hello world');

ws.$on('custom event', function (message) {
expect(message).toEqual('hello world');

done();
});
$interval.flush(2001);
});
});

Expand Down Expand Up @@ -273,6 +288,8 @@ describe('Testing ng-websocket', function () {
ws.$on('$open', function () {
done();
});
$timeout.flush();
$interval.flush();
});

afterEach(function () {
Expand Down Expand Up @@ -306,6 +323,8 @@ describe('Testing ng-websocket', function () {
expect(message.data).toEqual('hello lazy world');
done();
});

$interval.flush(2001);
});
});

Expand All @@ -329,20 +348,28 @@ describe('Testing ng-websocket', function () {
ws.$close();
});

it('should enqueue the message and flush it when is ready', function (done) {
it('should enqueue the message and flush it when is ready', function () {
var done = false;
ws.$on('my event', function (msg) {
expect(ws.$ready()).toBeTruthy();
expect(msg).toEqual('hello world');
done();
done = true;
});

ws.$emit('my event', 'hello world');
setTimeout(function () {
ws.$open();
}, 2500);
});

it('should enqueue every message and flush all of them when is ready', function (done) {

$interval.flush(501);
expect(done).toBe(false);

ws.$open();
$timeout.flush();
$interval.flush(501);

expect(done).toBe(true);
});

it('should enqueue every message and flush all of them when is ready', function () {
var done = false;
var counter = 5,
eventCounter = 0;

Expand All @@ -359,16 +386,21 @@ describe('Testing ng-websocket', function () {

expect(eventCounter).toEqual(counter);

done();
done = true;
});

for (var i = 0; i < counter; i++) ws.$emit('my event', 'Data #' + i);

ws.$emit('another event', 'hello world');

$interval.flush(501);
expect(done).toBe(false);

setTimeout(function () {
ws.$open();
}, 500);
ws.$open();
$timeout.flush();
$interval.flush(3001);

expect(done).toBe(true);
});
});

Expand Down Expand Up @@ -399,13 +431,15 @@ describe('Testing ng-websocket', function () {
ws.$on('$open', function () {
done();
});
$timeout.flush();
$interval.flush();
});

afterEach(function () {
ws.$close();
});

it('should responde with fixtures data', function (done) {
it('should respond with fixtures data', function (done) {
ws.$on('custom event', function (msg) {
expect(msg).toBeDefined();
expect(msg.hello).toEqual('world');
Expand All @@ -414,20 +448,24 @@ describe('Testing ng-websocket', function () {
});

ws.$emit('custom event');

$interval.flush(2001);
});

it('should responde with fixtures data on a fixture event', function (done) {
it('should respond with fixtures data on a fixture event', function (done) {
ws.$on('fixture response event', function (msg) {
expect(msg).toBeDefined();
expect(msg.fixture).toEqual('response data');
done();
});

ws.$emit('fixture event');

$interval.flush(2001);
});
});

describe('Testing mock remote fixtures feature', function () {
xdescribe('Testing mock remote fixtures feature', function () {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Why did you turn off this test?

var ws;

it('should make an HTTP GET request and retrieve fixtures', function (done) {
Expand Down Expand Up @@ -455,6 +493,9 @@ describe('Testing ng-websocket', function () {

done();
});

$timeout.flush();
$interval.flush(2001);

$httpBackend.flush();
});
Expand Down