-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.test.js
107 lines (81 loc) · 3.78 KB
/
api.test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const request = require('supertest');
const app = require('./testApi');
const Critter = require('./models/critter');
const Post = require('./models/post');
const User = require('./models/user');
jest.mock('./models/critter');
describe('POST /api/searchcritters', () => {
it('should return search results', async () => {
const mockCritters = [ { crittername: 'cat' }, { crittername: 'dog' } ];
Critter.find.mockResolvedValue(mockCritters);
const response = await request(app)
.post('/api/searchcritters')
.send({ search: 'cat', jwtToken: 'token' })
.expect(200);
expect(Critter.find).toHaveBeenCalledWith({ "crittername": { $regex: 'cat.*', $options: 'r' } });
expect(response.body).toEqual({ _ret: mockCritters });
});
});
describe('DELETE /api/deletepost', () => {
it('should return 400 if post not found', async () => {
jest.spyOn(Post, 'findByIdAndDelete').mockResolvedValueOnce(null);
const response = await request(app)
.delete('/api/deletepost')
.send({ PostsId: 'fake-id' });
expect(response.status).toBe(400);
expect(response.body.message).toBe('no post found');
});
it('should return 200 if post deleted successfully', async () => {
const deletedPost = { _id: 'fake-id', title: 'Test post', content: 'Test content' };
jest.spyOn(Post, 'findByIdAndDelete').mockResolvedValueOnce(deletedPost);
const response = await request(app)
.delete('/api/deletepost')
.send({ PostsId: 'fake-id' });
expect(response.status).toBe(200);
expect(response.body.message).toBe('Post deleted.');
});
});
describe('POST /api/searchposts', () => {
it('should return matching posts', async () => {
const matchingPosts = [
{ _id: '1', crittername: 'test critter 1', content: 'Test content 1' },
{ _id: '2', crittername: 'test critter 2', content: 'Test content 2' }
];
jest.spyOn(Post, 'find').mockResolvedValueOnce(matchingPosts);
const response = await request(app)
.post('/api/searchposts')
.send({ search: 'test', jwtToken: 'fake-token' });
expect(response.status).toBe(200);
expect(response.body._ret).toEqual(matchingPosts);
});
});
describe('GET /api/getUsername/:id', () => {
it('should return the username of an existing user', async () => {
const existingUser = { _id: '1', username: 'testuser' };
jest.spyOn(User, 'findOne').mockResolvedValueOnce(existingUser);
const response = await request(app).get('/api/getUsername/1');
expect(response.status).toBe(200);
expect(response.body.username).toBe(existingUser.username);
});
it('should return an error message if user with given ID not found', async () => {
jest.spyOn(User, 'findOne').mockResolvedValueOnce(null);
const response = await request(app).get('/api/getUsername/2');
expect(response.status).toBe(400);
expect(response.body.message).toBe('User not found with that ID');
});
});
describe('GET /api/datejoined/:id', () => {
it('should return the date joined for an existing user', async () => {
const existingUser = { _id: '1', createdAt: new Date('2022-01-01T00:00:00Z') };
jest.spyOn(User, 'findById').mockResolvedValueOnce(existingUser);
const response = await request(app).get('/api/datejoined/1');
expect(response.status).toBe(200);
expect(response.body.dateJoined).toBe(existingUser.createdAt.toISOString());
});
it('should return an error message if user with given ID not found', async () => {
jest.spyOn(User, 'findById').mockResolvedValueOnce(null);
const response = await request(app).get('/api/datejoined/2');
expect(response.status).toBe(400);
expect(response.body.message).toBe('Information not found');
});test
});