Skip to content

Commit

Permalink
Added more test cases (Task)
Browse files Browse the repository at this point in the history
  • Loading branch information
pranjalagg committed Aug 26, 2020
1 parent 99eb606 commit 1b7e4a0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start": "node src/index.js",
"dev": "env-cmd ./config/dev.env nodemon src/index.js",
"test": "env-cmd ./config/test.env jest --watch -- runInBand"
"test": "env-cmd ./config/test.env jest --watch --runInBand"
},
"jest": {
"testEnvironment": "node"
Expand Down
4 changes: 2 additions & 2 deletions src/routers/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ router.post('/tasks', auth, async (req, res) => {
}
})

// GET /taks?completed=true
// GET /tasks?completed=true
// GET /tasks?limit=10&skip=20
// GET /tasks?sortBy=createdAt:desc
router.get('/tasks', auth, async (req, res) => {
Expand Down Expand Up @@ -53,7 +53,7 @@ router.get('/tasks', auth, async (req, res) => {

router.get('/tasks/:id', auth, async (req, res) => {
const _id = req.params.id

try {
// const task = await Task.findById(_id)
const task = await Task.findOne({ _id, owner: req.user._id })
Expand Down
44 changes: 44 additions & 0 deletions tests/fixtures/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const User = require('../../src/models/user')
const Task = require('../../src/models/task')

const userOneId = new mongoose.Types.ObjectId()
const userOne = {
Expand All @@ -13,13 +14,56 @@ const userOne = {
}]
}

const userTwoId = new mongoose.Types.ObjectId()
const userTwo = {
_id: userTwoId,
name: 'Pranjal',
email: '[email protected]',
password: 'FakePass_-',
tokens: [{
token: jwt.sign({ _id: userTwoId }, process.env.JWT_SECRET)
}]
}

const taskOne = {
_id: new mongoose.Types.ObjectId(),
description: 'Dummy Task 1',
completed: true,
owner: userOneId
}

const taskTwo = {
_id: new mongoose.Types.ObjectId(),
description: 'Dummy Task 2',
completed: false,
owner: userTwoId
}

const taskThree = {
_id: new mongoose.Types.ObjectId(),
description: 'Dummy Task 3',
completed: false,
owner: userOneId
}


const setupDatabase = async () => {
await User.deleteMany()
await Task.deleteMany()
await new User(userOne).save()
await new User(userTwo).save()
await new Task(taskOne).save()
await new Task(taskTwo).save()
await new Task(taskThree).save()
}

module.exports = {
userOneId,
userOne,
userTwoId,
userTwo,
taskOne,
taskTwo,
taskThree,
setupDatabase
}
29 changes: 28 additions & 1 deletion tests/task.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const request = require('supertest')
const app = require('../src/app')
const Task = require('../src/models/task')
const { userOneId, userOne, setupDatabase } = require('./fixtures/db')
const {
userOneId,
userOne,
userTwoId,
userTwo,
taskOne,
setupDatabase
} = require('./fixtures/db')

beforeEach(setupDatabase)

Expand All @@ -16,4 +23,24 @@ test('Should create task for user', async () => {
const task = await Task.findById(response.body._id)
expect(task).not.toBeNull()
expect(task.completed).toEqual(false)
})

test('Should fetch all user tasks', async () => {
const response = await request(app)
.get('/tasks')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(response.body.length).toBe(2)
})

test('Should not delete other user\'s task', async () => {
await request(app)
.delete('/tasks/:' + taskOne._id)
.set('Authorization', `Bearer ${userTwo.tokens[0].token}`)
.send()
.expect(500)

const task = await Task.findById(taskOne._id)
expect(task).not.toBeNull()
})
2 changes: 1 addition & 1 deletion tests/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ test('Should not update invalid user fields', async () => {
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({ username: 'mytestusername' })
.expect(400)
})
})

0 comments on commit 1b7e4a0

Please sign in to comment.