diff --git a/models/Todo.js b/models/Todo.js index 2f41151..252b108 100644 --- a/models/Todo.js +++ b/models/Todo.js @@ -26,9 +26,9 @@ TodoSchema.pre('remove', async function () { TodoSchema.methods.toJson = function () { return { - id: this._id, + id: this._id.toString(), text: this.text, - list: this.list, + list: this.list.toString(), }; }; diff --git a/models/TodoList.js b/models/TodoList.js index e7a8311..5f81578 100644 --- a/models/TodoList.js +++ b/models/TodoList.js @@ -36,7 +36,7 @@ TodoListSchema.pre('remove', async function () { TodoListSchema.methods.toJson = function () { const todos = this.populated('todos') ? this.todos.map(todo => todo.toJson()) : this.todos; return { - id: this._id, + id: this._id.toString(), name: this.name, slug: this.slug, todos, diff --git a/tests/integration/lists.test.js b/tests/integration/lists.test.js index 05dfa9e..0d1574e 100644 --- a/tests/integration/lists.test.js +++ b/tests/integration/lists.test.js @@ -3,6 +3,33 @@ const server = require('../../app.js'); const request = require('supertest'); const mongoose = require('mongoose'); +const TodoList = mongoose.model('TodoList'); +const Todo = mongoose.model('Todo'); + +let lists; +let listsPopulated; +let todos; + +beforeEach(async () => { + // seed lists and todos + const list1 = new TodoList({ name: 'List1' }); + const todo1 = new Todo({ text: 'Todo1', list: list1._id }); + const todo2 = new Todo({ text: 'Todo2', list: list1._id }); + + await list1.save(); + await todo1.save(); + await todo2.save(); + lists = await TodoList.find({}).exec(); + listsPopulated = await TodoList.find({}) + .populate('todos') + .exec(); + todos = await Todo.find({}).exec(); +}); + +afterEach(async () => { + await mongoose.connection.dropDatabase(); +}); + afterAll(async () => { await mongoose.connection.dropDatabase(); await mongoose.disconnect(); @@ -10,7 +37,7 @@ afterAll(async () => { }); describe('test lists', () => { - test('index lists', async () => { + test('should index lists', async () => { const response = await request(server) .get('/lists') .set('Accept', 'application/json') @@ -18,5 +45,19 @@ describe('test lists', () => { .expect('Content-Type', 'application/json; charset=utf-8'); expect(response.body.success).toBe(true); expect(response.body.data).toBeInstanceOf(Array); + expect(response.body.data).toEqual(listsPopulated.map(list => list.toJson())); + }); + test('should create list', async () => { + const response = await request(server) + .post('/lists') + .send({ + name: 'List2', + }) + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', 'application/json; charset=utf-8'); + expect(response.body.success).toBeTruthy(); + expect(await TodoList.findOne({ name: 'List2' })).toBeTruthy(); }); }); diff --git a/tests/integration/root.test.js b/tests/integration/root.test.js index 1484fd0..93610f9 100644 --- a/tests/integration/root.test.js +++ b/tests/integration/root.test.js @@ -3,6 +3,10 @@ const server = require('../../app.js'); const request = require('supertest'); const mongoose = require('mongoose'); +afterEach(async () => { + await mongoose.connection.dropDatabase(); +}); + afterAll(async () => { await mongoose.connection.dropDatabase(); await mongoose.disconnect();