news-service.mock.ts•2.16 kB
/**
* Mock implementation of the NewsService
* This prevents tests from making actual API calls to TheNewsAPI
*/
import { jest } from '@jest/globals';
// Sample article data for testing
const MOCK_ARTICLES = [
{
uuid: 'test-uuid-1',
title: 'Test Article 1',
description: 'Test description 1',
url: 'https://example.com/article1',
image_url: 'https://example.com/image1.jpg',
published_at: new Date().toISOString(),
source: 'Test Source',
categories: ['general'],
language: 'en'
},
{
uuid: 'test-uuid-2',
title: 'Test Article 2',
description: 'Test description 2',
url: 'https://example.com/article2',
image_url: 'https://example.com/image2.jpg',
published_at: new Date().toISOString(),
source: 'Test Source',
categories: ['technology'],
language: 'en'
}
];
// Mock the entire NewsService class
export const mockNewsService = {
getTopNews: jest.fn().mockResolvedValue({
data: MOCK_ARTICLES,
meta: {
found: MOCK_ARTICLES.length,
returned: MOCK_ARTICLES.length,
limit: 10,
page: 1
}
}),
getAllNews: jest.fn().mockResolvedValue({
data: MOCK_ARTICLES,
meta: {
found: MOCK_ARTICLES.length,
returned: MOCK_ARTICLES.length,
limit: 10,
page: 1
}
}),
getNewsByUuid: jest.fn().mockImplementation((uuid: string) => {
const article = MOCK_ARTICLES.find(a => a.uuid === uuid);
if (article) {
return Promise.resolve({ data: article });
} else {
return Promise.reject(new Error('Article not found'));
}
}),
getNewsSources: jest.fn().mockResolvedValue({
data: [
{
id: 1,
name: 'Test Source 1',
url: 'https://example.com/source1'
},
{
id: 2,
name: 'Test Source 2',
url: 'https://example.com/source2'
}
],
meta: {
found: 2,
returned: 2,
limit: 10,
page: 1
}
})
};
// Create a jest mock to replace the NewsService module
jest.mock('../../services/news.service', () => {
return {
NewsService: jest.fn().mockImplementation(() => mockNewsService)
};
});