Skip to content

Commit 175943b

Browse files
committed
Adding the changes
1 parent ce8b92e commit 175943b

File tree

1 file changed

+118
-51
lines changed

1 file changed

+118
-51
lines changed

test/routes/infrastructure.test.js

Lines changed: 118 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,132 @@
1-
import { addinfrastructure } from '../controllers/infrastructure';
2-
3-
describe('Infrastructure Controller', () => {
4-
const mockInfrastructure = {
5-
name: 'Building A',
6-
type: 'Office',
7-
wing: 'East',
8-
floor: 3,
9-
capacity: 100,
10-
};
11-
12-
it('should add infrastructure', async () => {
13-
const createSpy = jest.spyOn(addinfrastructure, 'create');
14-
createSpy.mockResolvedValue(mockInfrastructure);
15-
16-
const result = await addinfrastructure.addInfrastructure(
17-
mockInfrastructure
1+
import mongoose from 'mongoose';
2+
import { MongoMemoryServer } from 'mongodb-memory-server';
3+
import { jest } from '@jest/globals';
4+
import databaseUtil from '#models/databaseUtil';
5+
import app from '#app';
6+
7+
import InfrastructureFunctions from '#models/infrastructure';
8+
9+
const request = require('supertest');
10+
jest.mock('#util');
11+
let mongoServer;
12+
let server;
13+
let agent;
14+
beforeAll((done) => {
15+
server = app.listen(5000, () => {
16+
agent = request.agent(server);
17+
connector.set('debug', false);
18+
done();
19+
});
20+
});
21+
22+
function cleanUp(callback) {
23+
accreditationModel.remove({ name: 'xyz' }).then(() => {
24+
connector.disconnect((DBerr) => {
25+
if (DBerr) console.log('Database dissconnnect error: ', DBerr);
26+
server.close((serverErr) => {
27+
if (serverErr) console.log(serverErr);
28+
callback();
29+
});
30+
});
31+
});
32+
}
33+
34+
afterAll((done) => {
35+
cleanUp(done);
36+
});
37+
38+
describe('Infrastructure API Endpoints', () => {
39+
// ... (beforeEach, beforeAll, afterAll, etc.)
40+
41+
test('Read Infrastructure', async () => {
42+
await InfrastructureFunctions.create(
43+
'Building A',
44+
'Office',
45+
'East Wing',
46+
2,
47+
100
1848
);
19-
expect(result).toEqual(mockInfrastructure);
20-
expect(createSpy).toHaveBeenCalledWith(
21-
mockInfrastructure.name,
22-
mockInfrastructure.type,
23-
mockInfrastructure.wing,
24-
mockInfrastructure.floor,
25-
mockInfrastructure.capacity
49+
await InfrastructureFunctions.create(
50+
'Building B',
51+
'Residential',
52+
'West Wing',
53+
1,
54+
50
2655
);
56+
57+
const response = await request(app).get('/api/infrastructures'); // Adjust the API endpoint
58+
59+
expect(response.status).toBe(200);
60+
expect(response.body).toHaveLength(2);
61+
expect(response.body[0].name).toBe('Building A');
62+
expect(response.body[1].name).toBe('Building B');
2763
});
2864

29-
it('should read infrastructure', async () => {
30-
const filter = { name: 'Building A' };
31-
const limit = 5;
32-
const readSpy = jest.spyOn(addinfrastructure, 'read');
33-
readSpy.mockResolvedValue([mockInfrastructure]);
65+
test('Update Infrastructure', async () => {
66+
const createdInfrastructure = await InfrastructureFunctions.create(
67+
'Building A',
68+
'Office',
69+
'East Wing',
70+
2,
71+
100
72+
);
3473

35-
const result = await addinfrastructure.readInfrastructure(filter, limit);
36-
expect(result).toEqual([mockInfrastructure]);
37-
expect(readSpy).toHaveBeenCalledWith(filter, limit);
74+
const response = await request(app)
75+
.put(`/api/infrastructures/${createdInfrastructure._id}`) // Adjust the API endpoint
76+
.send({ capacity: 150 });
77+
78+
expect(response.status).toBe(200);
79+
expect(response.body.capacity).toBe(150);
3880
});
3981

40-
it('should update infrastructure', async () => {
41-
const filter = { name: 'Building A' };
42-
const updateData = { capacity: 150 };
43-
const updatedInfrastructure = { ...mockInfrastructure, capacity: 150 };
44-
const updateSpy = jest.spyOn(addinfrastructure, 'update');
45-
updateSpy.mockResolvedValue(updatedInfrastructure);
82+
test('Remove Infrastructure', async () => {
83+
const createdInfrastructure = await InfrastructureFunctions.create(
84+
'Building A',
85+
'Office',
86+
'East Wing',
87+
2,
88+
100
89+
);
4690

47-
const result = await addinfrastructure.updateInfrastructure(
48-
filter,
49-
updateData
91+
const response = await request(app).delete(
92+
`/api/infrastructures/${createdInfrastructure._id}`
93+
); // Adjust the API endpoint
94+
95+
expect(response.status).toBe(200);
96+
expect(response.body).toMatchObject({
97+
name: 'Building A',
98+
type: 'Office',
99+
wing: 'East Wing',
100+
floor: 2,
101+
capacity: 100,
102+
});
103+
104+
const infrastructuresResponse = await request(app).get(
105+
'/api/infrastructures'
50106
);
51-
expect(result).toEqual(updatedInfrastructure);
52-
expect(updateSpy).toHaveBeenCalledWith(filter, updateData);
107+
expect(infrastructuresResponse.body).toHaveLength(0);
53108
});
54109

55-
it('should remove infrastructure', async () => {
56-
const filter = { name: 'Building A' };
57-
const removedInfrastructure = { ...mockInfrastructure };
58-
const removeSpy = jest.spyOn(addinfrastructure, 'remove');
59-
removeSpy.mockResolvedValue(removedInfrastructure);
110+
test('Get Infrastructure by ID', async () => {
111+
const createdInfrastructure = await InfrastructureFunctions.create(
112+
'Building A',
113+
'Office',
114+
'East Wing',
115+
2,
116+
100
117+
);
118+
119+
const response = await request(app).get(
120+
`/api/infrastructures/${createdInfrastructure._id}`
121+
); // Adjust the API endpoint
60122

61-
const result = await addinfrastructure.removeInfrastructure(filter);
62-
expect(result).toEqual(removedInfrastructure);
63-
expect(removeSpy).toHaveBeenCalledWith(filter);
123+
expect(response.status).toBe(200);
124+
expect(response.body).toMatchObject({
125+
name: 'Building A',
126+
type: 'Office',
127+
wing: 'East Wing',
128+
floor: 2,
129+
capacity: 100,
130+
});
64131
});
65132
});

0 commit comments

Comments
 (0)