Skip to content

Commit c1fdf9d

Browse files
committed
feat: add test/route for api/todos/:id
1 parent 5297d57 commit c1fdf9d

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

server/api/todos/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import controller from './todos.controller';
44
const router = express.Router();
55

66
router.get('/', controller.index);
7+
router.get('/:id', controller.show);
78

89
export default router;
Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
1+
import { find } from 'lodash';
12
// Exported controller methods
23
export default {
3-
index
4+
index,
5+
show
46
};
57

8+
// Replace this with an actual fetch from a database
9+
const todos = [
10+
{
11+
id: 1,
12+
text: 'Learn React',
13+
completed: true
14+
},
15+
{
16+
id: 2,
17+
text: 'Learn Redux',
18+
completed: true
19+
},
20+
{
21+
id: 3,
22+
text: 'Start an app',
23+
completed: true
24+
},
25+
{
26+
id: 4,
27+
text: 'Make it universally rendered',
28+
completed: true
29+
},
30+
{
31+
id: 5,
32+
text: 'Enable code splitting',
33+
completed: true
34+
},
35+
{
36+
id: 6,
37+
text: 'Build a kick ass app',
38+
completed: false
39+
}
40+
];
41+
42+
export function show(req, res) {
43+
const id = parseInt(req.params.id);
44+
const todo = find(todos, { id });
45+
46+
return res.json(todo);
47+
}
48+
649
export function index(req, res) {
7-
// Replace this with an actual fetch from a database
8-
return res.json([
9-
{
10-
id: 1,
11-
text: 'Learn React',
12-
completed: true
13-
},
14-
{
15-
id: 2,
16-
text: 'Learn Redux',
17-
completed: true
18-
},
19-
{
20-
id: 3,
21-
text: 'Start an app',
22-
completed: true
23-
},
24-
{
25-
id: 4,
26-
text: 'Make it universally rendered',
27-
completed: true
28-
},
29-
{
30-
id: 5,
31-
text: 'Enable code splitting',
32-
completed: true
33-
},
34-
{
35-
id: 6,
36-
text: 'Build a kick ass app',
37-
completed: false
38-
}
39-
]);
50+
return res.json(todos);
4051
}

server/api/todos/todos.controller.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ describe('GET /api/todos', function() {
2020
});
2121
});
2222
});
23+
24+
describe('GET /api/todos/:id', function() {
25+
test('endpoint returns a specific todos', function() {
26+
return agent
27+
.get('/api/todos/1')
28+
.expect(200)
29+
.then(({ body }) => {
30+
expect(body.id).toEqual(1);
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)