Created
October 28, 2018 01:09
-
-
Save DamianRivas/2bf2eb47870a68d8e6545a11f7647d6f to your computer and use it in GitHub Desktop.
Properly formatted tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require("request"); | |
const server = require("../../src/server"); | |
const base = "http://localhost:3000/topics/"; | |
const sequelize = require("../../src/db/models/index").sequelize; | |
const Topic = require("../../src/db/models").Topic; | |
const Post = require("../../src/db/models").Post; | |
const User = require("../../src/db/models").User; | |
const Comment = require("../../src/db/models").Comment; | |
describe("routes : comments", () => { | |
beforeEach(done => { | |
this.user; | |
this.topic; | |
this.post; | |
this.comment; | |
sequelize.sync({ force: true }).then(res => { | |
User.create({ | |
email: "starman@tesla.com", | |
password: "Trekkie4lyfe" | |
}).then(user => { | |
this.user = user; // store user | |
Topic.create( | |
{ | |
title: "Expeditions to Alpha Centauri", | |
description: | |
"A compilation of reports from recent visits to the star system.", | |
posts: [ | |
{ | |
title: "My first visit to Proxima Centauri b", | |
body: "I saw some rocks.", | |
userId: this.user.id | |
} | |
] | |
}, | |
{ | |
include: { | |
//nested creation of posts | |
model: Post, | |
as: "posts" | |
} | |
} | |
) | |
.then(topic => { | |
this.topic = topic; // store topic | |
this.post = this.topic.posts[0]; // store post | |
Comment.create({ | |
body: "ay caramba!!!!!", | |
userId: this.user.id, | |
postId: this.post.id | |
}) | |
.then(comment => { | |
this.comment = comment; // store comment | |
done(); | |
}) | |
.catch(err => { | |
console.log(err); | |
done(); | |
}); | |
}) | |
.catch(err => { | |
console.log(err); | |
done(); | |
}); | |
}); | |
}); | |
}); | |
//Context for guest user | |
describe("guest attempting to perform CRUD actions for Comment", () => { | |
beforeEach(done => { | |
request.get( | |
{ | |
url: "http://localhost:3000/auth/fake", | |
form: { | |
userId: 0 //userId starts from 1 and will be assigned to members | |
} | |
}, | |
(err, res, body) => { | |
done(); | |
} | |
); | |
describe("POST /topics/:topicId/posts/:postId/comments/create", () => { | |
it("should not create a new comment", done => { | |
const options = { | |
url: `${base}${this.topic.id}/posts/${ | |
this.post.id | |
}/comments/create`, | |
form: { | |
body: "This comment is amazing!" | |
} | |
}; | |
request.post(options, (err, res, body) => { | |
Comment.findOne({ where: { body: "This comment is amazing!" } }) | |
.then(comment => { | |
expect(comment).toBeNull(); // ensure no comment was created | |
done(); | |
}) | |
.catch(err => { | |
console.log(err); | |
done(); | |
}); | |
}); | |
}); | |
}); | |
describe("POST /topics/:topicId/posts/:postId/comments/:id/destroy", () => { | |
it("should not delete the comment with the associated ID", done => { | |
Comment.findAll().then(comments => { | |
const commentCountBeforeDelete = comments.length; | |
expect(commentCountBeforeDelete).toBe(1); | |
request.post( | |
`${base}${this.topic.id}/posts/${this.post.id}/comments/${ | |
this.comment.id | |
}/destroy`, | |
(err, res, body) => { | |
Comment.findAll().then(comments => { | |
expect(err).toBeNull(); | |
expect(comments.length).toBe(commentCountBeforeDelete); | |
done(); | |
}); | |
} | |
); | |
}); | |
}); | |
}); | |
}); | |
}); | |
//context for signed in (member) user | |
describe("signed in user performing CRUD actions for Comment", () => { | |
beforeEach(done => { | |
request.get( | |
{ | |
url: "http://localhost:3000/auth/fake", | |
form: { | |
role: "member", | |
userId: this.user.id, //the owner of the comment is this.user | |
email: this.user.email | |
} | |
}, | |
(err, res, body) => { | |
done(); | |
} | |
); | |
}); | |
describe("POST /topics/:topicId/posts/:postId/comments/create", () => { | |
it("should create a new comment and redirect", done => { | |
const options = { | |
url: `${base}${this.topic.id}/posts/${this.post.id}/comments/create`, | |
form: { | |
body: "This comment is amazing!" | |
} | |
}; | |
request.post(options, (err, res, body) => { | |
Comment.findOne({ where: { body: "This comment is amazing!" } }) | |
.then(comment => { | |
expect(comment).not.toBeNull(); | |
expect(comment.body).toBe("This comment is amazing!"); | |
expect(comment.id).not.toBeNull(); | |
done(); | |
}) | |
.catch(err => { | |
console.log(err); | |
done(); | |
}); | |
}); | |
}); | |
}); | |
describe("POST /topics/:topicId/posts/:postId/comments/:id/destroy", () => { | |
it("should delete the comment with the associated ID", done => { | |
Comment.findAll().then(comments => { | |
const commentCountBeforeDelete = comments.length; | |
expect(commentCountBeforeDelete).toBe(1); | |
request.post( | |
`${base}${this.topic.id}/posts/${this.post.id}/comments/${ | |
this.comment.id | |
}/destroy`, | |
(err, res, body) => { | |
expect(res.statusCode).toBe(302); | |
Comment.findAll().then(comments => { | |
expect(err).toBeNull(); | |
expect(comments.length).toBe(commentCountBeforeDelete - 1); | |
done(); | |
}); | |
} | |
); | |
}); | |
}); | |
}); | |
}); | |
}); //end context for signed in user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment