forked from bensonsung/mission1api2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
46 lines (37 loc) · 1.29 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
const request = require('supertest');
const { app, server } = require('./app');
// The following code ensures that the server is started before any tests run and closed after all tests have completed.
// Hook to start the server before running tests
beforeAll((done) => {
server.listen(3000, () => {
done();
});
});
// Hook to close the server after running tests
afterAll((done) => {
server.close(() => {
done();
});
});
// API endpoint tests
describe('API endpoint /calculateRating', () => {
it('should return a valid response with suggested rating', async () => {
const claimHistory = 'The driver has no claim history.';
const response = await request(app)
.post('/calculateRating')
.send({ text: claimHistory })
.expect(200);
expect(response.body).toHaveProperty('suggestedRating');
expect(typeof response.body.suggestedRating).toBe('number');
expect(response.body.suggestedRating).toBeGreaterThanOrEqual(1);
expect(response.body.suggestedRating).toBeLessThanOrEqual(5);
});
it('should handle invalid input and return a 400 error', async () => {
const response = await request(app)
.post('/calculateRating')
.send({})
.expect(400);
expect(response.body).toHaveProperty('error');
expect(response.body.error).toBe('Invalid input. Missing "text" field.');
});
});