import { configureStore } from '@reduxjs/toolkit';
import { GuarantorSlice, fetchGuarantors } from '../GuarantorSlice';

jest.mock('@/services/api/api', () => ({ apiGet: jest.fn(), apiPost: jest.fn(), apiPut: jest.fn(), apiDelete: jest.fn() }));
jest.mock('antd', () => ({ message: { success: jest.fn(), error: jest.fn(), warning: jest.fn() } }));
jest.mock('@/utilities/Helpers', () => ({ default: { handleServerError: jest.fn((e) => e?.message || 'Error') } }));

const createStore = () => configureStore({ reducer: { guarantors: GuarantorSlice.reducer } });

describe('GuarantorSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().guarantors;
    expect(state.guarantors).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.deleting).toBe(false);
    expect(state.error).toBeNull();
  });

  it('sets loading on fetchGuarantors.pending', () => {
    const store = createStore();
    store.dispatch(fetchGuarantors.pending('req', 'emp-1'));
    expect(store.getState().guarantors.loading).toBe(true);
  });

  it('populates data on fetchGuarantors.fulfilled', () => {
    const store = createStore();
    const data = [{ id: '1', name: 'Guarantor' }] as any;
    store.dispatch(fetchGuarantors.fulfilled(data, 'req', 'emp-1'));
    expect(store.getState().guarantors.guarantors).toEqual(data);
    expect(store.getState().guarantors.loading).toBe(false);
  });
});
