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); }); });