Skip to content

Commit

Permalink
finished writing tests for eventRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunbalaji99 committed Jan 19, 2025
1 parent d2fc392 commit e54ca51
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
11 changes: 11 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 3 additions & 10 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
54 changes: 54 additions & 0 deletions server/tests/eventRoutes.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit e54ca51

Please sign in to comment.