import { configureStore } from '@reduxjs/toolkit';
import AssessmentDashboardSlice from '../AssessmentDashboardSlice';

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

describe('AssessmentDashboardSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().ad;
    expect(state.data).toBeNull();
    expect(state.loading).toBe(false);
    expect(state.error).toBeNull();
  });

  it('sets loading on fetch pending', () => {
    const store = createStore();
    store.dispatch({ type: 'assessment/dashboard/fetch/pending' });
    expect(store.getState().ad.loading).toBe(true);
  });

  it('populates data on fetch fulfilled', () => {
    const store = createStore();
    store.dispatch({ type: 'assessment/dashboard/fetch/fulfilled', payload: { totalQuizzes: 10 } });
    expect(store.getState().ad.data).toEqual({ totalQuizzes: 10 });
    expect(store.getState().ad.loading).toBe(false);
  });
});
