Skip to content

Commit 0d12921

Browse files
tests
1 parent 1f3786a commit 0d12921

File tree

3 files changed

+64
-81
lines changed

3 files changed

+64
-81
lines changed

test/server.test.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,26 @@ const {
1212
app
1313
} = require('../app/server.js');
1414

15-
const expect = chai.expect; // So we can do "expect" instead of always typing "chai.expect"
16-
chai.use(chaiHttp); // implements chai http plugin
15+
const expect = chai.expect;
16+
chai.use(chaiHttp);
1717

1818
describe('Integration tests for: /', function() {
19-
// Mocha Hook: Runs before ALL the "it" test blocks.
2019
before(function() {
21-
// Be sure to always return a promise to Mocha when doing asynchronous work,
22-
// Otherwise Mocha will just asume your work is done even if it isn't.
2320

24-
// Starts our Express Server, so we can test it.
2521
return startServer(true);
2622
});
27-
// Mocha Hook: Runs after ALL the "it" test blocks.
23+
.
2824
after(function() {
29-
// Be sure to always return a promise to Mocha when doing asynchronous work,
30-
// Otherwise Mocha will just asume your work is done even if it isn't.
3125

32-
// Shuts down our Express Server, since we don't need it anymore.
3326
return stopServer();
3427
});
3528

3629
it('Should return index.html', function() {
3730
chai.request(app)
3831
.get('/')
3932
.then(res => {
40-
// chai-http assertions docs: http://www.chaijs.com/plugins/chai-http/#assertions
4133
expect(res).to.have.status(HTTP_STATUS_CODES.OK);
4234
expect(res).to.be.html;
43-
// Chai.string assertion docs: http://www.chaijs.com/api/bdd/#stringstr-msg
4435
expect(res.text).to.have.string('<!DOCTYPE html>');
4536
});
4637
});

test/user.test.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const mongoose = require('mongoose');
2-
// https://www.npmjs.com/package/chai
2+
33
const chai = require('chai');
4-
// http://www.chaijs.com/plugins/chai-http/
4+
55
const chaiHttp = require('chai-http');
66

77
const faker = require('faker');
@@ -18,38 +18,34 @@ const {
1818
User
1919
} = require('../app/user/user.model');
2020

21-
const expect = chai.expect; // So we can do "expect" instead of always typing "chai.expect"
22-
chai.use(chaiHttp); // implements chai http plugin
21+
const expect = chai.expect;
22+
chai.use(chaiHttp);
2323

2424
describe('Integration tests for: /api/user', function() {
2525
let testUser;
2626

27-
// Mocha Hook: Runs before ALL the "it" test blocks.
27+
2828
before(function() {
29-
// Be sure to always return a promise to Mocha when doing asynchronous work,
30-
// Otherwise Mocha will just asume your work is done even if it isn't.
3129

32-
// Starts our Express Server, so we can test it.
3330
return startServer(true);
3431
});
3532

36-
// Mocha Hook: Runs before EACH "it" test block.
33+
3734
beforeEach(function() {
3835
testUser = createFakerUser();
39-
// Create a randomized test user.
36+
4037
return User.create(testUser)
4138
.then(() => {})
4239
.catch(err => {
4340
console.error(err);
4441
});
4542
});
4643

47-
// Mocha Hook: Runs after EACH "it" test block.
44+
4845
afterEach(function() {
49-
// Be sure to always return a promise to Mocha when doing asynchronous work,
50-
// Otherwise Mocha will just asume your work is done even if it isn't.
46+
5147
return new Promise((resolve, reject) => {
52-
// Deletes the entire database.
48+
5349
mongoose.connection.dropDatabase()
5450
.then(result => {
5551
resolve(result);
@@ -61,12 +57,9 @@ describe('Integration tests for: /api/user', function() {
6157
});
6258
});
6359

64-
// Mocha Hook: Runs after ALL the "it" test blocks.
60+
6561
after(function() {
66-
// Be sure to always return a promise to Mocha when doing asynchronous work,
67-
// Otherwise Mocha will just asume your work is done even if it isn't.
6862

69-
// Shuts down our Express Server, since we don't need it anymore.
7063
return stopServer();
7164
});
7265

test/note.test.js renamed to test/workout.js

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const {
2626
const expect = chai.expect; // So we can do "expect" instead of always typing "chai.expect"
2727
chai.use(chaiHttp); // implements chai http plugin
2828

29-
describe('Integration tests for: /api/note', function() {
29+
describe('Integration tests for: /api/workout', function() {
3030
let testUser, jwtToken;
3131

3232
// Mocha Hook: Runs before ALL the "it" test blocks.
@@ -75,11 +75,11 @@ describe('Integration tests for: /api/note', function() {
7575

7676
const seedData = [];
7777
for (let i = 1; i <= 10; i++) {
78-
const newNote = createFakerNote();
79-
newNote.user = createdUser.id;
80-
seedData.push(newNote);
78+
const newWorkout = createFakerWorkout();
79+
newWorkout.user = createdUser.id;
80+
seedData.push(newWorkout);
8181
}
82-
return Note.insertMany(seedData)
82+
return Workout.insertMany(seedData)
8383
.catch(err => {
8484
console.error(err);
8585
throw new Error(err);
@@ -113,20 +113,20 @@ describe('Integration tests for: /api/note', function() {
113113
return stopServer();
114114
});
115115

116-
it('Should return user notes', function() {
116+
it('Should return user workouts', function() {
117117
return chai.request(app)
118-
.get('/api/note')
118+
.get('/api/workout')
119119
.set('Authorization', `Bearer ${jwtToken}`)
120120
.then(res => {
121121
expect(res).to.have.status(HTTP_STATUS_CODES.OK);
122122
expect(res).to.be.json;
123123
expect(res.body).to.be.a('array');
124124
expect(res.body).to.have.lengthOf.at.least(1);
125-
const note = res.body[0];
126-
expect(note).to.include.keys('user', 'title', 'content');
127-
expect(note.user).to.be.a('object');
128-
expect(note.user).to.include.keys('name', 'email', 'username');
129-
expect(note.user).to.deep.include({
125+
const workout = res.body[0];
126+
expect(workout).to.include.keys('user', 'exercise', 'set');
127+
expect(workout.user).to.be.a('object');
128+
expect(workout.user).to.include.keys('name', 'email', 'username');
129+
expect(workout.user).to.deep.include({
130130
id: testUser.id,
131131
username: testUser.username,
132132
email: testUser.email,
@@ -135,16 +135,16 @@ describe('Integration tests for: /api/note', function() {
135135
});
136136
});
137137

138-
it('Should return a specific note', function() {
139-
let foundNote;
138+
it('Should return a specific workout', function() {
139+
let foundWorkout;
140140
return Workout.find()
141-
.then(notes => {
142-
expect(notes).to.be.a('array');
143-
expect(notes).to.have.lengthOf.at.least(1);
141+
.then(workouts => {
142+
expect(workouts).to.be.a('array');
143+
expect(workouts).to.have.lengthOf.at.least(1);
144144
foundWorkout = workouts[0];
145145

146146
return chai.request(app)
147-
.get(`/api/note/${foundNote.id}`)
147+
.get(`/api/workout/${foundWorkout.id}`)
148148
.set('Authorization', `Bearer ${jwtToken}`);
149149
})
150150
.then(res => {
@@ -153,61 +153,60 @@ describe('Integration tests for: /api/note', function() {
153153
expect(res.body).to.be.a('object');
154154
expect(res.body).to.include.keys('user', 'title', 'content');
155155
expect(res.body).to.deep.include({
156-
id: foundNote.id,
157-
title: foundNote.title,
158-
content: foundNote.content
156+
id: foundWorkout.id,
157+
exercise: foundWorkout.exercise
158+
159159
});
160160
});
161161
});
162162

163-
it('Should update a specific note', function() {
164-
let noteToUpdate;
165-
const newNoteData = createFakerNote();
166-
return Note.find()
167-
.then(notes => {
168-
expect(notes).to.be.a('array');
169-
expect(notes).to.have.lengthOf.at.least(1);
170-
noteToUpdate = notes[0];
163+
it('Should update a specific workout', function() {
164+
let workoutToUpdate;
165+
const newWorkoutData = createFakerWorkout();
166+
return Workout.find()
167+
.then(workouts => {
168+
expect(workouts).to.be.a('array');
169+
expect(workouts).to.have.lengthOf.at.least(1);
170+
workoutToUpdate = workouts[0];
171171

172172
return chai.request(app)
173-
.put(`/api/note/${noteToUpdate.id}`)
173+
.put(`/api/workout/${workoutToUpdate.id}`)
174174
.set('Authorization', `Bearer ${jwtToken}`)
175-
.send(newNoteData);
175+
.send(newWorkoutData);
176176
})
177177
.then(res => {
178178
expect(res).to.have.status(HTTP_STATUS_CODES.NO_CONTENT);
179179

180-
return Note.findById(noteToUpdate.id);
180+
return Workout.findById(workoutToUpdate.id);
181181
})
182-
.then(note => {
183-
expect(note).to.be.a('object');
184-
expect(note).to.deep.include({
185-
id: noteToUpdate.id,
186-
title: newNoteData.title,
187-
content: newNoteData.content
182+
.then(workout => {
183+
expect(workout).to.be.a('object');
184+
expect(workout).to.deep.include({
185+
id: workoutToUpdate.id,
186+
exercise: newWorkoutData.title,
188187
});
189188
});
190189
});
191190

192-
it('Should delete a specific note', function() {
193-
let noteToDelete;
194-
return Note.find()
195-
.then(notes => {
196-
expect(notes).to.be.a('array');
197-
expect(notes).to.have.lengthOf.at.least(1);
198-
noteToDelete = notes[0];
191+
it('Should delete a specific workout', function() {
192+
let workoutToDelete;
193+
return Workout.find()
194+
.then(workouts => {
195+
expect(workouts).to.be.a('array');
196+
expect(workouts).to.have.lengthOf.at.least(1);
197+
workoutToDelete = workouts[0];
199198

200199
return chai.request(app)
201-
.delete(`/api/note/${noteToDelete.id}`)
200+
.delete(`/api/workout/${workoutToDelete.id}`)
202201
.set('Authorization', `Bearer ${jwtToken}`);
203202
})
204203
.then(res => {
205204
expect(res).to.have.status(HTTP_STATUS_CODES.NO_CONTENT);
206205

207-
return Note.findById(noteToDelete.id);
206+
return Workout.findById(workoutToDelete.id);
208207
})
209-
.then(note => {
210-
expect(note).to.not.exist;
208+
.then(workout => {
209+
expect(workout).to.not.exist;
211210
});
212211
});
213212

@@ -220,10 +219,10 @@ describe('Integration tests for: /api/note', function() {
220219
};
221220
}
222221

223-
function createFakerNote() {
222+
function createFakerworkout() {
224223
return {
225224
title: faker.lorem.sentence(),
226225
content: faker.lorem.paragraphs()
227226
};
228227
}
229-
});
228+
});

0 commit comments

Comments
 (0)