-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathangular-socket.io-mock.spec.js
65 lines (61 loc) · 2.84 KB
/
angular-socket.io-mock.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* global expect: false, it: false, describe: false */
'use strict';
describe('Angular Socket.io Mock', function () {
var socketFactory;
var $rootScope;
beforeEach(module('btford.socket-io'))
beforeEach(inject(function (_socketFactory_, _$rootScope_) {
socketFactory = _socketFactory_
$rootScope = _$rootScope_;
}));
it('should be able to listen on an event', function () {
expect(new socketFactory().on('test-event', function () {
})).not.toBe(false)
});
it('should be able to listen once event', function () {
expect(new socketFactory().once('test-event', function () {
})).not.toBe(false)
});
it('should be able to emit an event', function () {
expect(new socketFactory().emit('test-event', {})).not.toBe(false)
});
it('should be able to receive an event', function () {
expect(new socketFactory().receive('test-event', {})).not.toBe(false)
});
it('should be able to acknowledge an emited event only once', function (done) {
var socket = new socketFactory();
var timesCalled = 0;
socket.emit('test-event', {}, function (resp) {
expect(resp).not.toBe(false);
expect(++timesCalled).toEqual(1);
});
socket.receive('test-event', {});
socket.receive('test-event', {});
setTimeout(function () { // Wait to see if the event was acknowledged twice
done();
}, 100);
});
it('should be able to forward an event with default prefix', function () {
var socket = new socketFactory();
spyOn($rootScope, "$broadcast");
socket.forward('test-event');
socket.receive('test-event', {'test-data': 'test-value'});
expect($rootScope.$broadcast).toHaveBeenCalledWith('socket:test-event', {'test-data': 'test-value'});
});
it('should be able to forward an event with custom prefix', function () {
var socket = new socketFactory({prefix: 'test-prefix:'});
spyOn($rootScope, "$broadcast");
socket.forward('test-event');
socket.receive('test-event', {'test-data': 'test-value'});
expect($rootScope.$broadcast).toHaveBeenCalledWith('test-prefix:test-event', {'test-data': 'test-value'});
});
it('should be able to forward events', function () {
var socket = new socketFactory({prefix: 'test-prefix:'});
spyOn($rootScope, "$broadcast").and.callThrough;
socket.forward(['test-event1', 'test-event2']);
socket.receive('test-event1', {'test-data1': 'test-value1'});
socket.receive('test-event2', {'test-data2': 'test-value2'});
expect($rootScope.$broadcast).toHaveBeenCalledWith('test-prefix:test-event1', {'test-data1': 'test-value1'});
expect($rootScope.$broadcast).toHaveBeenCalledWith('test-prefix:test-event2', {'test-data2': 'test-value2'});
});
})