| 1 |
import { getSiteLogo } from '@launch/api/DataApi'; |
| 2 |
|
| 3 |
const fallbackUrl = |
| 4 |
'https://images.extendify-cdn.com/demo-content/logos/ext-custom-logo-default.webp'; |
| 5 |
|
| 6 |
const originalFetch = global.fetch; |
| 7 |
|
| 8 |
describe('getSiteLogo', () => { |
| 9 |
beforeEach(() => { |
| 10 |
global.fetch = jest.fn(); |
| 11 |
|
| 12 |
window.extSharedData = Object.assign({}, window.extSharedData, { |
| 13 |
siteId: 'test-site', |
| 14 |
partnerId: 'test-partner', |
| 15 |
showAILogo: true, |
| 16 |
}); |
| 17 |
}); |
| 18 |
|
| 19 |
afterEach(() => { |
| 20 |
jest.clearAllMocks(); |
| 21 |
global.fetch = originalFetch; |
| 22 |
}); |
| 23 |
|
| 24 |
it('returns logoUrl on successful fetch and valid JSON', async () => { |
| 25 |
global.fetch.mockResolvedValueOnce({ |
| 26 |
ok: true, |
| 27 |
json: () => ({ logoUrl: 'https://cdn/image.png' }), |
| 28 |
}); |
| 29 |
|
| 30 |
const result = await getSiteLogo('test-logo'); |
| 31 |
expect(result).toBe('https://cdn/image.png'); |
| 32 |
expect(global.fetch).toHaveBeenCalledTimes(1); |
| 33 |
}); |
| 34 |
|
| 35 |
it('returns fallback when showAILogo is false', async () => { |
| 36 |
global.window.extSharedData = { |
| 37 |
siteId: 'test-site', |
| 38 |
partnerId: 'test-partner', |
| 39 |
showAILogo: false, |
| 40 |
}; |
| 41 |
|
| 42 |
const result = await getSiteLogo('test-logo'); |
| 43 |
expect(result).toBe(fallbackUrl); |
| 44 |
expect(global.fetch).not.toHaveBeenCalled(); |
| 45 |
}); |
| 46 |
|
| 47 |
it('throws error when required parameters are missing', async () => { |
| 48 |
global.window.extSharedData = { |
| 49 |
siteId: '', |
| 50 |
partnerId: 'test-partner', |
| 51 |
showAILogo: true, |
| 52 |
}; |
| 53 |
|
| 54 |
await expect(getSiteLogo('test-logo')).rejects.toThrow( |
| 55 |
'Missing required parameter (siteId, partnerId or objectName)', |
| 56 |
); |
| 57 |
}); |
| 58 |
|
| 59 |
it('returns fallback on failed fetch and retry (non-ok response)', async () => { |
| 60 |
global.fetch |
| 61 |
.mockRejectedValueOnce(new Error('Network error')) |
| 62 |
.mockResolvedValueOnce({ ok: false }); |
| 63 |
|
| 64 |
const result = await getSiteLogo('test-logo'); |
| 65 |
expect(result).toBe(fallbackUrl); |
| 66 |
expect(global.fetch).toHaveBeenCalledTimes(2); |
| 67 |
}); |
| 68 |
|
| 69 |
it('returns fallback on JSON parse error', async () => { |
| 70 |
global.fetch.mockResolvedValueOnce({ |
| 71 |
ok: true, |
| 72 |
json: () => { |
| 73 |
throw new Error('Invalid JSON'); |
| 74 |
}, |
| 75 |
}); |
| 76 |
|
| 77 |
const result = await getSiteLogo('test-logo'); |
| 78 |
expect(result).toBe(fallbackUrl); |
| 79 |
}); |
| 80 |
|
| 81 |
describe('returns fallback when objectName is falsy', () => { |
| 82 |
it('returns fallback when objectName is null', async () => { |
| 83 |
const result = await getSiteLogo(null); |
| 84 |
expect(result).toBe(fallbackUrl); |
| 85 |
expect(global.fetch).not.toHaveBeenCalled(); |
| 86 |
}); |
| 87 |
|
| 88 |
it('returns fallback when objectName is undefined', async () => { |
| 89 |
const result = await getSiteLogo(undefined); |
| 90 |
expect(result).toBe(fallbackUrl); |
| 91 |
expect(global.fetch).not.toHaveBeenCalled(); |
| 92 |
}); |
| 93 |
|
| 94 |
it('returns fallback when objectName is an empty string', async () => { |
| 95 |
const result = await getSiteLogo(''); |
| 96 |
expect(result).toBe(fallbackUrl); |
| 97 |
expect(global.fetch).not.toHaveBeenCalled(); |
| 98 |
}); |
| 99 |
}); |
| 100 |
}); |
| 101 |
|