import {
  take,
  fork,
  cancel,
  takeLatest,
  call,
} from 'redux-saga/effects';
import { fromJS } from 'immutable';
import { expectSaga } from 'redux-saga-test-plan';
import * as matchers from 'redux-saga-test-plan/matchers';
import { throwError } from 'redux-saga-test-plan/providers';
import {
  authorize,
  loginFlow,
  fetchSchemaForEntity,
  v2CapSagas,
  getSupportVideosConfig,
  watchForGetVideosConfig,
  watchFetchSchemaForEntity,
  watchLiquidEntity,
  getLiquidTags,
  logoutFlow,
  capSagaLiquidEntity,
  createCentralCommsMetaId,
  watchMetaId,
} from '../sagas';
import * as api from '../../../services/api';
import {
  LOGIN_REQUEST,
  LOGIN_FAILURE,
  LOGOUT_REQUEST_V2,
  GET_SCHEMA_FOR_ENTITY_SUCCESS,
  GET_SCHEMA_FOR_ENTITY_FAILURE,
  GET_SUPPORT_VIDEOS_CONFIG_REQUEST,
  GET_SUPPORT_VIDEOS_CONFIG_SUCCESS,
  GET_SUPPORT_VIDEOS_CONFIG_FAILURE,
  GET_SCHEMA_FOR_ENTITY_REQUEST,
  GET_LIQUID_TAGS_SUCCESS,
  GET_LIQUID_TAGS_FAILURE,
  GET_LIQUID_TAGS_REQUEST,
  CREATE_CENTRAL_COMMS_META_ID_SUCCESS,
  CREATE_CENTRAL_COMMS_META_ID_FAILURE,
  CREATE_CENTRAL_COMMS_META_ID_REQUEST,
} from '../constants';
// Mock data imports - these may not exist in the mockdata file
// const { callback, error, error2, videoConfigData } = require('../../mockdata');
import { LOGOUT_FAILURE, LOGOUT_SUCCESS } from '../../../constants/unified';
import { loyaltyTagProps } from '../../CreativesContainer/selectors';

// Mock data definitions
const callback = jest.fn();
const error = new Error('Test error');
const error2 = new Error('Test error 2');
const videoConfigData = { videos: [] };

describe('v2CapSagas', () => {
  it("should initialize all v2CapSagas-related watcher sagas without error", async () => expectSaga(v2CapSagas).run());
});

describe('capSagaLiquidEntity', () => {
  it("should initialize all capSagaLiquidEntity-related watcher sagas without error", () => expectSaga(capSagaLiquidEntity).run());
});

describe('logoutFlow [Unit Test]', () => {
  describe('logoutFlow saga', () => {
    it('handle valid response from api', async () => {
      expectSaga(logoutFlow)
        .provide([
          [
            matchers.call.fn(api.logout),
            {
              success: true,
              message: 'message',
            },
          ],
        ])
        .put({
          type: LOGOUT_SUCCESS,
          result: 'message',
        })
        .run();
    });

    it('handle invalid response from api', async () => {
      expectSaga(logoutFlow)
        .provide([
          [
            matchers.call.fn(api.logout),
            {
              success: false,
              message: 'message',
            },
          ],
        ])
        .run();
    });

    it('handle error response from api', async () => {
      expectSaga(logoutFlow)
        .provide([
          [
            matchers.call.fn(api.logout),
            {
              success: false,
              error,
            },
          ],
        ])
        .put({
          type: LOGOUT_FAILURE,
          error,
        })
        .run();
    });

    it('handles error thrown from api', async () => {
      expectSaga(logoutFlow)
        .provide([[matchers.call.fn(api.logout), throwError(error)]])
        .put({
          type: LOGOUT_FAILURE,
          error,
        })
        .run();
    });
  });
});
describe('loginFlow', () => {
  it('should handle the login flow', () => {
    const generator = loginFlow();
    const user = { username: 'testuser', password: 'password123' };
    // First iteration
    expect(generator.next().value).toEqual(take(LOGIN_REQUEST));
    expect(generator.next({ user }).value).toEqual(fork(authorize, user));
    const task = generator.next().value; // Assign the task value
    try {
      const logoutAction = { type: LOGOUT_REQUEST_V2 };
      const failureAction = { type: LOGIN_FAILURE };

      expect(generator.next(logoutAction).value).toEqual(cancel(task));

      // When LOGIN_FAILURE action is dispatched
      expect(generator.next(failureAction).value).toEqual(take(LOGIN_REQUEST));
      expect(generator.next().value).toEqual(fork(authorize, user));
      expect(generator.next().done).toBe(true);
    } catch {
      // Handle any errors
    }
  });
});

