| 1 |
import { getSiteStrings } from '@launch/api/DataApi'; |
| 2 |
|
| 3 |
const fallback = { aiHeaders: [], aiBlogTitles: [] }; |
| 4 |
const originalFetch = global.fetch; |
| 5 |
|
| 6 |
describe('getSiteStrings', () => { |
| 7 |
beforeEach(() => { |
| 8 |
global.fetch = jest.fn(); |
| 9 |
}); |
| 10 |
|
| 11 |
afterEach(() => { |
| 12 |
jest.clearAllMocks(); |
| 13 |
global.fetch = originalFetch; |
| 14 |
}); |
| 15 |
|
| 16 |
it('returns parsed JSON when fetch succeeds with valid data', async () => { |
| 17 |
const mockResponse = { |
| 18 |
aiHeaders: ['Header 1', 'Header 2'], |
| 19 |
aiBlogTitles: ['Post A', 'Post B'], |
| 20 |
}; |
| 21 |
global.fetch.mockResolvedValueOnce({ |
| 22 |
ok: true, |
| 23 |
json: () => mockResponse, |
| 24 |
}); |
| 25 |
|
| 26 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 27 |
expect(result).toEqual(mockResponse); |
| 28 |
expect(global.fetch).toHaveBeenCalledTimes(1); |
| 29 |
}); |
| 30 |
|
| 31 |
it('returns fallback when first fetch throws and second returns not ok', async () => { |
| 32 |
global.fetch |
| 33 |
.mockRejectedValueOnce(new Error('Network error')) |
| 34 |
.mockResolvedValueOnce({ ok: false }); |
| 35 |
|
| 36 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 37 |
expect(result).toEqual(fallback); |
| 38 |
expect(global.fetch).toHaveBeenCalledTimes(2); |
| 39 |
}); |
| 40 |
|
| 41 |
it('returns fallback when both fetch attempts throw', async () => { |
| 42 |
global.fetch |
| 43 |
.mockRejectedValueOnce(new Error('Network error')) |
| 44 |
.mockRejectedValueOnce(new Error('Still failing')); |
| 45 |
|
| 46 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 47 |
expect(result).toEqual(fallback); |
| 48 |
expect(global.fetch).toHaveBeenCalledTimes(2); |
| 49 |
}); |
| 50 |
|
| 51 |
it('returns fallback when response is ok but JSON parsing fails', async () => { |
| 52 |
global.fetch.mockResolvedValueOnce({ |
| 53 |
ok: true, |
| 54 |
json: () => { |
| 55 |
throw new Error('Invalid JSON'); |
| 56 |
}, |
| 57 |
}); |
| 58 |
|
| 59 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 60 |
expect(result).toEqual(fallback); |
| 61 |
expect(global.fetch).toHaveBeenCalledTimes(1); |
| 62 |
}); |
| 63 |
|
| 64 |
it('returns fallback when response is ok but payload lacks aiHeaders', async () => { |
| 65 |
global.fetch.mockResolvedValueOnce({ |
| 66 |
ok: true, |
| 67 |
json: () => ({ aiBlogTitles: ['Only titles, no headers'] }), |
| 68 |
}); |
| 69 |
|
| 70 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 71 |
expect(result).toEqual(fallback); |
| 72 |
expect(global.fetch).toHaveBeenCalledTimes(1); |
| 73 |
}); |
| 74 |
|
| 75 |
it('returns fallback when fetch resolves with undefined response', async () => { |
| 76 |
global.fetch.mockResolvedValueOnce(undefined); |
| 77 |
|
| 78 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 79 |
expect(result).toEqual(fallback); |
| 80 |
expect(global.fetch).toHaveBeenCalledTimes(1); |
| 81 |
}); |
| 82 |
|
| 83 |
it('returns fallback when response ok=false without throwing (no retry by design)', async () => { |
| 84 |
global.fetch.mockResolvedValueOnce({ ok: false }); |
| 85 |
|
| 86 |
const result = await getSiteStrings({ foo: 'bar' }); |
| 87 |
expect(result).toEqual(fallback); |
| 88 |
expect(global.fetch).toHaveBeenCalledTimes(1); |
| 89 |
}); |
| 90 |
}); |
| 91 |
|