Skip to content

Commit

Permalink
Merge pull request #13 from saseungmin/apply-todos-list-api
Browse files Browse the repository at this point in the history
[Feature] Apply to todos list api
  • Loading branch information
saseungmin committed Feb 22, 2021
2 parents 7b236a2 + 254328e commit a69456b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/api/todos/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Router from 'koa-router';

import { write } from './todos.ctrl';
import { write, list } from './todos.ctrl';

const todos = new Router();

todos.get('/', list);
todos.post('/', write);

export default todos;
17 changes: 16 additions & 1 deletion src/api/todos/todos.ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ export const write = async (ctx) => {
}
};

export const temp = [];
export const list = async (ctx) => {
const { user } = ctx.state;

try {
const todos = await Todo.find()
.where('writer._id')
.equals(user._id)
.sort({ createdAt: -1 })
.lean()
.exec();

ctx.body = todos;
} catch (error) {
ctx.throw(500, error);
}
};
25 changes: 24 additions & 1 deletion src/api/todos/todos.ctrl.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';

import { write } from './todos.ctrl';
import { write, list } from './todos.ctrl';

const mockSave = jest.fn().mockRejectedValue(new Error('error'));

Expand Down Expand Up @@ -43,4 +43,27 @@ describe('/todos', () => {
expect(e).toEqual(error);
}
});

it('GET response 500 /', async () => {
const error = new Error('error');

const payload = {
state: {
user: 'user',
},
request: {
body: {
task: 'test',
isComplete: false,
},
},
throw: () => {},
};

try {
await list(payload);
} catch (e) {
expect(e).toEqual(error);
}
});
});
32 changes: 32 additions & 0 deletions src/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,36 @@ describe('app', () => {
});
});
});

describe('GET /api/todos', () => {
let sessionCookie;

const payload = {
id: 'seugmin',
password: 'test123',
};

beforeEach(async () => {
sessionCookie = await setSessionCookie(payload);

const todo = {
task: 'task1',
isComplete: false,
};

await request(app.callback())
.post('/api/todos')
.set('Cookie', sessionCookie)
.send(todo);
});

it('When successful load to todos, Response 200', async () => {
const { status, body } = await request(app.callback())
.get('/api/todos')
.set('Cookie', sessionCookie);

expect(status).toBe(200);
expect(body[0]).toHaveProperty('task', 'task1');
});
});
});

0 comments on commit a69456b

Please sign in to comment.