import { configureStore } from '@reduxjs/toolkit';
import { EmploymentStatusSlice, fetchEmploymentStatuses, createEmploymentStatus } from '../EmploymentStatusSlice';

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: { employmentStatuses: EmploymentStatusSlice.reducer } });

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

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

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

  it('prepends on createEmploymentStatus.fulfilled', () => {
    const store = createStore();
    const item = { id: '2', name: 'Probation' } as any;
    store.dispatch(createEmploymentStatus.fulfilled(item, 'req', {} as any));
    expect(store.getState().employmentStatuses.employmentStatuses[0]).toEqual(item);
  });
});
