diff --git a/src/__tests__/ScrapingService.test.ts b/src/__tests__/ScrapingService.test.ts index b658cb1..1030003 100644 --- a/src/__tests__/ScrapingService.test.ts +++ b/src/__tests__/ScrapingService.test.ts @@ -135,5 +135,42 @@ describe('ScrapingService', () => { expect(mockFeedRepository.create).not.toHaveBeenCalled(); expect(result).toBeNull(); }); + + test('should process multiple feed items and return results', async () => { + const feedItems = [ + { + title: 'News 1', + description: 'Description 1', + url: 'https://example.com/news1', + source: 'El País' as any, + publishedAt: new Date(), + isManual: false + }, + { + title: 'News 2', + description: 'Description 2', + url: 'https://example.com/news2', + source: 'El País' as any, + publishedAt: new Date(), + isManual: false + } + ]; + + const savedFeeds = [ + { _id: '1', ...feedItems[0] }, + { _id: '2', ...feedItems[1] } + ]; + + mockFeedRepository.findByUrl.mockResolvedValue(null); + mockFeedRepository.create.mockResolvedValueOnce(savedFeeds[0]).mockResolvedValueOnce(savedFeeds[1]); + + const results = await scrapingService.processFeedBatch(feedItems); + + expect(mockFeedRepository.findByUrl).toHaveBeenCalledTimes(2); + expect(mockFeedRepository.create).toHaveBeenCalledTimes(2); + expect(results).toHaveLength(2); + expect(results[0]).toEqual(savedFeeds[0]); + expect(results[1]).toEqual(savedFeeds[1]); + }); }); }); \ No newline at end of file diff --git a/src/services/ScrapingService.ts b/src/services/ScrapingService.ts index cb14fbd..2b1fdc5 100644 --- a/src/services/ScrapingService.ts +++ b/src/services/ScrapingService.ts @@ -32,4 +32,13 @@ export class ScrapingService { } return await this.saveFeedItem(feedData); } + + async processFeedBatch(feedItems: Omit[]): Promise<(IFeed | null)[]> { + const results: (IFeed | null)[] = []; + for (const feedItem of feedItems) { + const result = await this.saveIfNotExists(feedItem); + results.push(result); + } + return results; + } } \ No newline at end of file