Skip to content

Commit 8c896cd

Browse files
committed
added testing framework
added config added seeding file added one unit test for admin visibility NEEDS MORE SEED DATA!
1 parent 3eb6811 commit 8c896cd

File tree

7 files changed

+5179
-3153
lines changed

7 files changed

+5179
-3153
lines changed

packages/api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "nodemon ./src/index.ts",
88
"start": "tsc && node dist/index.js",
9-
"test": "jest --runInBand --watchAll"
9+
"test": "jest --runInBand --watchAll --testTimeout=10000"
1010
},
1111
"dependencies": {
1212
"@seitz/shared": "workspace:*",
@@ -31,6 +31,7 @@
3131
"@types/passport-local": "^1.0.36",
3232
"@types/supertest": "^2.0.15",
3333
"jest": "^29.7.0",
34+
"mongodb-memory-server": "^8.16.1",
3435
"nodemon": "^3.0.1",
3536
"supertest": "^6.3.3",
3637
"ts-jest": "^29.1.1",

packages/api/src/routes/admin.route.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ router.delete(
4343
);
4444

4545
router.put(
46-
<<<<<<< HEAD
4746
"/battery/:id/stage/:stageId/visibility/:status",
4847
isAdmin,
4948
route((req) =>
@@ -56,8 +55,6 @@ router.put(
5655
);
5756

5857
router.put(
59-
=======
60-
>>>>>>> 94315a1 (first implementation)
6158
"/battery/:id/visibility/:status",
6259
isAdmin,
6360
route((req) =>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import mongoose from "mongoose";
2+
import { MongoMemoryServer } from "mongodb-memory-server";
3+
4+
let mongoServer: MongoMemoryServer;
5+
6+
export const connectDB = async () => {
7+
mongoServer = await MongoMemoryServer.create();
8+
const mongoUri = mongoServer.getUri();
9+
await mongoose.connect(mongoUri);
10+
};
11+
12+
export const closeDB = async () => {
13+
await mongoose.disconnect();
14+
await mongoServer.stop();
15+
};
16+
17+
export const clearDB = async () => {
18+
const collections = mongoose.connection.collections;
19+
for (const key in collections) {
20+
await collections[key].deleteMany({});
21+
}
22+
};

packages/api/test/e2e/example.test.ts

Lines changed: 0 additions & 110 deletions
This file was deleted.

packages/api/test/seed/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import mongoose from "mongoose";
2+
import { Battery, CustomizedBattery, User } from "../../src/models";
3+
import type { CreateBattery, CreateUser } from "@seitz/shared";
4+
5+
export const seedUsers = [
6+
{
7+
_id: new mongoose.Types.ObjectId(),
8+
9+
password: "password123",
10+
isAdmin: true,
11+
},
12+
{
13+
_id: new mongoose.Types.ObjectId(),
14+
15+
password: "password123",
16+
isAdmin: false,
17+
},
18+
] as CreateUser[];
19+
20+
export const seedBatteries = [
21+
{
22+
_id: new mongoose.Types.ObjectId(),
23+
name: "Test Battery",
24+
description: "A test battery",
25+
imageUrl: "https://test.com/image.jpg",
26+
stages: [],
27+
deleted: false,
28+
},
29+
] as CreateBattery[];
30+
31+
export const seedCustomizedBatteries = [
32+
{
33+
_id: new mongoose.Types.ObjectId(),
34+
battery: seedBatteries[0]._id,
35+
isVisibleToNonAdmins: false,
36+
},
37+
];
38+
39+
export const seedDatabase = async () => {
40+
await User.create(seedUsers);
41+
await Battery.create(seedBatteries);
42+
await CustomizedBattery.create(seedCustomizedBatteries);
43+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { connectDB, closeDB, clearDB } from "../config/database";
2+
import { seedDatabase, seedCustomizedBatteries } from "../seed";
3+
import * as adminService from "../../src/services/admin.service";
4+
import HttpError from "../../src/types/errors";
5+
6+
describe("Admin Service", () => {
7+
beforeAll(async () => {
8+
await connectDB();
9+
});
10+
11+
beforeEach(async () => {
12+
await clearDB();
13+
await seedDatabase();
14+
});
15+
16+
afterAll(async () => {
17+
await closeDB();
18+
});
19+
20+
describe("updateAdminVisibility", () => {
21+
const batteryId = seedCustomizedBatteries[0]._id.toString();
22+
23+
it('should update visibility to true when setting to "on"', async () => {
24+
const [status, battery] = await adminService.updateAdminVisibility(
25+
batteryId,
26+
"on"
27+
);
28+
29+
expect(status).toBe(200);
30+
expect(battery?.isVisibleToNonAdmins).toBe(true);
31+
});
32+
33+
it('should update visibility to false when setting to "off"', async () => {
34+
const [status, battery] = await adminService.updateAdminVisibility(
35+
batteryId,
36+
"off"
37+
);
38+
39+
expect(status).toBe(200);
40+
expect(battery?.isVisibleToNonAdmins).toBe(false);
41+
});
42+
43+
it("should default to false for invalid visibility value", async () => {
44+
const [status, battery] = await adminService.updateAdminVisibility(
45+
batteryId,
46+
"invalid"
47+
);
48+
49+
expect(status).toBe(200);
50+
expect(battery?.isVisibleToNonAdmins).toBe(false);
51+
});
52+
53+
describe("edge cases should default to false", () => {
54+
const testCases = [
55+
{ desc: "undefined", value: undefined },
56+
{ desc: "null", value: null },
57+
{ desc: "empty string", value: "" },
58+
{ desc: "number", value: 123 },
59+
{ desc: "object", value: {} },
60+
];
61+
62+
testCases.forEach(({ desc, value }) => {
63+
it(`should handle ${desc} input`, async () => {
64+
const [status, battery] = await adminService.updateAdminVisibility(
65+
batteryId,
66+
value as unknown as string // have to do this to avoid type error :/
67+
);
68+
69+
expect(status).toBe(200);
70+
expect(battery?.isVisibleToNonAdmins).toBe(false);
71+
});
72+
});
73+
});
74+
75+
it("should throw 404 error for non-existent battery", async () => {
76+
const nonExistentId = "507f1f77bcf86cd799439011";
77+
78+
await expect(
79+
adminService.updateAdminVisibility(nonExistentId, "on")
80+
).rejects.toThrow(
81+
new HttpError(404, `Battery not found with ID: ${nonExistentId}`)
82+
);
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)