From 6895b4d7f2233e4e0746712b9d97020fd1a2b699 Mon Sep 17 00:00:00 2001 From: William Valentin Date: Sat, 1 Nov 2025 13:53:05 -0700 Subject: [PATCH] fix: update posts route test for CouchDB mocking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- backend/__tests__/routes/posts.test.js | 29 ++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/backend/__tests__/routes/posts.test.js b/backend/__tests__/routes/posts.test.js index d3901c1..6119944 100644 --- a/backend/__tests__/routes/posts.test.js +++ b/backend/__tests__/routes/posts.test.js @@ -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); }); });