Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ef0c67

Browse files
committed
[finished] debuging comment tests
1 parent 51261c5 commit 9ef0c67

File tree

5 files changed

+117
-20
lines changed

5 files changed

+117
-20
lines changed

api/v1/controllers/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const createCommentController = (req, res, next) => {
1313
if (checkFields) {
1414
res.status(400).json({
1515
status: 'error',
16-
Error: 'Invalid request',
16+
error: 'Invalid request',
1717
});
1818
} else {
1919
const { comment } = req.body;

api/v1/models/user.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ const User = {
2323
return rows;
2424
},
2525

26-
update(values) {
27-
const queryText = `UPDATE users WHERE
28-
ID=$1 firstName=$2 lastName=$3 storeduserEmail=$4, hashedPassword=$5, gender=$6, jobRole=$7, department=$8, address=$9
29-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`;
30-
const row = query(queryText, values)
31-
.then((res) => res.rows[0])
32-
.catch((err) => {
33-
throw err;
34-
});
35-
return row;
36-
},
37-
3826
save(values) {
3927
const queryText = `INSERT INTO
4028
users (id, firstName, lastName, storeduserEmail, hashedPassword, gender, jobRole, department, address)

api/v1/test/comment.spec.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import chai, { expect } from 'chai';
2+
import chaiHttp from 'chai-http';
3+
import 'chai/register-should';
4+
import dotenv from 'dotenv';
5+
import moment from 'moment';
6+
import uuidv1 from 'uuid/v1';
7+
import server from '../../../server';
8+
import Gif from '../models/gif';
9+
import User from '../models/user';
10+
import Article from '../models/article';
11+
import Comment from '../models/comment';
12+
import helper from '../controllers/helper';
13+
14+
dotenv.config();
15+
16+
process.env.NODE_ENV = 'test';
17+
18+
const { save, deleteAllUsers } = User;
19+
const { saveArticle, deleteAllArticles } = Article;
20+
const { saveGif, deleteAllGifs } = Gif;
21+
const { deleteAllComments } = Comment;
22+
23+
const { hashPassword, generateToken} = helper;
24+
25+
chai.use(chaiHttp);
26+
27+
describe('comments', () => {
28+
const id1 = uuidv1();
29+
const obj = generateToken({ sub: id1, role: 'ADMIN' });
30+
const token = `Bearer ${obj}`;
31+
before(async () => {
32+
const articleTitle = 'First test title';
33+
const articleText = 'First test article test';
34+
const date = moment().format('YYYY-MM-DD HH:mm:ss');
35+
36+
const userValues = [id1, 'Daniel', 'Mukuna', '[email protected]', hashPassword('DANIEL12345'), 'male', 'admin', 'I.T', 'thika'];
37+
const articleValues = [1, articleTitle, articleText, date, id1];
38+
39+
const gifTitle = 'First gif test value';
40+
const gUrl = 'http://res.cloudinary.com/mukuna/image/upload/v1574479438/teamwork-api-gifs/utvsco0pt6suogvzfbu9.gif';
41+
const p_id = 'teamwork-api-gifs/utvsco0pt6suogvzfbu9';
42+
43+
const gifValues = [1, gifTitle, gUrl, p_id, date, id1];
44+
await save(userValues);
45+
await saveGif(gifValues);
46+
await saveArticle(articleValues);
47+
});
48+
49+
after(async () => {
50+
await deleteAllComments();
51+
await deleteAllGifs();
52+
await deleteAllArticles();
53+
await deleteAllUsers();
54+
});
55+
56+
describe('POST /api/v1/articles/:articleID/comments', () => {
57+
it('should create a new comment on the article', async () => {
58+
const comment1 = {
59+
comment: 'first article test comment'
60+
};
61+
const res = await chai.request(server)
62+
.post('/api/v1/articles/1/comments')
63+
.set('Authorization', token)
64+
.set('Content-Type', 'application/json')
65+
.send(comment1);
66+
expect(res).to.have.status(201);
67+
expect(res.body).to.be.an('Object');
68+
expect(res.body.status).to.be.equals('success');
69+
});
70+
71+
it('should fail if the comment field is empty', async () => {
72+
const comment2 = {
73+
comment: ''
74+
};
75+
const res = await chai.request(server)
76+
.post('/api/v1/articles/1/comments')
77+
.set('Authorization', token)
78+
.set('Content-Type', 'application/json')
79+
.send(comment2);
80+
expect(res).to.have.status(400);
81+
expect(res.body).to.be.an('Object');
82+
expect(res.body.status).to.be.equals('error');
83+
});
84+
});
85+
86+
describe('POST /api/v1/gifs/:gifID/comments', () => {
87+
it('should create a new comment on the gif', async () => {
88+
const comment3 = {
89+
comment: 'first gif image test comment'
90+
};
91+
const res = await chai.request(server)
92+
.post('/api/v1/gifs/1/comments')
93+
.set('Authorization', token)
94+
.set('Content-Type', 'application/json')
95+
.send(comment3);
96+
expect(res).to.have.status(201);
97+
expect(res.body).to.be.an('Object');
98+
expect(res.body.status).to.be.equals('success');
99+
});
100+
101+
it('should fail if the comment field is empty', async () => {
102+
const comment4 = {
103+
comment: ''
104+
};
105+
const res = await chai.request(server)
106+
.post('/api/v1/gifs/1/comments')
107+
.set('Authorization', token)
108+
.set('Content-Type', 'application/json')
109+
.send(comment4);
110+
expect(res).to.have.status(400);
111+
expect(res.body).to.be.an('Object');
112+
expect(res.body.status).to.be.equals('error');
113+
});
114+
});
115+
});

api/v1/test/gif.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import chai, { expect } from 'chai';
22
import chaiHttp from 'chai-http';
33
import 'chai/register-should';
4-
import { after, before } from 'mocha';
54
import dotenv from 'dotenv';
65
import moment from 'moment';
76
import uuidv1 from 'uuid/v1';

server.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ const normalizePort = (val) => {
1717
return false;
1818
};
1919

20-
let port;
21-
if (process.env.NODE_ENV === 'test') {
22-
port = normalizePort('3001');
23-
} else {
24-
port = normalizePort(process.env.PORT || '3000');
25-
}
20+
const port = normalizePort(process.env.PORT || '3000');;
2621

2722
app.set('port', port);
2823

0 commit comments

Comments
 (0)