PluginProbe
Extendify / 3.0.6
Extendify v3.0.6
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__ / getSiteQuestions.test.js

getSiteQuestions.test.js in Extendify 3.0.6, at src/Launch/api/__tests__/getSiteQuestions.test.js

92 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { getSiteQuestions } from '@launch/api/DataApi';
2
3 const fallback = { questions: [] };
4 const originalFetch = global.fetch;
5 const mockUseUserSelectionStore = {
6 getState: jest.fn(),
7 };
8
9 describe('getSiteQuestions', () => {
10 beforeEach(() => {
11 global.fetch = jest.fn();
12 global.window = Object.create(window);
13 global.window.extSharedData = { wpLanguage: 'en' };
14 mockUseUserSelectionStore.getState.mockReturnValue({
15 businessInformation: { description: 'My business' },
16 siteObjective: 'Grow',
17 });
18 });
19
20 afterEach(() => {
21 jest.clearAllMocks();
22 global.fetch = originalFetch;
23 });
24
25 it('returns fallback when siteProfile is falsy', async () => {
26 const result = await getSiteQuestions({ siteProfile: null });
27 expect(result).toEqual(fallback);
28 expect(global.fetch).not.toHaveBeenCalled();
29 });
30
31 it('returns parsed data when fetch succeeds', async () => {
32 const mockData = { questions: ['Q1', 'Q2'] };
33 global.fetch.mockResolvedValueOnce({
34 ok: true,
35 json: () => mockData,
36 });
37
38 const result = await getSiteQuestions({ siteProfile: { id: 1 } });
39 expect(result).toEqual(mockData.questions);
40 expect(global.fetch).toHaveBeenCalledTimes(1);
41 });
42
43 it('returns fallback when first fetch throws and second fetch returns not ok', async () => {
44 global.fetch
45 .mockRejectedValueOnce(new Error('Network error'))
46 .mockResolvedValueOnce({ ok: false });
47
48 const result = await getSiteQuestions({ siteProfile: { id: 1 } });
49 expect(result).toEqual(fallback);
50 expect(global.fetch).toHaveBeenCalledTimes(2);
51 });
52
53 it('returns fallback when both fetch attempts throw', async () => {
54 global.fetch
55 .mockRejectedValueOnce(new Error('Network error'))
56 .mockRejectedValueOnce(new Error('Still failing'));
57
58 const result = await getSiteQuestions({ siteProfile: { id: 1 } });
59 expect(result).toEqual(fallback);
60 expect(global.fetch).toHaveBeenCalledTimes(2);
61 });
62
63 it('returns fallback when response is ok but JSON parsing fails', async () => {
64 global.fetch.mockResolvedValueOnce({
65 ok: true,
66 json: () => {
67 throw new Error('Invalid JSON');
68 },
69 });
70
71 const result = await getSiteQuestions({ siteProfile: { id: 1 } });
72 expect(result).toEqual(fallback);
73 expect(global.fetch).toHaveBeenCalledTimes(1);
74 });
75
76 it('returns fallback when response ok=false (retries by design)', async () => {
77 global.fetch.mockResolvedValueOnce({ ok: false });
78
79 const result = await getSiteQuestions({ siteProfile: { id: 1 } });
80 expect(result).toEqual(fallback);
81 expect(global.fetch).toHaveBeenCalledTimes(2);
82 });
83
84 it('returns fallback when fetch resolves undefined (retries by design)', async () => {
85 global.fetch.mockResolvedValueOnce(undefined);
86
87 const result = await getSiteQuestions({ siteProfile: { id: 1 } });
88 expect(result).toEqual(fallback);
89 expect(global.fetch).toHaveBeenCalledTimes(2);
90 });
91 });
92