test index, create list

This commit is contained in:
2018-05-20 01:02:38 +03:00
parent 87ec0ac4d4
commit 13f8d02ad4
4 changed files with 49 additions and 4 deletions

View File

@@ -26,9 +26,9 @@ TodoSchema.pre('remove', async function () {
TodoSchema.methods.toJson = function () { TodoSchema.methods.toJson = function () {
return { return {
id: this._id, id: this._id.toString(),
text: this.text, text: this.text,
list: this.list, list: this.list.toString(),
}; };
}; };

View File

@@ -36,7 +36,7 @@ TodoListSchema.pre('remove', async function () {
TodoListSchema.methods.toJson = function () { TodoListSchema.methods.toJson = function () {
const todos = this.populated('todos') ? this.todos.map(todo => todo.toJson()) : this.todos; const todos = this.populated('todos') ? this.todos.map(todo => todo.toJson()) : this.todos;
return { return {
id: this._id, id: this._id.toString(),
name: this.name, name: this.name,
slug: this.slug, slug: this.slug,
todos, todos,

View File

@@ -3,6 +3,33 @@ const server = require('../../app.js');
const request = require('supertest'); const request = require('supertest');
const mongoose = require('mongoose'); 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 () => { afterAll(async () => {
await mongoose.connection.dropDatabase(); await mongoose.connection.dropDatabase();
await mongoose.disconnect(); await mongoose.disconnect();
@@ -10,7 +37,7 @@ afterAll(async () => {
}); });
describe('test lists', () => { describe('test lists', () => {
test('index lists', async () => { test('should index lists', async () => {
const response = await request(server) const response = await request(server)
.get('/lists') .get('/lists')
.set('Accept', 'application/json') .set('Accept', 'application/json')
@@ -18,5 +45,19 @@ describe('test lists', () => {
.expect('Content-Type', 'application/json; charset=utf-8'); .expect('Content-Type', 'application/json; charset=utf-8');
expect(response.body.success).toBe(true); expect(response.body.success).toBe(true);
expect(response.body.data).toBeInstanceOf(Array); 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();
}); });
}); });

View File

@@ -3,6 +3,10 @@ const server = require('../../app.js');
const request = require('supertest'); const request = require('supertest');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
afterEach(async () => {
await mongoose.connection.dropDatabase();
});
afterAll(async () => { afterAll(async () => {
await mongoose.connection.dropDatabase(); await mongoose.connection.dropDatabase();
await mongoose.disconnect(); await mongoose.disconnect();