-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
post.spec.js
78 lines (58 loc) · 2.22 KB
/
post.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import test from 'ava';
import request from 'supertest';
import app from '../../server';
import Post from '../post';
import { connectDB, dropDB } from '../../util/test-helpers';
// Initial posts added into test db
const posts = [
new Post({ name: 'Prashant', title: 'Hello Mern', slug: 'hello-mern', cuid: 'f34gb2bh24b24b2', content: "All cats meow 'mern!'" }),
new Post({ name: 'Mayank', title: 'Hi Mern', slug: 'hi-mern', cuid: 'f34gb2bh24b24b3', content: "All dogs bark 'mern!'" }),
];
test.before('connect to mockgoose', async () => {
await connectDB();
});
test.beforeEach('connect and add two post entries', async () => {
await Post.create(posts).catch(() => 'Unable to create posts');
});
test.afterEach.always(async () => {
await dropDB();
});
test.serial('Should correctly give number of Posts', async t => {
t.plan(2);
const res = await request(app)
.get('/api/posts')
.set('Accept', 'application/json');
t.is(res.status, 200);
t.deepEqual(posts.length, res.body.posts.length);
});
test.serial('Should send correct data when queried against a cuid', async t => {
t.plan(2);
const post = new Post({ name: 'Foo', title: 'bar', slug: 'bar', cuid: 'f34gb2bh24b24b2', content: 'Hello Mern says Foo' });
post.save();
const res = await request(app)
.get('/api/posts/f34gb2bh24b24b2')
.set('Accept', 'application/json');
t.is(res.status, 200);
t.is(res.body.post.name, post.name);
});
test.serial('Should correctly add a post', async t => {
t.plan(2);
const res = await request(app)
.post('/api/posts')
.send({ post: { name: 'Foo', title: 'bar', content: 'Hello Mern says Foo' } })
.set('Accept', 'application/json');
t.is(res.status, 200);
const savedPost = await Post.findOne({ title: 'bar' }).exec();
t.is(savedPost.name, 'Foo');
});
test.serial('Should correctly delete a post', async t => {
t.plan(2);
const post = new Post({ name: 'Foo', title: 'bar', slug: 'bar', cuid: 'f34gb2bh24b24b2', content: 'Hello Mern says Foo' });
post.save();
const res = await request(app)
.delete(`/api/posts/${post.cuid}`)
.set('Accept', 'application/json');
t.is(res.status, 200);
const queriedPost = await Post.findOne({ cuid: post.cuid }).exec();
t.is(queriedPost, null);
});