import { call } from 'redux-saga/effects';
import { fromJS } from 'immutable';
import { expectSaga } from 'redux-saga-test-plan';
import { fetchSchemaForEntity } from '../sagas';
import * as api from '../../../services/api';
import { GET_SCHEMA_FOR_ENTITY_SUCCESS } from '../constants';

describe('fetchSchemaForEntity Saga Coverage Test', () => {
  it('should handle success when loyalty module is enabled', () => {
    const loyaltyMock = { isLoyaltyModule: true };

    const inputQueryParams = {
      queryParams: {
        type: 'someType',
      },
    };

    const expectedParams = {
      queryParams: {
        type: 'someType',
        loyaltyTagsParams: loyaltyMock,
      },
    };

    const apiResponse = {
      success: true,
      response: fromJS({ metaEntities: ['x'] }),
      status: { code: '200' },
    };

    const mockReduxState = fromJS({
      creativesContainer: {
        loyaltyTagProps: loyaltyMock,
        containerLoader: false,
      },
    });

    return expectSaga(fetchSchemaForEntity, inputQueryParams)
      .withState(mockReduxState) // ✅ Provides correct state for selector chain
      .provide([
        [call(api.fetchSchemaForEntity, expectedParams), apiResponse]
      ])
      .put({
        type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
        data: apiResponse.response,
        statusCode: '200',
        entityType: 'someType',
      })
      .run();
  });

  it('should handle success when loyalty module is disabled', () => {
    const loyaltyMock = { isLoyaltyModule: false };

    const inputQueryParams = {
      queryParams: {
        type: 'someType',
      },
    };

    const expectedParams = {
      queryParams: {
        type: 'someType',
      },
    };

    const apiResponse = {
      success: true,
      response: fromJS({ metaEntities: ['x'] }),
      status: { code: '200' },
    };

    const mockReduxState = fromJS({
      creativesContainer: {
        loyaltyTagProps: loyaltyMock,
        containerLoader: false,
      },
    });

    return expectSaga(fetchSchemaForEntity, inputQueryParams)
      .withState(mockReduxState) // ✅ Provides correct state for selector chain
      .provide([
        [call(api.fetchSchemaForEntity, expectedParams), apiResponse]
      ])
      .put({
        type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
        data: apiResponse.response,
        statusCode: '200',
        entityType: 'someType',
      })
      .run();
  });
});