describe("fetchSchemaForEntity", () => {
  it("handle success response from fetchSchemaForEntity", () => {
    expectSaga(fetchSchemaForEntity, {
      queryParams: {
        type: 'tags',
      },
    })
      .provide([
        [
          matchers.call.fn(api.fetchSchemaForEntity),
          {
            success: true,
            response: {},
          },
        ],
      ])
      .put({
        type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
        data: {},
        statusCode: '',
        entityType: 'tags',
      })
      .run();
  });

  it("handle success response from fetchSchemaForEntity when metaEntities available", () => {
    expectSaga(fetchSchemaForEntity, {
      queryParams: {
        type: 'tags',
      },
    })
      .provide([
        [
          matchers.call.fn(api.fetchSchemaForEntity),
          {
            response: {
              metaEntities: [],
            },
          },
        ],
      ])
      .put({
        type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
        data: {
          metaEntities: [],
        },
        statusCode: '',
        entityType: 'tags',
      })
      .run();
  });

  it("handle when response and query params are empty from fetchSchemaForEntity when metaEntities available", () => {
    expectSaga(fetchSchemaForEntity, {})
      .provide([
        [
          matchers.call.fn(api.fetchSchemaForEntity),
          {
            success: true,
          },
        ],
      ])
      .put({
        type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
        data: {},
        statusCode: '',
        entityType: '',
      })
      .run();
  });

  it("handle error response from fetchSchemaForEntity", () => {
    expectSaga(fetchSchemaForEntity, {
      queryParams: {
        type: 'tags',
      },
    })
      .provide([
        [
          matchers.call.fn(api.fetchSchemaForEntity),
          {
            success: false,
            response: {},
          },
        ],
      ])
      .put({
        type: GET_SCHEMA_FOR_ENTITY_FAILURE,
        error: 'Error in fetching tags',
      })
      .run();
  });

  // Loyalty Tag Props Test Cases
  describe('fetchSchemaForEntity with loyalty tag props', () => {
    it('should add loyaltyTagsParams when loyalty module is enabled and data exists', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: true,
        loyaltyMetaData: { tagId: '123', tagName: 'Test Tag' },
        forwardedTags: { custom: 'value' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      const expectedQueryParams = {
        queryParams: {
          type: 'tags',
          loyaltyTagsParams: loyaltyTagPropsData,
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, expectedQueryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when loyalty module is disabled', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: false,
        loyaltyMetaData: { tagId: '123', tagName: 'Test Tag' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when loyaltyTagPropsData is null', () => {
      const loyaltyTagPropsData = null;

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when loyaltyTagPropsData is undefined', () => {
      const loyaltyTagPropsData = undefined;

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when loyaltyTagPropsData is empty object', () => {
      const loyaltyTagPropsData = {};

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when isLoyaltyModule is false even if other data exists', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: false,
        loyaltyMetaData: { tagId: '123', tagName: 'Test Tag' },
        forwardedTags: { custom: 'value' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when isLoyaltyModule is undefined', () => {
      const loyaltyTagPropsData = {
        loyaltyMetaData: { tagId: '123', tagName: 'Test Tag' },
        forwardedTags: { custom: 'value' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should not add loyaltyTagsParams when isLoyaltyModule is null', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: null,
        loyaltyMetaData: { tagId: '123', tagName: 'Test Tag' },
        forwardedTags: { custom: 'value' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should add loyaltyTagsParams when isLoyaltyModule is true even with minimal data', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: true,
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      const expectedQueryParams = {
        queryParams: {
          type: 'tags',
          loyaltyTagsParams: loyaltyTagPropsData,
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, expectedQueryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should handle loyalty tag props with complex nested data structure', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: true,
        loyaltyMetaData: {
          tagId: '123',
          tagName: 'Test Tag',
          nested: {
            level1: {
              level2: 'deep value',
            },
          },
        },
        forwardedTags: {
          custom: 'value',
          array: [1, 2, 3],
          boolean: true,
        },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      const expectedQueryParams = {
        queryParams: {
          type: 'tags',
          loyaltyTagsParams: loyaltyTagPropsData,
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, expectedQueryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should preserve existing queryParams when adding loyaltyTagsParams', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: true,
        loyaltyMetaData: { tagId: '123' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
          existingParam: 'existingValue',
          anotherParam: 123,
        },
      };

      const expectedQueryParams = {
        queryParams: {
          type: 'tags',
          existingParam: 'existingValue',
          anotherParam: 123,
          loyaltyTagsParams: loyaltyTagPropsData,
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, expectedQueryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should handle error response when loyalty module is enabled', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: true,
        loyaltyMetaData: { tagId: '123' },
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      const expectedQueryParams = {
        queryParams: {
          type: 'tags',
          loyaltyTagsParams: loyaltyTagPropsData,
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, expectedQueryParams),
            {
              success: false,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_FAILURE,
          error: 'Error in fetching tags',
        })
        .run();
    });

    it('should add loyaltyTagsParams when isLoyaltyModule is true and loyaltyTagPropsData is empty object (truthy)', () => {
      const loyaltyTagPropsData = {
        isLoyaltyModule: true,
        // Empty object is still truthy, so it should add loyaltyTagsParams
      };

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      const expectedQueryParams = {
        queryParams: {
          type: 'tags',
          loyaltyTagsParams: loyaltyTagPropsData,
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, expectedQueryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should handle the case where loyaltyTagPropsData is 0 (falsy)', () => {
      const loyaltyTagPropsData = 0; // Falsy value

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should handle the case where loyaltyTagPropsData is empty string (falsy)', () => {
      const loyaltyTagPropsData = ''; // Falsy value

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });

    it('should handle the case where loyaltyTagPropsData is false (falsy)', () => {
      const loyaltyTagPropsData = false; // Falsy value

      const queryParams = {
        queryParams: {
          type: 'tags',
        },
      };

      expectSaga(fetchSchemaForEntity, queryParams)
        .provide([
          [matchers.select(loyaltyTagProps()), loyaltyTagPropsData],
          [
            matchers.call.fn(api.fetchSchemaForEntity, queryParams),
            {
              success: true,
              response: {},
            },
          ],
        ])
        .put({
          type: GET_SCHEMA_FOR_ENTITY_SUCCESS,
          data: {},
          statusCode: '',
          entityType: 'tags',
        })
        .run();
    });
  });
});

describe('getSupportVideoConfig saga', () => {
  it('handle valid response from api', () => {
    expectSaga(getSupportVideosConfig, {
      callback,
    })
      .provide([
        [
          matchers.call.fn(api.getSupportVideosConfig),
          {
            success: true,
            status: 200,
            data: videoConfigData,
          },
        ],
      ])
      .put({
        type: GET_SUPPORT_VIDEOS_CONFIG_SUCCESS,
        result: videoConfigData,
      })
      .run();
  });

  it('handle error thrown from api', () => {
    expectSaga(getSupportVideosConfig, {
      data: videoConfigData,
    })
      .provide([
        [matchers.call.fn(api.getSupportVideosConfig), error2],
      ])
      .put({
        type: GET_SUPPORT_VIDEOS_CONFIG_FAILURE,
        error: error2,
      })
      .run();
  });

  it('handles error from catch block', () => {
    expectSaga(getSupportVideosConfig, {
      data: videoConfigData,
    })
      .provide([
        [matchers.call.fn(api.getSupportVideosConfig), throwError(error)],
      ])
      .put({
        type: GET_SUPPORT_VIDEOS_CONFIG_FAILURE,
        error,
      })
      .run();
  });
});

describe('watchForGetVideosConfig saga', () => {
  let generator = null;
  beforeEach(() => {
    generator = watchForGetVideosConfig();
  });
  it('handle valid response from api', () => {
    expect(generator.next().value).toEqual(
      takeLatest(GET_SUPPORT_VIDEOS_CONFIG_REQUEST, getSupportVideosConfig),
    );
  });
});

describe('watchFetchSchemaForEntity saga', () => {
  it('test watchFetchSchemaForEntity', () => {
    const generator = watchFetchSchemaForEntity();
    expect(generator.next().value).toEqual(
      takeLatest(GET_SCHEMA_FOR_ENTITY_REQUEST, fetchSchemaForEntity),
    );
  });
});


describe('getLiquidTags saga', () => {
  it('handles successful API response', () => {
    const action = { data: 'templateContent', callback: () => {} };
    const result = { result: { errors: [{ message: "error in content" }] } };

    expectSaga(getLiquidTags, action)
      .provide([
        [matchers.call.fn(api.getLiquidTags, 'templateContent'), result],
        [matchers.call.fn(api.askAiraForLiquid, { templateContent: 'templateContent', errorMessage: result?.result?.errors[0]?.message }), { result: { errors: [] } }],
      ])
      .call.fn(action.callback, result)
      .put({ type: GET_LIQUID_TAGS_SUCCESS })
      .run();
  });

  it('handles successful API response for askAira', () => {
    const action = { data: 'templateContent', callback: () => {} };
    const result = { result: { errors: [{ message: "error in content" }] } };

    expectSaga(getLiquidTags, action)
      .provide([
        [matchers.call.fn(api.getLiquidTags, 'templateContent'), result],
        [matchers.call.fn(api.askAiraForLiquid, { templateContent: 'templateContent', errorMessage: result?.result?.errors[0]?.message }), { result: { errors: [{ message: "Something went wrong" }] } }],
      ])
      .call.fn(action.callback, result)
      .put({ type: GET_LIQUID_TAGS_SUCCESS })
      .run();
  });

  it('handles API response with errors', () => {
    const action = { data: 'templateContent' };
    const result = { errors: [{ message: 'Error message' }] };

    expectSaga(getLiquidTags, action)
      .provide([
        [matchers.call.fn(api.getLiquidTags, 'templateContent'), result],
        [matchers.call.fn(api.askAiraForLiquid, { templateContent: 'templateContent', errorMessage: result.errors[0].message }), { result: { errors: [{ message: "Something went wrong" }] } }],
      ])
      .not.call.fn(action.callback, result)
      .put({ type: GET_LIQUID_TAGS_FAILURE, error: true })
      .run();
  });
  it('handles API response with errors', () => {
    const action = { data: 'templateContent' };
    const result = { data: [{ message: 'Error message' }] };

    expectSaga(getLiquidTags, action)
      .provide([
        [matchers.call.fn(api.getLiquidTags, 'templateContent'), result],
      ])
      .call.fn(action.callback, result)
      .put({ type: GET_LIQUID_TAGS_SUCCESS })
      .run();
  });
  it('handles API response with undefined', () => {
    const action = { data: 'templateContent' };
    const result = undefined;

    expectSaga(getLiquidTags, action)
      .provide([
        [matchers.call.fn(api.getLiquidTags, 'templateContent'), result],
      ])
      .not.call.fn(action.callback, result)
      .put({ type: GET_LIQUID_TAGS_FAILURE, error: true })
      .run();
  });
  it('handles error thrown during API call', () => {
    const action = { data: 'templateContent' };
    const apiError = new Error('API error');

    expectSaga(getLiquidTags, action)
      .provide([
        [matchers.call.fn(api.getLiquidTags, 'templateContent'), throwError(apiError)],
      ])
      .put({ type: GET_LIQUID_TAGS_FAILURE, error: apiError })
      .run();
  });
});

describe('watchForGetLiquidTags saga', () => {
  const generator = watchLiquidEntity();
  it('should call watchers functions', () => {
    expect(generator.next().value).toEqual(
      takeLatest(GET_LIQUID_TAGS_REQUEST, getLiquidTags),
    );
  });
});

describe('createCentralCommsMetaId saga', () => {
  const mockCallback = jest.fn();

  it('handles successful API response', () => {
    const action = {
      data: { templateId: '123456' },
      callback: mockCallback,
    };
    const result = {
      status: { code: 200 },
      data: { id: '123456' },
    };

    expectSaga(createCentralCommsMetaId, action)
      .provide([
        [matchers.call.fn(api.createCentralCommsMetaId, action.data), result],
      ])
      .call(action.callback, result)
      .put({
        type: CREATE_CENTRAL_COMMS_META_ID_SUCCESS,
        result,
      })
      .run();
  });

  it('handles unsuccessful API response', () => {
    const action = {
      data: { templateId: '123456' },
    };
    const result = {
      status: { code: 400 },
      message: 'Failed to create meta ID',
    };

    expectSaga(createCentralCommsMetaId, action)
      .provide([
        [matchers.call.fn(api.createCentralCommsMetaId, action.data), result],
      ])
      .put({
        type: CREATE_CENTRAL_COMMS_META_ID_FAILURE,
        error: result.message,
      })
      .run();
  });

  it('handles error thrown during API call', () => {
    const action = {
      data: { templateId: '123456' },
    };
    const apiError = new Error('API error');

    expectSaga(createCentralCommsMetaId, action)
      .provide([
        [matchers.call.fn(api.createCentralCommsMetaId, action.data), throwError(apiError)],
      ])
      .put({
        type: CREATE_CENTRAL_COMMS_META_ID_FAILURE,
        error: apiError,
      })
      .run();
  });
});

describe('watchMetaId saga', () => {
  it('should watch for CREATE_CENTRAL_COMMS_META_ID_REQUEST actions', () => {
    const generator = watchMetaId();
    expect(generator.next().value).toEqual(
      takeLatest(CREATE_CENTRAL_COMMS_META_ID_REQUEST, createCentralCommsMetaId),
    );
  });
});
