<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
import { getSiteQuestions } from '@launch/api/DataApi';

const fallback = { questions: [] };
const originalFetch = global.fetch;
const mockUseUserSelectionStore = {
	getState: jest.fn(),
};

describe('getSiteQuestions', () => {
	beforeEach(() => {
		global.fetch = jest.fn();
		global.window = Object.create(window);
		global.window.extSharedData = { wpLanguage: 'en' };
		mockUseUserSelectionStore.getState.mockReturnValue({
			businessInformation: { description: 'My business' },
			siteObjective: 'Grow',
		});
	});

	afterEach(() => {
		jest.clearAllMocks();
		global.fetch = originalFetch;
	});

	it('returns fallback when siteProfile is falsy', async () => {
		const result = await getSiteQuestions({ siteProfile: null });
		expect(result).toEqual(fallback);
		expect(global.fetch).not.toHaveBeenCalled();
	});

	it('returns parsed data when fetch succeeds', async () => {
		const mockData = { questions: ['Q1', 'Q2'] };
		global.fetch.mockResolvedValueOnce({
			ok: true,
			json: () => mockData,
		});

		const result = await getSiteQuestions({ siteProfile: { id: 1 } });
		expect(result).toEqual(mockData.questions);
		expect(global.fetch).toHaveBeenCalledTimes(1);
	});

	it('returns fallback when first fetch throws and second fetch returns not ok', async () => {
		global.fetch
			.mockRejectedValueOnce(new Error('Network error'))
			.mockResolvedValueOnce({ ok: false });

		const result = await getSiteQuestions({ siteProfile: { id: 1 } });
		expect(result).toEqual(fallback);
		expect(global.fetch).toHaveBeenCalledTimes(2);
	});

	it('returns fallback when both fetch attempts throw', async () => {
		global.fetch
			.mockRejectedValueOnce(new Error('Network error'))
			.mockRejectedValueOnce(new Error('Still failing'));

		const result = await getSiteQuestions({ siteProfile: { id: 1 } });
		expect(result).toEqual(fallback);
		expect(global.fetch).toHaveBeenCalledTimes(2);
	});

	it('returns fallback when response is ok but JSON parsing fails', async () => {
		global.fetch.mockResolvedValueOnce({
			ok: true,
			json: () => {
				throw new Error('Invalid JSON');
			},
		});

		const result = await getSiteQuestions({ siteProfile: { id: 1 } });
		expect(result).toEqual(fallback);
		expect(global.fetch).toHaveBeenCalledTimes(1);
	});

	it('returns fallback when response ok=false (retries by design)', async () => {
		global.fetch.mockResolvedValueOnce({ ok: false });

		const result = await getSiteQuestions({ siteProfile: { id: 1 } });
		expect(result).toEqual(fallback);
		expect(global.fetch).toHaveBeenCalledTimes(2);
	});

	it('returns fallback when fetch resolves undefined (retries by design)', async () => {
		global.fetch.mockResolvedValueOnce(undefined);

		const result = await getSiteQuestions({ siteProfile: { id: 1 } });
		expect(result).toEqual(fallback);
		expect(global.fetch).toHaveBeenCalledTimes(2);
	});
});
