Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaLearnsToCode committed Jul 8, 2019
1 parent ee44223 commit 659eeee
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 27 deletions.
24 changes: 12 additions & 12 deletions controllers/chats.js
Expand Up @@ -15,22 +15,22 @@ function indexRoute(req, res) {
// SHOW
function showRoute(req, res) {
req.body.user = req.currentUser

const lang = req.currentUser.lang === 'vi' ? 'en-vi' : 'vi-en'
Chat
.findById(req.params.chatId)
.then(chat => {
if (req.currentUser.lang === 'vi') {
chat.comments.map(comment => {
axios.get(encodeURI(`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${key}&text=${comment.text}&lang=en-vi`))
.then(comment => {
console.log(comment.data.text.join('+'))
})
})
}
console.log(chat.comments)
return chat.comments
return Promise.all([chat, ...chat.comments.map(comment => {
return axios.get(encodeURI(`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${key}&text=${comment.text}&lang=${lang}`))
})])

})
.then(values => {
const [ chat, ...comments ] = values
chat.comments.forEach((comment, index) => {
comment.text = comments[index].data.text[0]
})
res.json(chat)
})
.then(chat => res.status(200).json(chat))
.catch(err => res.status(400).json(err))
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -47,6 +47,7 @@
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"spectre.css": "^0.5.8",
"supertest": "^4.0.2"
},
"repository": "git@github.com:davt49/sei-group-project.git",
Expand Down
23 changes: 10 additions & 13 deletions test/controllers/chats-test.js
Expand Up @@ -8,7 +8,6 @@ const jwt = require('jsonwebtoken')
const { secret } = require('../../config/environment')

describe('Chat test', () => {
let user = ''
let token = ''

beforeEach(done => {
Expand All @@ -24,7 +23,6 @@ describe('Chat test', () => {
userType: 'Local'
})
.then(userData => {
user = userData
token = jwt.sign({ sub: userData._id }, secret, { expiresIn: '10d' })
done()
})
Expand Down Expand Up @@ -133,18 +131,18 @@ describe('Chat test', () => {

describe('GET /api/chats/:id', () => {

let chat
let chat = {}

beforeEach(done => {
Chat.create([{
Chat.create({
title: 'Hotels',
image: 'https://dynaimage.cdn.cnn.com/cnn/q_auto,w_412,c_fill,g_auto,h_232,ar_16:9/http%3A%2F%2Fcdn.cnn.com%2Fcnnnext%2Fdam%2Fassets%2F170606122114-vietnam---travel-destination--shutterstock-168342398.jpg',
comments: []
}])
})
.then(chatData => {
chat = chatData
done()
return chat = chatData
})
.then(() => done())
.catch(done)
})

Expand All @@ -168,15 +166,14 @@ describe('Chat test', () => {
})
})

it('should return an array of chat objects', done => {
it('should return a chat object', done => {
api
.get('/api/chats')
.get(`/api/chats/${chat._id}`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + token)
.end((err, res) => {
expect(res.body)
.and.be.an('array')
.and.have.property(0)
.and.be.an('object')
.and.have.all.keys([
'__v',
'_id',
Expand All @@ -194,8 +191,8 @@ describe('Chat test', () => {
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + token)
.end((err, res) => {
const chat = res.body[0]
console.log(chat)
const chat = res.body

expect(chat)
.to.have.property('_id')
.and.to.be.a('string')
Expand Down
99 changes: 98 additions & 1 deletion test/controllers/gems_test.js
Expand Up @@ -187,9 +187,106 @@ describe('Gem Tests', () => {
})
})

describe('POST /api/gems - Create Gem API Endpoint', () => {
describe('GET /api/chats/:id', () => {

let gem = {}

beforeEach(done => {
Gem.create({
image: 'http://static.asiawebdirect.com/m/.imaging/678x452/website/bangkok/portals/vietnam/homepage/vietnam-top10s/best-markets-in-vietnam/allParagraphs/00/top10Set/00/image.jpg',
caption: 'Han Market is a prominent attraction in Da Nang, having served the local population since the French occupation in the early 20th century. Located at the grand intersection of Tran Phu Street, Bach Dang Street, Hung Vuong Street and Tran Hung Dao Street, visitors can find hundreds of stalls selling just about everything from local produce and coffee beans to T-shirts, jewellery, and accessories.',
location: 'Han Market',
user: user,
category: 'Markets'
})
.then(gemData => {
return gem = gemData
})
.then(() => done())
.catch(done)
})

it('should return a 200 response', done => {
api
.get(`/api/gems/${gem._id}`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + token)
.expect(200, done)
})

it('should respond with a JSON object', done => {
api
.get(`/api/gems/${gem._id}`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + token)
.end((err, res) => {
expect(res.header['content-type'])
.to.be.eq('application/json; charset=utf-8')
done()
})
})

it('should return a gem object', done => {
api
.get(`/api/gems/${gem._id}`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + token)
.end((err, res) => {
expect(res.body)
.and.be.an('object')
.and.have.all.keys([
'__v',
'_id',
'image',
'caption',
'location',
'user',
'category',
'comments',
'createdAt',
'id',
'likeCount',
'updatedAt'
])
done()
})
})

it('gem object should have properities: _id, title, image, comments', done => {
api
.get(`/api/gems/${gem._id}`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + token)
.end((err, res) => {
const gem = res.body

expect(gem)
.to.have.property('_id')
.and.to.be.a('string')

expect(gem)
.to.have.property('image')
.and.to.be.a('string')

expect(gem)
.to.have.property('caption')
.and.to.be.a('string')


expect(gem)
.to.have.property('location')
.and.to.be.a('string')

expect(gem)
.to.have.property('category')
.and.to.be.a('string')

done()
})
})
})

describe('POST /api/gems - Create Gem API Endpoint', () => {

it('should return a 201 response', done => {
api
Expand Down
1 change: 0 additions & 1 deletion test/controllers/users_test.js
Expand Up @@ -49,7 +49,6 @@ describe('User tests', () => {
userType: 'traveller'
})
.end((err, res) => {
console.log(res)
const user = res.body

expect(user)
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -6479,6 +6479,11 @@ spdy@^4.0.0:
select-hose "^2.0.0"
spdy-transport "^3.0.0"

spectre.css@^0.5.8:
version "0.5.8"
resolved "https://registry.yarnpkg.com/spectre.css/-/spectre.css-0.5.8.tgz#e543e58a52dd83409286a065b0fb1e9e1c16e077"
integrity sha512-3N4WocWY+Dl6b3e5v3nsZYyp+VSDcBfGDzyyHw/H78ie9BoAhHkxmrhLxo9y8RadxYzVrPjfPdlev3hXEUzR2w==

split-string@^3.0.1, split-string@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"
Expand Down

0 comments on commit 659eeee

Please sign in to comment.