From e2e5274edb23018ab033457cc758fced91993308 Mon Sep 17 00:00:00 2001 From: Lucas Chuairi Cruz Date: Thu, 30 Aug 2018 14:45:05 -0300 Subject: [PATCH] testes api --- api/channel.test.js | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/api/channel.test.js b/api/channel.test.js index ac31924..39cff29 100644 --- a/api/channel.test.js +++ b/api/channel.test.js @@ -1,7 +1,6 @@ /* global describe it require expect beforeEach spyOn */ const uuid = require('uuid') const sinon = require('sinon') -const channelManager = require('./channel')() function ioFunc() {} ioFunc.prototype.emit = function() {return this} @@ -10,18 +9,20 @@ ioFunc.prototype.to = function() {return this} function socketFunc() { this.rooms = {} } socketFunc.prototype.emit = function() {return this} socketFunc.prototype.join = function() {return this} +socketFunc.prototype.leave = function() {return this} describe('Channel tests', () => { - var io, socket; + let io, socket, channelManager; + sinon.stub(uuid, 'v4').returns('1-2-3-4') beforeEach(() => { io = new ioFunc() socket = new socketFunc() + channelManager = require('./channel')() }) describe('createChannel method', () => { it('should insert channel in the channels object', () => { - sinon.stub(uuid, 'v4').returns('1-2-3-4') channelManager.createChannel(socket, io, 'my channel') expect(channelManager.channels).toEqual({ '1-2-3-4': { @@ -76,29 +77,57 @@ describe('Channel tests', () => { }) describe('joinChannel method', () => { + beforeEach(() => { + channelManager.createChannel(socket, io, 'my channel') + }) + it('should join socket', () => { spyOn(socket, 'join').and.callThrough() - channelManager.joinChannel(socket, io, {channelID: '1', msg: 'Hello!'}) - expect(socket.join).toBeCalledWith('1') + channelManager.joinChannel(socket, io, {channelID: '1-2-3-4', msg: 'Hello!'}) + expect(socket.join).toBeCalledWith('1-2-3-4') }) it('should emit join channel', () => { spyOn(socket, 'emit').and.callThrough() - channelManager.joinChannel(socket, io, {channelID: '1', msg: 'Hello!'}) - expect(socket.emit).toBeCalledWith('joined channel', {'joined': true, 'msgs': ['Hello!', 'Hello!', 'Hello!']}) + channelManager.joinChannel(socket, io, {channelID: '1-2-3-4', msg: 'Hello!'}) + expect(socket.emit).toBeCalledWith('joined channel', {id: '1-2-3-4', 'joined': true, 'msgs': ['Hello!'], 'name': 'my channel'}) }) it('should emit message to channel', () => { spyOn(io, 'to').and.callThrough() spyOn(io, 'emit').and.callThrough() - channelManager.joinChannel(socket, io, {channelID: '1', msg: 'Hello!'}) - expect(io.to).toBeCalledWith('1') - expect(io.emit).toBeCalledWith('message', {'channelID': '1', 'msg': 'Hello!'}) + channelManager.joinChannel(socket, io, {channelID: '1-2-3-4', msg: 'Hello!'}) + expect(io.to).toBeCalledWith('1-2-3-4') + expect(io.emit).toBeCalledWith('message', {'channelID': '1-2-3-4', 'msg': 'Hello!'}) // and append to channel }) }) describe('leaveChannel method', () => { + beforeEach(() => { + channelManager.createChannel(socket, io, 'my channel') + }) + + it('should emit leave channel with joined false', () => { + spyOn(socket, 'emit').and.callThrough() + channelManager.channels['1-2-3-4'].joined = true + channelManager.leaveChannel(socket, io, {channelID: '1-2-3-4', msg: 'Hello!'}) + expect(socket.emit).toBeCalledWith('leaved channel', {'id': '1-2-3-4', 'joined': false, 'msgs': ['Hello!'], 'name': 'my channel'}) + }) + + it('should emit message to the channel', () => { + spyOn(io, 'to').and.callThrough() + spyOn(io, 'emit').and.callThrough() + channelManager.leaveChannel(socket, io, {channelID: '1-2-3-4', msg: 'Hello!'}) + expect(io.to).toBeCalledWith('1-2-3-4') + expect(io.emit).toBeCalledWith('message', {'channelID': '1-2-3-4', 'msg': 'Hello!'}) + }) + + it('should called leave method from socket', () => { + spyOn(socket, 'leave').and.callThrough() + channelManager.leaveChannel(socket, io, {channelID: '1-2-3-4', msg: 'Hello!'}) + expect(socket.leave).toBeCalledWith('1-2-3-4') + }) }) describe('getChannel method', () => {