Skip to main content
Glama

News Aggregator API

simple-controller.test.ts6.44 kB
/** * Simple focused tests for news controllers with proper mocking */ import { describe, test, expect, beforeEach, jest, beforeAll } from '@jest/globals'; import { Request, Response } from 'express'; // Mock the NewsService jest.mock('../services/news.service', () => { return { NewsService: jest.fn().mockImplementation(() => ({ getTopNews: jest.fn().mockResolvedValue({ data: [ { uuid: 'test-uuid-1', title: 'Test Article 1', description: 'Test description 1', source: 'Test Source', url: 'https://example.com/1', image_url: 'https://example.com/img1.jpg', published_at: new Date().toISOString(), categories: ['general'] } ], meta: { found: 1, returned: 1, limit: 10, page: 1 } }), getAllNews: jest.fn().mockResolvedValue({ data: [ { uuid: 'test-uuid-1', title: 'Test Article 1', description: 'Test description 1', source: 'Test Source', url: 'https://example.com/1', image_url: 'https://example.com/img1.jpg', published_at: new Date().toISOString(), categories: ['general'] } ], meta: { found: 1, returned: 1, limit: 10, page: 1 } }), getNewsByUuid: jest.fn().mockImplementation((uuid) => { if (uuid === 'test-uuid-1') { return Promise.resolve({ data: { uuid: 'test-uuid-1', title: 'Test Article 1', description: 'Test description 1', source: 'Test Source', url: 'https://example.com/1', image_url: 'https://example.com/img1.jpg', published_at: new Date().toISOString(), categories: ['general'] } }); } else { const error = new Error('Article not found'); error.name = 'NotFoundError'; return Promise.reject(error); } }) })) }; }); // Import our controller after the mocks are set up import { NewsController } from '../controllers/news.controller'; import { cacheService } from '../utils/cache'; describe('News Controller Tests', () => { let newsController: NewsController; beforeEach(() => { cacheService.clear(); jest.clearAllMocks(); newsController = new NewsController(); }); test('getTopNews should return top news articles', async () => { // Arrange const req = { query: {} } as unknown as Request; const res = { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response; // Act await newsController.getTopNews(req, res); // Assert expect(res.status).toHaveBeenCalledWith(200); // Use type assertion to access mock properties const mockJson = res.json as jest.Mock; expect(mockJson).toHaveBeenCalled(); const responseData = mockJson.mock.calls[0][0]; expect(responseData.success).toBe(true); expect(Array.isArray(responseData.data)).toBe(true); expect(responseData.data.length).toBeGreaterThan(0); const article = responseData.data[0]; expect(article).toHaveProperty('title'); expect(article).toHaveProperty('uuid'); }); test('getTopNews should handle category filtering', async () => { // Arrange const req = { query: { categories: 'general' } } as unknown as Request; const res = { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response; // Act await newsController.getTopNews(req, res); // Assert expect(res.status).toHaveBeenCalledWith(200); // Use type assertion to access mock properties const mockJson = res.json as jest.Mock; expect(mockJson).toHaveBeenCalled(); const responseData = mockJson.mock.calls[0][0]; expect(responseData.success).toBe(true); expect(Array.isArray(responseData.data)).toBe(true); }); test('getAllNews should return paginated news', async () => { // Arrange const req = { query: { page: '1', limit: '10' } } as unknown as Request; const res = { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response; // Act await newsController.getAllNews(req, res); // Assert expect(res.status).toHaveBeenCalledWith(200); // Use type assertion to access mock properties const mockJson = res.json as jest.Mock; expect(mockJson).toHaveBeenCalled(); const responseData = mockJson.mock.calls[0][0]; expect(responseData.success).toBe(true); expect(Array.isArray(responseData.data)).toBe(true); expect(responseData.meta).toBeDefined(); }); test('getNewsByUuid should return a specific article', async () => { // Arrange const req = { params: { uuid: 'test-uuid-1' } } as unknown as Request; const res = { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response; // Act await newsController.getNewsByUuid(req, res); // Assert expect(res.status).toHaveBeenCalledWith(200); // Use type assertion to access mock properties const mockJson = res.json as jest.Mock; expect(mockJson).toHaveBeenCalled(); const responseData = mockJson.mock.calls[0][0]; expect(responseData.success).toBe(true); expect(responseData.data).toHaveProperty('uuid', 'test-uuid-1'); }); test('getNewsByUuid should return 404 for non-existent article', async () => { // Arrange const req = { params: { uuid: 'non-existent-uuid' } } as unknown as Request; const res = { status: jest.fn().mockReturnThis(), json: jest.fn() } as unknown as Response; // Act await newsController.getNewsByUuid(req, res); // Assert expect(res.status).toHaveBeenCalledWith(404); // Use type assertion to access mock properties const mockJson = res.json as jest.Mock; expect(mockJson).toHaveBeenCalled(); const responseData = mockJson.mock.calls[0][0]; expect(responseData.success).toBe(false); expect(responseData.error).toBeDefined(); }); });

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Malachi-devel/the-news-api-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server