process.env.NODE_ENV = 'test';
|
|
|
|
const { describe }=require("mocha");
|
|
const { assert, expect } = require("chai");
|
|
const { connectToDatabase, disconnectFromDatabase } = require("../../controller/MongoController");
|
|
const { findAllTask,findTaskById,addTask, updateTask, deleteTask } = require("../../controller/TaskController");
|
|
const Task = require("../../model/Task");
|
|
|
|
describe("TaskController",()=>{
|
|
before(()=>{
|
|
connectToDatabase()
|
|
.then(()=>{})
|
|
.catch(err=>console.error(err));
|
|
});
|
|
beforeEach((done) => {
|
|
Task.deleteMany({}, (err) => {
|
|
if(err){
|
|
console.error(err);
|
|
}else{
|
|
done();
|
|
}
|
|
});
|
|
});
|
|
afterEach((done) => {
|
|
Task.deleteMany({}, (err) => {
|
|
if(err){
|
|
console.error(err);
|
|
}else{
|
|
done();
|
|
}
|
|
});
|
|
});
|
|
after(()=>{
|
|
disconnectFromDatabase();
|
|
});
|
|
|
|
describe("findAllTask()",(done)=>{
|
|
it("it should return empty object",(done)=>{
|
|
findAllTask().then((value)=>{
|
|
assert.isTrue(Object.keys(value).length===0);
|
|
done();
|
|
}).catch((reason)=>{assert.fail(reason)});
|
|
});
|
|
it("it should return objects",(done)=>{
|
|
let taskOne=new Task({name:"One", _id:"5e99ac68604ea9031e8545c8"});
|
|
let taskTwo=new Task({name:"Two", _id:"6e99ac68604ea9031e8545c8"});
|
|
taskOne.save((errOne,docOne)=>{
|
|
taskTwo.save((errTwo,docTwo)=>{
|
|
findAllTask().then((value)=>{
|
|
assert.isTrue(Object.keys(value).length===2);
|
|
assert.containsAllKeys(value[0],taskOne);
|
|
assert.containsAllKeys(value[1],taskTwo);
|
|
done();
|
|
}).catch((reason)=>{assert.fail(reason)});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("findTaskById()",(done)=>{
|
|
it("it should not return non-existing id",(done)=>{
|
|
findTaskById(0)
|
|
.then((item)=>{assert.fail("got "+item)})
|
|
.catch(done());
|
|
});
|
|
it("it should return valid id",(done)=>{
|
|
let task=new Task({name:"One", _id:"5e99ac68604ea9031e8545c8"});
|
|
task.save((err,doc)=>{
|
|
findTaskById(task._id)
|
|
.then((value)=>{
|
|
assert.equal(value._id.toString(),task._id.toString());
|
|
assert.equal(value.name,task.name);
|
|
assert.equal(value.description,task.description);
|
|
done();
|
|
})
|
|
.catch((reason)=>{assert.fail(reason);});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("addTask()",(done)=>{
|
|
it("it should not accept empty object",(done)=>{
|
|
let data={};
|
|
expect(()=>{addTask(data)}).to.throw();
|
|
done();
|
|
});
|
|
it("it should accept object with name",(done)=>{
|
|
let data={"name":"random"};
|
|
addTask(data)
|
|
.then((value)=>{
|
|
assert.isObject(value);
|
|
assert.equal(value.name,data.name);
|
|
existingTask=value;
|
|
done();
|
|
})
|
|
.catch((reason)=>{assert.fail(reason);});
|
|
});
|
|
it("it should have empty description if none provided",(done)=>{
|
|
let data={"name":"random"};
|
|
addTask(data)
|
|
.then((value)=>{
|
|
assert.isObject(value);
|
|
assert.equal(value.description,"");
|
|
done();
|
|
})
|
|
.catch((reason)=>{assert.fail(reason);});
|
|
});
|
|
});
|
|
|
|
describe("updateTask()",(done)=>{
|
|
it("it should not update non-existing id",(done)=>{
|
|
expect(()=>{updateTask(0,{})}).not.to.throw();
|
|
updateTask(0,{})
|
|
.then((value)=>{assert.fail(value);})
|
|
.catch((reason)=>{done();})
|
|
});
|
|
it("it should not accept parent as itself",(done)=>{
|
|
let task=new Task({name:"One", _id:"5e99ac68604ea9031e8545c8"});
|
|
task.save((err,doc)=>{
|
|
let updateParent={"parent":task._id};
|
|
updateTask(task._id,updateParent)
|
|
.then((value)=>{
|
|
assert.fail(value);
|
|
},(reason)=>{
|
|
assert.isNotEmpty(reason.toString());
|
|
done();
|
|
})
|
|
.catch((reason)=>{assert.fail(reason)});
|
|
});
|
|
});
|
|
it("it should update properties without touching others",(done)=>{
|
|
let task=new Task({name:"One", _id:"5e99ac68604ea9031e8545c8"});
|
|
task.save((err,doc)=>{
|
|
let newName={"name":"fooIsMyNewName"};
|
|
let newDescription={"description":"barIsMyNewDescription"};
|
|
updateTask(task._id,newName).then((value)=>{
|
|
assert.equal(value.name,newName.name);
|
|
assert.equal(value.description,task.description);
|
|
|
|
updateTask(task._id,newDescription).then((valueD)=>{
|
|
assert.equal(valueD.name,newName.name);
|
|
assert.equal(valueD.description,newDescription.description);
|
|
existingTask=valueD;
|
|
done();
|
|
}).catch((reasonD)=>{assert.fail(reasonD)});
|
|
|
|
}).catch((reason)=>{assert.fail(reason)});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("deleteTask()",(done)=>{
|
|
it("it should not delete non-existing id",(done)=>{
|
|
deleteTask(0)
|
|
.then((value)=>{assert.fail(value)})
|
|
.catch((reason)=>{done()})
|
|
});
|
|
it("it should delete existing id",(done)=>{
|
|
let task=new Task({name:"One", _id:"5e99ac68604ea9031e8545c8"});
|
|
task.save((err,doc)=>{
|
|
deleteTask(task._id)
|
|
.then((value)=>{
|
|
assert.isObject(value);
|
|
assert.equal(value._id.toString(),task._id.toString());
|
|
done();
|
|
})
|
|
.catch((reason)=>{console.error(reason)})
|
|
});
|
|
});
|
|
});
|
|
});
|