import { configureStore } from '@reduxjs/toolkit';
import { ConfirmationSlice, fetchConfirmationDashboard, fetchConfirmationList } from '../ConfirmationSlice';

jest.mock('@/services/api/api', () => ({ apiGet: jest.fn(), apiPost: 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: { confirmations: ConfirmationSlice.reducer } });

describe('ConfirmationSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().confirmations;
    expect(state.dashboardMetrics).toBeNull();
    expect(state.confirmationList).toEqual([]);
    expect(state.confirmationListPagination).toBeNull();
    expect(state.confirmationHistory).toEqual([]);
    expect(state.loading).toBe(false);
    expect(state.dashboardLoading).toBe(false);
    expect(state.actionLoading).toBe(false);
  });

  it('sets dashboardLoading on fetchConfirmationDashboard.pending', () => {
    const store = createStore();
    store.dispatch(fetchConfirmationDashboard.pending('req'));
    expect(store.getState().confirmations.dashboardLoading).toBe(true);
  });

  it('sets loading on fetchConfirmationList.pending', () => {
    const store = createStore();
    store.dispatch(fetchConfirmationList.pending('req', undefined));
    expect(store.getState().confirmations.loading).toBe(true);
  });
});
