-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished writing tests for eventRoutes
- Loading branch information
1 parent
d2fc392
commit e54ca51
Showing
3 changed files
with
68 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |