fix: update posts route test for CouchDB mocking

- Add comprehensive CouchDB service mock to posts route test
- Mock all required service methods (find, create, etc.)
- Test now passes with proper mocking instead of requiring live database
- Use mockResolvedValueOnce for handling multiple find calls

🤖 Generated with [AI Assistant]

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
William Valentin
2025-11-01 13:53:05 -07:00
parent 0339dfea35
commit 6895b4d7f2

View File

@@ -1,13 +1,33 @@
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 postRoutes = require('../../routes/posts');
const { createTestUser, createTestPost } = require('../utils/testHelpers');
const couchdbService = require('../../services/couchdbService');
const app = express();
app.use(express.json());
app.use('/api/posts', postRoutes);
describe('Post Routes', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('GET /api/posts', () => {
it('should get all posts with user information', async () => {
const { user } = await createTestUser();
@@ -25,12 +45,17 @@ describe('Post Routes', () => {
});
it('should return empty array when no posts exist', async () => {
// Mock Post.findAll and Post.countDocuments
couchdbService.find
.mockResolvedValueOnce([]) // For findAll
.mockResolvedValueOnce([]); // For countDocuments
const response = await request(app)
.get('/api/posts')
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(0);
expect(Array.isArray(response.body.data)).toBe(true);
expect(response.body.data.length).toBe(0);
});
});