Skip to content

Commit faeb400

Browse files
committed
dbconfig, jest config, schema, and
testing files for classic and api testing
1 parent 7b1eabf commit faeb400

File tree

10 files changed

+938
-32
lines changed

10 files changed

+938
-32
lines changed

app/api/users/deleteAccount/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function DELETE(req: Request) {
2323
}
2424

2525
// Respond with a success message
26-
return NextResponse.json({ message: "Account successfully deleted" }, { status: 200 });
26+
return NextResponse.json({ success: true, message: "Account successfully deleted" }, { status: 200 });
2727
} catch (error) {
2828
console.error("Error deleting account:", error);
2929
return NextResponse.json({ error: "Server error" }, { status: 500 });

dbConfig.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
// dbConfig.ts - establish a connection to a MongoDB database
2-
import mongoose from 'mongoose';
1+
// dbConfig.ts
2+
import mongoose from "mongoose";
33

4-
export async function connect() {
5-
try {
6-
if (!process.env.MONGO_DB_URL) {
7-
throw new Error("MONGO_DB_URL is not defined. Check your environment variables.");
8-
}
4+
let isConnected = false; // track connection status
95

10-
await mongoose.connect(process.env.MONGO_DB_URL, {
11-
} as mongoose.ConnectOptions);
6+
export async function connect() {
7+
if (isConnected) return; // singleton pattern
128

13-
const connection = mongoose.connection;
9+
const uri = process.env.MONGO_DB_URL;
10+
if (!uri) throw new Error("MONGO_DB_URL is not defined");
1411

15-
connection.on('connected', () => {
16-
console.log("MongoDB connected successfully");
17-
});
12+
try {
13+
await mongoose.connect(uri);
14+
console.log("MongoDB connected");
15+
isConnected = true;
16+
} catch (err) {
17+
console.error("MongoDB connection error:", err);
18+
throw err;
19+
}
20+
}
1821

19-
connection.on('error', (err) => {
20-
console.error("MongoDB connection error:", err);
21-
process.exit(1);
22-
});
23-
} catch (error) {
24-
console.error("MongoDB connection failed:", error);
25-
process.exit(1);
26-
}
22+
export async function disconnect() {
23+
if (!isConnected) return;
24+
await mongoose.disconnect();
25+
isConnected = false;
2726
}

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ module.exports = {
88
'^.+\\.tsx?$': 'ts-jest',
99
},
1010
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
11+
12+
moduleNameMapper: {
13+
'^@/(.*)$': '<rootDir>/$1', // Map @/ to the project root
14+
},
15+
16+
setupFilesAfterEnv: ["<rootDir>/tests/setup.ts"],
1117
};

0 commit comments

Comments
 (0)