/**
 *  @author: Hemal
 * File: getFAQsList.test.ts
 * Purpose: This file contains unit tests for the `getFAQsList` function.
 * Description:
 * - Utilizes `jest.mock` to mock the `fetch` function.
 * - Tests the behavior of the `getFAQsList` function, including making API calls and handling errors.
 * - Verifies that the function returns the expected response data.
*/

import getFAQsList from "@/src/ssrapi/getFAQsList";
import { FAQResponse, FAQsListPayload } from "@/src/types/api/faqs.type";
import { API_ENDPOINTS } from "@/src/utils/commonVariables";

// Mock payload 
const mockPayload: FAQsListPayload = {
    page: 1,
    perPage: 10
};

// Mock response
const mockResponse: FAQResponse = {
    statusCode: 200,
    meta: { status: 1, message: 'FAQ has been listed successfully.', totalCount: 1 },
    data: [
        {
            faqId: '1',
            title: 'What is FAQs?',
            description: 'FAQs are frequently asked questions',
            slug: 'what-is-faqs',
            createdAt: '2022-01-01T00:00:00.000Z',
            updatedAt: '2022-01-01T00:00:00.000Z'
        }
    ]
};

describe('getFAQsList', () => {
    // Mock fetch
    const originalFetch = global.fetch;

    // Set up mock fetch
    beforeEach(() => {
        global.fetch = jest.fn();
    });

    // Reset mocks
    afterEach(() => {
        jest.resetAllMocks();
    });

    // Restore original fetch
    afterAll(() => {
        global.fetch = originalFetch;
    });

    // Test cases for fetch data for FAQsList for Home page
    it('should call fetch with correct arguments and return parsed JSON', async () => {
        (global.fetch as jest.Mock).mockResolvedValueOnce({
            json: jest.fn().mockResolvedValueOnce(mockResponse)
        });

        const response = await getFAQsList(mockPayload);

        expect(global.fetch).toHaveBeenCalledWith(
            `${process.env.NEXT_PUBLIC_API_URL}${API_ENDPOINTS.LIST_FAQ}`, // update path if needed
            {
                method: 'POST',
                body: JSON.stringify(mockPayload),
                headers: {
                    'Content-Type': 'application/json',
                    'usertype': 'guest'
                },
                cache: 'no-store'
            }
        );

        expect(response).toEqual(mockResponse);
    });

    // Test cases for fetch data for FAQsList for FAQ page
    it('should call fetch with correct arguments and return parsed JSON', async () => {
        (global.fetch as jest.Mock).mockResolvedValueOnce({
            json: jest.fn().mockResolvedValueOnce(mockResponse)
        });

        //get all FAQs
        const mockPayloadData = {
            page: 1,
            perPage: -1
        };

        const response = await getFAQsList(mockPayloadData);

        expect(global.fetch).toHaveBeenCalledWith(
            `${process.env.NEXT_PUBLIC_API_URL}${API_ENDPOINTS.LIST_FAQ}`, // update path if needed
            {
                method: 'POST',
                body: JSON.stringify(mockPayloadData),
                headers: {
                    'Content-Type': 'application/json',
                    'usertype': 'guest'
                },
                cache: 'no-store'
            }
        );

        expect(response).toEqual(mockResponse);
    });

    // Test case for fetch error
    it('should return undefined on fetch error', async () => {
        (global.fetch as jest.Mock).mockRejectedValueOnce(new Error('Network error'));

        const response = await getFAQsList(mockPayload);

        expect(response).toBeUndefined();
    });
});
