diff --git a/server/app.js b/server/app.js new file mode 100644 index 0000000..6af08e0 --- /dev/null +++ b/server/app.js @@ -0,0 +1,11 @@ +const express = require('express'); +const cors = require('cors'); +const eventRoutes = require('./routes/eventRoutes'); + +const app = express(); + +app.use(cors()); +app.use(express.json()); +app.use('/api/events', eventRoutes); + +module.exports = app; \ No newline at end of file diff --git a/server/index.js b/server/index.js index ad6d71a..9251bb5 100644 --- a/server/index.js +++ b/server/index.js @@ -1,14 +1,7 @@ -const express = require('express'); -const cors = require('cors'); -require('dotenv').config(); +const app = require('./app'); const mongoose = require('mongoose'); -const eventRoutes = require('./routes/eventRoutes'); - -const app = express(); - -app.use(cors()); -app.use(express.json()); -app.use('/api/events', eventRoutes); +const dotenv = require('dotenv'); +dotenv.config({ path: '.env.local' }); mongoose.connect(process.env.MONGO_URI) .then(() => { diff --git a/server/tests/eventRoutes.test.js b/server/tests/eventRoutes.test.js new file mode 100644 index 0000000..ec199fe --- /dev/null +++ b/server/tests/eventRoutes.test.js @@ -0,0 +1,54 @@ +const request = require('supertest'); +const mongoose = require('mongoose'); +const app = require('../app'); +const dotenv = require('dotenv'); +dotenv.config({ path: '.env.local' }); + +beforeAll(async () => { + await mongoose.connect(process.env.MONGO_URI); +}); + +afterEach(async () => { + const collections = await mongoose.connection.db.collections(); + for (let collection of collections) { + await collection.deleteMany(); + } +}); + +afterAll(async () => { + await mongoose.disconnect(); +}); + +describe('Event API', () => { + test('GET /api/events should return an empty array initially', async () => { + const res = await request(app).get('/api/events'); + expect(res.statusCode).toBe(200); + expect(res.body).toEqual([]); + }); + + test('POST /api/events should create a new event', async () => { + const newEvent = { + title: 'Conference 2025', + startTime: '2025-03-15T10:00:00.000Z', + endTime: '2025-03-15T12:00:00.000Z', + }; + + const res = await request(app).post('/api/events').send(newEvent); + expect(res.statusCode).toBe(201); + expect(res.body.title).toBe(newEvent.title); + + const getRes = await request(app).get('/api/events'); + expect(getRes.body.length).toBe(1); + expect(getRes.body[0].title).toBe(newEvent.title); + }); + + test('POST /api/events should validate required fields', async () => { + const invalidEvent = { + title: 'Invalid Event', + }; + + const res = await request(app).post('/api/events').send(invalidEvent); + expect(res.statusCode).toBe(400); + expect(res.body.message).toBeDefined(); + }); +});