Skip to content

Commit 659eeee

Browse files
author
MiaLearnsToCode
committed
fixed tests
1 parent ee44223 commit 659eeee

File tree

6 files changed

+126
-27
lines changed

6 files changed

+126
-27
lines changed

controllers/chats.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ function indexRoute(req, res) {
1515
// SHOW
1616
function showRoute(req, res) {
1717
req.body.user = req.currentUser
18-
18+
const lang = req.currentUser.lang === 'vi' ? 'en-vi' : 'vi-en'
1919
Chat
2020
.findById(req.params.chatId)
2121
.then(chat => {
22-
if (req.currentUser.lang === 'vi') {
23-
chat.comments.map(comment => {
24-
axios.get(encodeURI(`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${key}&text=${comment.text}&lang=en-vi`))
25-
.then(comment => {
26-
console.log(comment.data.text.join('+'))
27-
})
28-
})
29-
}
30-
console.log(chat.comments)
31-
return chat.comments
22+
return Promise.all([chat, ...chat.comments.map(comment => {
23+
return axios.get(encodeURI(`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${key}&text=${comment.text}&lang=${lang}`))
24+
})])
25+
26+
})
27+
.then(values => {
28+
const [ chat, ...comments ] = values
29+
chat.comments.forEach((comment, index) => {
30+
comment.text = comments[index].data.text[0]
31+
})
32+
res.json(chat)
3233
})
33-
.then(chat => res.status(200).json(chat))
3434
.catch(err => res.status(400).json(err))
3535
}
3636

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"react": "^16.8.6",
4848
"react-dom": "^16.8.6",
4949
"react-router-dom": "^5.0.1",
50+
"spectre.css": "^0.5.8",
5051
"supertest": "^4.0.2"
5152
},
5253
"repository": "git@github.com:davt49/sei-group-project.git",

test/controllers/chats-test.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const jwt = require('jsonwebtoken')
88
const { secret } = require('../../config/environment')
99

