Files
adopt-a-street/backend/__tests__/routes/events.test.js
William Valentin 56c2292797 fix: add CouchDB mocking to all route tests
- Add comprehensive CouchDB service mocks to all route test files
- Include all required service methods (find, create, update, etc.)
- Add beforeEach cleanup to ensure mock state is reset
- Consistent mocking pattern across all route tests

🤖 Generated with [AI Assistant]

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
2025-11-01 13:54:54 -07:00

180 lines
5.5 KiB
JavaScript

const request = require('supertest');
const express = require('express');
// Mock CouchDB service before importing routes
jest.mock('../../services/couchdbService', () => ({
initialize: jest.fn().mockResolvedValue(true),
create: jest.fn(),
getById: jest.fn(),
find: jest.fn(),
createDocument: jest.fn(),
updateDocument: jest.fn(),
deleteDocument: jest.fn(),
findByType: jest.fn(),
findUserById: jest.fn(),
update: jest.fn(),
}));
const eventsRoutes = require('../../routes/events');
const Event = require('../../models/Event');
const { createTestUser, createTestEvent } = require('../utils/testHelpers');
const couchdbService = require('../../services/couchdbService');
// Create Express app for testing
const app = express();
app.use(express.json());
app.use('/api/events', eventsRoutes);
describe('Events Routes', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('GET /api/events', () => {
it('should get all events', async () => {
const { user } = await createTestUser();
await createTestEvent(user.id, { title: 'Event 1' });
await createTestEvent(user.id, { title: 'Event 2' });
const response = await request(app)
.get('/api/events')
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(2);
expect(response.body[0]).toHaveProperty('title');
});
it('should return empty array when no events exist', async () => {
const response = await request(app)
.get('/api/events')
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(0);
});
});
describe('POST /api/events', () => {
it('should create a new event with authentication', async () => {
const { token } = await createTestUser();
const eventData = {
title: 'Community Cleanup',
description: 'Annual community cleanup event',
date: new Date(Date.now() + 86400000),
location: 'Central Park',
};
const response = await request(app)
.post('/api/events')
.set('x-auth-token', token)
.send(eventData)
.expect(200);
expect(response.body).toHaveProperty('_id');
expect(response.body.title).toBe(eventData.title);
expect(response.body.description).toBe(eventData.description);
expect(response.body.location).toBe(eventData.location);
// Verify event was created in database
const event = await Event.findById(response.body._id);
expect(event).toBeTruthy();
expect(event.title).toBe(eventData.title);
});
it('should reject event creation without authentication', async () => {
const eventData = {
title: 'Unauthorized Event',
description: 'This should fail',
date: new Date(Date.now() + 86400000),
location: 'Nowhere',
};
const response = await request(app)
.post('/api/events')
.send(eventData)
.expect(401);
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
});
it('should handle missing required fields', async () => {
const { token } = await createTestUser();
const response = await request(app)
.post('/api/events')
.set('x-auth-token', token)
.send({ title: 'Incomplete Event' })
.expect(500);
expect(response.body).toBeDefined();
});
});
describe('PUT /api/events/rsvp/:id', () => {
it('should allow user to RSVP to an event', async () => {
const { user, token } = await createTestUser();
const event = await createTestEvent(user.id);
const response = await request(app)
.put(`/api/events/rsvp/${event.id}`)
.set('x-auth-token', token)
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body).toContain(user.id);
// Verify event was updated in database
const updatedEvent = await Event.findById(event.id);
expect(updatedEvent.participants).toContain(user._id);
});
it('should not allow duplicate RSVPs', async () => {
const { user, token } = await createTestUser();
const event = await createTestEvent(user.id, {
participants: [user.id],
});
const response = await request(app)
.put(`/api/events/rsvp/${event.id}`)
.set('x-auth-token', token)
.expect(400);
expect(response.body).toHaveProperty('msg', 'Already RSVPed');
});
it('should return 404 for non-existent event', async () => {
const { token } = await createTestUser();
const fakeId = '507f1f77bcf86cd799439011';
const response = await request(app)
.put(`/api/events/rsvp/${fakeId}`)
.set('x-auth-token', token)
.expect(404);
expect(response.body).toHaveProperty('msg', 'Event not found');
});
it('should reject RSVP without authentication', async () => {
const { user } = await createTestUser();
const event = await createTestEvent(user.id);
const response = await request(app)
.put(`/api/events/rsvp/${event.id}`)
.expect(401);
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
});
it('should handle invalid event ID format', async () => {
const { token } = await createTestUser();
const response = await request(app)
.put('/api/events/rsvp/invalid-id')
.set('x-auth-token', token)
.expect(500);
expect(response.body).toBeDefined();
});
});
});