Skip to content

Commit e4adcec

Browse files
Merge pull request #248 from tcet-opensource/127-Write_testcases_for_infrastructure_endpointnewbranch
127 write testcases for infrastructure endpointnewbranch
2 parents 65ebad8 + 5c59794 commit e4adcec

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

test/routes/infrastructure.test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { jest } from '@jest/globals';
2+
import request from 'supertest';
3+
import app from '#app'; // Update this import based on your app's structure
4+
import connector from '#models/databaseUtil'; // Update this import
5+
import infrastructureModel from '#models/infrastructure'; // Update this import
6+
7+
jest.mock('#util');
8+
9+
let server;
10+
let agent;
11+
12+
beforeAll((done) => {
13+
server = app.listen(5000, () => {
14+
agent = request.agent(server);
15+
connector.set('debug', false);
16+
done();
17+
});
18+
});
19+
20+
function cleanUp(callback) {
21+
infrastructureModel
22+
.remove({
23+
name: 'Building A',
24+
type: 'Office',
25+
wing: 'East',
26+
floor: 3,
27+
capacity: 100,
28+
})
29+
.then(() => {
30+
connector.disconnect((DBerr) => {
31+
if (DBerr) console.log('Database disconnect error: ', DBerr);
32+
server.close((serverErr) => {
33+
if (serverErr) console.log(serverErr);
34+
callback();
35+
});
36+
});
37+
});
38+
}
39+
40+
afterAll((done) => {
41+
cleanUp(done);
42+
});
43+
44+
describe('Infrastructure API', () => {
45+
it('should create infrastructure', async () => {
46+
const response = await agent.post('/infrastructure/add').send({
47+
name: 'Building A',
48+
type: 'Office',
49+
wing: 'East',
50+
floor: 3,
51+
capacity: 100,
52+
});
53+
54+
expect(response.status).toBe(200);
55+
expect(response.body.res).toMatch(/added user/);
56+
});
57+
58+
describe('after adding infrastructure', () => {
59+
beforeEach(async () => {
60+
await agent.post('/infrastructure/add').send({
61+
name: 'Building A',
62+
type: 'Office',
63+
wing: 'East',
64+
floor: 3,
65+
capacity: 100,
66+
});
67+
});
68+
69+
afterEach(async () => {
70+
await infrastructureModel.remove({
71+
name: 'Building A',
72+
type: 'Office',
73+
wing: 'East',
74+
floor: 3,
75+
capacity: 100,
76+
});
77+
});
78+
79+
it('should read infrastructure', async () => {
80+
const response = await agent
81+
.post('/infrastructure/list')
82+
.send({ name: 'Building A' });
83+
expect(response.body.res).not.toBeNull();
84+
});
85+
86+
it('should update infrastructure', async () => {
87+
const response = await agent
88+
.post('/infrastructure/update')
89+
.send({ name: 'Building A' }, { capacity: 150 });
90+
91+
expect(response.status).toBe(200);
92+
expect(response.body.res).toMatch(/updated infrastructure/);
93+
});
94+
});
95+
});

0 commit comments

Comments
 (0)