1010
describe('Chat test', () => {
11-
let user = ''
1211
let token = ''
1312

1413
beforeEach(done => {
@@ -24,7 +23,6 @@ describe('Chat test', () => {
2423
userType: 'Local'
2524
})
2625
.then(userData => {
27-
user = userData
2826
token = jwt.sign({ sub: userData._id }, secret, { expiresIn: '10d' })
2927
done()
3028
})
@@ -133,18 +131,18 @@ describe('Chat test', () => {
133131

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

136-
let chat
134+
let chat = {}
137135

138136
beforeEach(done => {
139-
Chat.create([{
137+
Chat.create({
140138
title: 'Hotels',
141139
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',
142140
comments: []
143-
}])
141+
})
144142
.then(chatData => {
145-
chat = chatData
146-
done()
143+
return chat = chatData
147144
})
145+
.then(() => done())
148146
.catch(done)
149147
})
150148

@@ -168,15 +166,14 @@ describe('Chat test', () => {
168166
})
169167
})
170168

171-
it('should return an array of chat objects', done => {
169+
it('should return a chat object', done => {
172170
api
173-
.get('/api/chats')
171+
.get(`/api/chats/${chat._id}`)
174172
.set('Accept', 'application/json')
175173
.set('Authorization', 'Bearer ' + token)
176174
.end((err, res) => {
177175
expect(res.body)
178-
.and.be.an('array')
179-
.and.have.property(0)
176+
.and.be.an('object')
180177
.and.have.all.keys([
181178
'__v',
182179
'_id',
@@ -194,8 +191,8 @@ describe('Chat test', () => {
194191
.set('Accept', 'application/json')
195192
.set('Authorization', 'Bearer ' + token)
196193
.end((err, res) => {
197-
const chat = res.body[0]
198-
console.log(chat)
194+
const chat = res.body
195+
199196
expect(chat)
200197
.to.have.property('_id')
201198
.and.to.be.a('string')

test/controllers/gems_test.js

+98-1
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,106 @@ describe('Gem Tests', () => {
187187
})
188188
})
189189

190-
describe('POST /api/gems - Create Gem API Endpoint', () => {
190+
describe('GET /api/chats/:id', () => {
191+
192+
let gem = {}
193+
194+
beforeEach(done => {
195+
Gem.create({
196+
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',
197+
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.',
198+
location: 'Han Market',
199+
user: user,
200+
category: 'Markets'
201+
})
202+
.then(gemData => {
203+
return gem = gemData
204+
})
205+
.then(() => done())
206+
.catch(done)
207+
})
208+
209+
it('should return a 200 response', done => {
210+
api
211+
.get(`/api/gems/${gem._id}`)
212+
.set('Accept', 'application/json')
213+
.set('Authorization', 'Bearer ' + token)
214+
.expect(200, done)
215+
})
216+
217+
it('should respond with a JSON object', done => {
218+
api
219+
.get(`/api/gems/${gem._id}`)
220+
.set('Accept', 'application/json')
221+
.set('Authorization', 'Bearer ' + token)
222+
.end((err, res) => {
223+
expect(res.header['content-type'])
224+
.to.be.eq('application/json; charset=utf-8')
225+
done()
226+
})
227+
})
228+
229+
it('should return a gem object', done => {
230+
api
231+
.get(`/api/gems/${gem._id}`)
232+
.set('Accept', 'application/json')
233+
.set('Authorization', 'Bearer ' + token)
234+
.end((err, res) => {
235+
expect(res.body)
236+
.and.be.an('object')
237+
.and.have.all.keys([
238+
'__v',
239+
'_id',
240+
'image',
241+
'caption',
242+
'location',
243+
'user',
244+
'category',
245+
'comments',
246+
'createdAt',
247+
'id',
248+
'likeCount',
249+
'updatedAt'
250+
])
251+
done()
252+
})
253+
})
254+
255+
it('gem object should have properities: _id, title, image, comments', done => {
256+
api
257+
.get(`/api/gems/${gem._id}`)
258+
.set('Accept', 'application/json')
259+
.set('Authorization', 'Bearer ' + token)
260+
.end((err, res) => {
261+
const gem = res.body
262+
263+
expect(gem)
264+
.to.have.property('_id')
265+
.and.to.be.a('string')
191266

267+
expect(gem)
268+
.to.have.property('image')
269+
.and.to.be.a('string')
270+
271+
expect(gem)
272+
.to.have.property('caption')
273+
.and.to.be.a('string')
274+
275+
276+
expect(gem)
277+
.to.have.property('location')
278+
.and.to.be.a('string')
279+
280+
expect(gem)
281+
.to.have.property('category')
282+
.and.to.be.a('string')
192283

284+
done()
285+
})
286+
})
287+
})
288+
289+
describe('POST /api/gems - Create Gem API Endpoint', () => {
193290

194291
it('should return a 201 response', done => {
195292
api

test/controllers/users_test.js

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ describe('User tests', () => {
4949
userType: 'traveller'
5050
})
5151
.end((err, res) => {
52-
console.log(res)
5352
const user = res.body
5453

5554
expect(user)

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -6479,6 +6479,11 @@ spdy@^4.0.0:
64796479
select-hose "^2.0.0"
64806480
spdy-transport "^3.0.0"
64816481

6482+
spectre.css@^0.5.8:
6483+
version "0.5.8"
6484+
resolved "https://registry.yarnpkg.com/spectre.css/-/spectre.css-0.5.8.tgz#e543e58a52dd83409286a065b0fb1e9e1c16e077"
6485+
integrity sha512-3N4WocWY+Dl6b3e5v3nsZYyp+VSDcBfGDzyyHw/H78ie9BoAhHkxmrhLxo9y8RadxYzVrPjfPdlev3hXEUzR2w==
6486+
64826487
split-string@^3.0.1, split-string@^3.0.2:
64836488
version "3.1.0"
64846489
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"

0 commit comments

Comments
 (0)