Skip to content

Commit

Permalink
testes api
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Chuairi Cruz committed Aug 30, 2018
1 parent 569c661 commit e2e5274
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions api/channel.test.js
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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': {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit e2e5274

Please sign in to comment.