Skip to content

Commit

Permalink
Comandos diferentes pra rodar testes front e back
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Chuairi Cruz committed Aug 30, 2018
1 parent a17c512 commit 13f32d3
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ Websocket -> `localhost:3001`

Para rodar os testes da parte client:

`yarn test`
`yarn test:client`

Para rodar os testes da api:

`yarn test:api`

4 changes: 2 additions & 2 deletions api/channel.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* global require module */
var uuidv4 = require('uuid/v4')
var uuid = require('uuid')

module.exports = () => {
const channels = {}

const createChannel = (socket, io, name) => {
let channel = {
id: uuidv4(),
id: uuid.v4(),
name: name,
msgs: []
}
Expand Down
99 changes: 99 additions & 0 deletions api/channel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* global describe it require expect beforeEach spyOn */
const uuid = require('uuid')
const sinon = require('sinon')
const channelManager = require('./channel')()

let ioFunc = () => {
const emit = () => {return this}
const to = () => {return this}
return {emit, to}
}
let io = ioFunc()

let socketFunc = () => {
const emit = () => {return this}
const join = () => {return this}
const rooms = {}
return {emit, join, rooms}
}
let socket = socketFunc()

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': {
msgs: [],
joined: true,
id: '1-2-3-4',
name: 'my channel'
}
})
})

it('should join room', () => {
spyOn(socket, 'emit')
spyOn(socket, 'join')
spyOn(io, 'emit')
channelManager.createChannel(socket, io, 'my channel')
expect(socket.emit).toHaveBeenCalledWith('joined channel', {id: '1-2-3-4', joined: true, msgs: [], name: 'my channel'})
expect(socket.join).toHaveBeenCalledWith('1-2-3-4')
expect(io.emit).toHaveBeenCalledWith('channels', channelManager.channels)
})
})

// describe('sendMessage method', () => {
// it('should not send message if room doesnt exist', () => {
// spyOn(io, 'to')
// spyOn(io, 'emit')
// socket.rooms[1] = true
// channelManager.sendMessage(socket, io, {channelID: '1', msg: 'Hello!'})
// expect(io.to).toBeCalledWith('1')
// expect(io.emit).toBeCalledWith('message', {channelID: '1', msg: 'Hello!'})
// // channels[channelID].msgs.push(msg)
// })
// })

// describe('joinChannel method', () => {
// })

// describe('leaveChannel method', () => {
// })

describe('getChannel method', () => {
it('should emit got channel', () => {
spyOn(socket, 'emit')
channelManager.channels['123'] = {
id: 1-2-3-4,
joined: false,
msgs: [],
name: "my channel"
}
channelManager.getChannel(socket, '123')
expect(socket.emit).toHaveBeenCalledWith('got channel', {
id: 1-2-3-4,
joined: false,
msgs: [],
name: "my channel"
})
})

it('should emit got channel with joined true', () => {
spyOn(socket, 'emit')
channelManager.channels['123'] = {
id: 1-2-3-4,
joined: false,
msgs: [],
name: "my channel"
}
socket.rooms['123'] = true
channelManager.getChannel(socket, '123')
expect(socket.emit).toHaveBeenCalledWith('got channel', {
id: 1-2-3-4,
joined: true,
msgs: [],
name: "my channel"
})
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"scripts": {
"start": "react-scripts start | node api/server.js",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"test:client": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"sass": "sass --watch src/styles/scss:src/styles/css",
"test:api": "jest api/*test.js",
"postinstall": "yarn build"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 13f32d3

Please sign in to comment.