|
| 1 | +import Event from './event'; |
| 2 | +import EventDelivery from './event-delivery'; |
| 3 | + |
| 4 | +describe('EventDelivery', () => { |
| 5 | + global.fetch = jest.fn(); |
| 6 | + const sdkKey = 'test-sdk-key'; |
| 7 | + const ingestionUrl = 'https://test-ingestion.url'; |
| 8 | + const testBatch: Event[] = [ |
| 9 | + { uuid: '1', timestamp: Date.now(), type: 'test_event', payload: { key: 'value1' } }, |
| 10 | + { uuid: '2', timestamp: Date.now(), type: 'test_event', payload: { key: 'value2' } }, |
| 11 | + { uuid: '3', timestamp: Date.now(), type: 'test_event', payload: { key: 'value3' } }, |
| 12 | + ]; |
| 13 | + let eventDelivery: EventDelivery; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + eventDelivery = new EventDelivery(sdkKey, ingestionUrl); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should deliver events successfully when response is OK', async () => { |
| 21 | + const mockResponse = { ok: true, json: async () => ({}) }; |
| 22 | + (global.fetch as jest.Mock).mockResolvedValue(mockResponse); |
| 23 | + const result = await eventDelivery.deliver(testBatch); |
| 24 | + expect(result).toEqual({ failedEvents: [] }); |
| 25 | + expect(global.fetch).toHaveBeenCalledWith(ingestionUrl, { |
| 26 | + method: 'POST', |
| 27 | + headers: { 'Content-Type': 'application/json', 'x-eppo-token': sdkKey }, |
| 28 | + body: JSON.stringify({ eppo_events: testBatch }), |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return failed result if response is not OK', async () => { |
| 33 | + const mockResponse = { ok: false }; |
| 34 | + (global.fetch as jest.Mock).mockResolvedValue(mockResponse); |
| 35 | + const result = await eventDelivery.deliver(testBatch); |
| 36 | + expect(result).toEqual({ failedEvents: testBatch }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return failed events when response includes failed events', async () => { |
| 40 | + const failedEvents = ['1', '2']; |
| 41 | + const mockResponse = { ok: true, json: async () => ({ failed_events: failedEvents }) }; |
| 42 | + (global.fetch as jest.Mock).mockResolvedValue(mockResponse); |
| 43 | + const result = await eventDelivery.deliver(testBatch); |
| 44 | + expect(result).toEqual({ failedEvents: [testBatch[0], testBatch[1]] }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return success=true if no failed events in the response', async () => { |
| 48 | + const mockResponse = { ok: true, json: async () => ({}) }; |
| 49 | + (global.fetch as jest.Mock).mockResolvedValue(mockResponse); |
| 50 | + const result = await eventDelivery.deliver(testBatch); |
| 51 | + expect(result).toEqual({ failedEvents: [] }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle fetch errors gracefully', async () => { |
| 55 | + (global.fetch as jest.Mock).mockRejectedValue(new Error('Network error')); |
| 56 | + const result = await eventDelivery.deliver(testBatch); |
| 57 | + expect(result).toEqual({ failedEvents: testBatch }); |
| 58 | + }); |
| 59 | +}); |
0 commit comments