-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
130 lines (115 loc) · 4.02 KB
/
app.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const request = require("supertest");
const app = require("./app");
const { getVisit, saveVisit, searchVisits } = require('./visits');
jest.mock('./visits')
describe("Test routes", () => {
afterEach(() => {
jest.clearAllMocks();
})
test("bad get request no params", done => {
request(app)
.get("/visit")
.then(response => {
expect(response.statusCode).toBe(400);
done();
});
});
test('get visit', done => {
getVisit.mockReturnValueOnce([{
userId: 'testUser',
name: 'testLocation',
visitId: 'testId'
}])
request(app)
.get("/visit?visitId=1234")
.then(response => {
expect(getVisit).toBeCalled();
expect(response.body).toStrictEqual([{
userId: 'testUser',
name: 'testLocation',
visitId: 'testId'
}]);
expect(response.statusCode).toBe(200);
done();
});
});
test('get visit error', done => {
getVisit.mockRejectedValue(new Error('test error'))
request(app)
.get("/visit?visitId=1234")
.then(response => {
expect(getVisit).toBeCalled();
expect(response.text).toStrictEqual('we have encountered an error: Error: test error');
expect(response.statusCode).toBe(500);
done();
});
});
test('search visits', done => {
const expected = [{
userId: 'testUser',
name: 'testLocation1',
visitId: 'testId1'
}, {
userId: 'testUser',
name: 'testLocation2',
visitId: 'testId2'
},]
searchVisits.mockReturnValueOnce(expected)
request(app)
.get("/visit?userId=1234&searchString=test")
.then(response => {
expect(searchVisits).toBeCalled();
expect(response.body).toStrictEqual(expected);
expect(response.statusCode).toBe(200);
done();
});
});
test('search visits error', done => {
searchVisits.mockRejectedValue(new Error('test error'))
request(app)
.get("/visit?userId=1234&searchString=test")
.then(response => {
expect(searchVisits).toBeCalled();
expect(response.text).toStrictEqual('we have encountered an error: Error: test error');
expect(response.statusCode).toBe(500);
done();
});
});
test('save visit bad request', done => {
request(app)
.post('/visit')
.send({ test: "testUser", bad: "testLocation" })
.set('Accept', 'application/json')
.then(response => {
expect(response.statusCode).toBe(400);
done();
})
});
test('save visit', done => {
const expected = { visitId: '1234' };
saveVisit.mockReturnValueOnce(expected);
request(app)
.post('/visit')
.send({ userId: "testUser", name: "testLocation" })
.set('Accept', 'application/json')
.then(response => {
expect(saveVisit).toBeCalled();
expect(response.body).toStrictEqual(expected);
expect(response.statusCode).toBe(200);
done();
})
});
test('save visit error', done => {
saveVisit.mockRejectedValue(new Error('test error'))
request(app)
.post('/visit')
.send({ userId: "testUser", name: "testLocation" })
.set('Accept', 'application/json')
.then(response => {
expect(saveVisit).toBeCalled();
expect(response.text).toStrictEqual('we have encountered an error: Error: test error');
expect(response.statusCode).toBe(500);
done();
})
});
});