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