import { configureStore } from '@reduxjs/toolkit';
import LeaveDashboardSlice, { fetchLeaveDashboard } from '../LeaveDashboardSlice';

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: { leaveDashboard: LeaveDashboardSlice.reducer } });

describe('LeaveDashboardSlice', () => {
  it('has correct initial state', () => {
    const state = createStore().getState().leaveDashboard;
    expect(state.dashboard).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

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

  it('populates dashboard on fetchLeaveDashboard.fulfilled', () => {
    const store = createStore();
    const data = { total_employees: 50, on_leave: 5 } as any;
    store.dispatch(fetchLeaveDashboard.fulfilled(data, 'req', undefined));
    expect(store.getState().leaveDashboard.dashboard).toEqual(data);
    expect(store.getState().leaveDashboard.loading).toBe(false);
  });
});
