Skip to content

Commit

Permalink
trying to test socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
moshmage committed Dec 18, 2016
1 parent ef2bf4f commit 262cac3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.2",
"karma-typescript": "https://github.com/monounity/karma-typescript",
"socket.io": "^1.7.2",
"typescript": "^2.1.4"
}
}
80 changes: 62 additions & 18 deletions src/subjects/socket-io.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
*/
import {IO} from './socket-io';
import {ioEvent} from './io-events';
import {initialState} from "./../interfaces/socket-io";
import {assign} from "rxjs/util/assign";
const socketIO = require('socket.io');

function setUpTestServer() {
return socketIO.listen(1337).on('connection', (socket) => {
socket.on('test-event',(socket)=> {
socket.emit('test-event', {data: true});
})
});
}
setUpTestServer();

describe('IO', () => {
let socket = new IO();

it ('is instance of itself', () => {
let socket = new IO();
expect(socket instanceof IO).toBe(true);
});

it('initialValues are set', () => {
expect(socket.socketState.connected).toBe(false);
});

describe('listenEvent & eventExists', () => {
let socket = new IO();
let eventCount = 0;
let event = new ioEvent({name: 'test-event', once: false, count: 0});

Expand Down Expand Up @@ -46,25 +51,64 @@ describe('IO', () => {
});

describe('Public coverage', () => {
it('socketState', () => {
expect(socket.socketState).toEqual(initialState);
});

it("raw", () => {
let spySocket = new IO();
expect(socket.raw).toBeFalsy();
expect(spySocket.raw).toBeFalsy();
spyOn(spySocket, 'connected').and.returnValue(true);
expect(spySocket.raw).toBeUndefined(); /** is undefined because .connect() wasn't issued */

});

it('socketState updating', () => {
let event$ = socket.event$;
let subscription = event$.subscribe((newValue) => {
/** I'm not reeeeaally sure this is how you test this. */
expect(newValue).toEqual({connected: false});
it('socketState updating to true', () => {
let spySocket = new IO();
let event$ = spySocket.event$;
spySocket.connected = true;

event$.subscribe((newValue) => {
expect(newValue).toEqual({connected: true});
});
socket.connected = false;
subscription.unsubscribe();
});

it ('SocketState updating to false', () => {
let spySocket = new IO();
let event$ = spySocket.event$;
assign(spySocket, {
_connected: true,
socket: {disconnect() {}}
});
spyOn(spySocket.socket, 'disconnect').and.callThrough();

event$.subscribe((newValue) => {
expect({connected: false}).toEqual(newValue);
}).unsubscribe();

spySocket.connected = false;
});

it('Emit', () => {
let spySocket = new IO();
spySocket.connected = true;
assign(spySocket, {socket: {emit() {}}});

spyOn(spySocket.socket, 'emit').and.callThrough();

spySocket.emit('test-event',{});
expect(spySocket.socket.emit.calls.count()).toBe(1);

});
});

describe('Connection', () => {
let socket = new IO();

it('connects', () => {
console.log()
socket.event$.subscribe((newData)=>{
console.log('newData', newData);
expect(newData).toContain({connected: true})
}).unsubscribe();
socket.connect('localhost:1337');
});
})
});
4 changes: 2 additions & 2 deletions src/subjects/socket-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class IO {
public connect(address?: string, forceNew?:boolean) :void {
if (this.connected && !forceNew) return;
else if (this.connected && forceNew) this.connected = false;

this.socket = io(address || SOCKET_URL);
this.socket.on('connect', () => {
this.connected = true;
Expand Down Expand Up @@ -102,5 +102,5 @@ export class IO {
this._connected = value;
this._socketState.next({connected: value});
};

}

0 comments on commit 262cac3

Please sign in to comment.