-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.e2e.test.js
More file actions
85 lines (73 loc) · 2.05 KB
/
index.e2e.test.js
File metadata and controls
85 lines (73 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict'
const {expect} = require('chai')
const chai = require('chai')
const supertest = require('supertest')
const app = require('../../app')
const mongoose = require('mongoose')
const {model: electionsSchema} = require('../../models/elections/schema')
const mongodbUrl = 'mongodb://root:password123@localhost:27017,localhost:27018,localhost:27019/elecTest?replicaSet=replicat&authSource=admin'
describe('POST /elections', () => {
before(async () => {
await mongoose.connect(mongodbUrl)
if (mongoose.connection.readyState !== 1) throw new Error('mongodb connection failed')
})
beforeEach(async () => {
})
afterEach(async () => {
await electionsSchema.deleteMany({})
});
after(async () => {
// await mongoose.connection.db.dropDatabase();
});
it('should return validation error on party field missing', async () => {
await supertest(app)
.post('/elections')
.send({
candidate: 'some'
})
.expect(400)
.then(res => {
expect(res.body.details[0].message).to.equal('"party" is required')
});
});
it('should return success on validation pass', async () => {
await supertest(app)
.post('/elections')
.send({
candidate: 'some',
party: 'jubilee'
})
.expect(200)
.then(res => {
const {candidate, votes, party, __v} = res.body.data
return expect({candidate, votes, party, __v}).to.eql({
candidate: 'some',
votes: '0',
party: 'jubilee',
__v: 0
});
});
});
it('should return duplication error on candidate field', async () => {
await electionsSchema.create({
candidate: 'some',
party: 'jubilee',
votes: 10
})
await supertest(app)
.post('/elections')
.send({
candidate: 'some',
party: 'jubilee',
votes: 10
})
.expect(400)
.then(res => {
return expect(res.body).to.eql({
name: 'DUPLICATE_KEY_ERROR',
param: 'votes',
value: '10'
});
});
});
})