/**
 * @author: Hemal
 * File: getLocationList.test.ts
 * Purpose: This file contains unit tests for the `getLocationList` asynchronous thunk action.
 * Description:
 * - Mocks the `apiClient` module with Jest.
 * - Tests API success and failure scenarios.
 */

jest.mock('../../src/interceptor/apiClient');

import apiClient from '../../src/interceptor/apiClient';
import { API_ENDPOINTS } from '@/src/utils/commonVariables';

const mockedApi = apiClient as jest.Mocked<typeof apiClient>;

describe('getLocationList thunk', () => {
    const mockDispatch = jest.fn();

    /**
     * Thunk to get location list.
     * @param payload - API request body
     */
    const getLocationList =
        (payload: any) =>
            async (dispatch: any) => {
                try {
                    dispatch({ type: 'REQUEST_LOCATION_LIST' });
                    const response = await apiClient.post(API_ENDPOINTS.LIST_LOCATION, payload);
                    dispatch({
                        type: 'SUCCESS_LOCATION_LIST',
                        payload: response?.data,
                    });
                } catch (error) {
                    dispatch({ type: 'FAILURE_LOCATION_LIST' });
                }
            };

    const mockResponseData = {
        data: {
            statusCode: 200,
            data: [{
                title: "Wien, Niederösterreich",
                propertyLocation: {
                    zip: "2295",
                    city: "Wien",
                    state: "Niederösterreich",
                    countryId: "7a036bec-3674-4a9e-a084-f1ca19e8980b",
                    streetName: "Straße Ohne Straßennamen",
                    unitNumber: "75",
                    countryName: "Austria",
                    additionalAddressInfo: "Near school"
                },
                coordinates: [
                    16.821295889327047,
                    48.2946696575986
                ]
            }],
            meta: { status: 1, message: 'Property location list fetched successfully', totalCount: 1 },
        },
    };

    beforeEach(() => {
        jest.clearAllMocks();
    });

    //Test: Successful API call
    it('should dispatch success actions when API call succeeds', async () => {
        mockedApi.post.mockResolvedValueOnce({ data: mockResponseData });

        const payload = { page: 1, perPage: 10 };

        await getLocationList(payload)(mockDispatch);

        expect(mockedApi.post).toHaveBeenCalledWith(API_ENDPOINTS.LIST_LOCATION, payload);
        expect(mockDispatch).toHaveBeenCalledWith({ type: 'REQUEST_LOCATION_LIST' });
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'SUCCESS_LOCATION_LIST',
            payload: mockResponseData,
        });
    });

    //Test: Failed API call
    it('should dispatch failure action on API error', async () => {
        mockedApi.post.mockRejectedValueOnce(new Error('API error'));

        const payload = { page: 1, perPage: 10 };

        await getLocationList(payload)(mockDispatch);

        expect(mockDispatch).toHaveBeenCalledWith({ type: 'REQUEST_LOCATION_LIST' });
        expect(mockDispatch).toHaveBeenCalledWith({ type: 'FAILURE_LOCATION_LIST' });
    });
});
