PluginProbe
Extendify / 3.0.4
Extendify v3.0.4
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Launch / api / __tests__ / getSiteStrings.test.js

getSiteStrings.test.js in Extendify 3.0.4, at src/Launch/api/__tests__/getSiteStrings.test.js

91 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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