import { Test, TestingModule } from '@nestjs/testing';
import { FeatureflagService } from './featureflag.service';
import {SharedService} from "../shared/shared.service";
import {SharedServiceMock} from "../mocks/shared-service.mock";


describe('FeatureflagService', () => {
  let service: FeatureflagService;
  let sharedService: SharedService;

  beforeEach(async () => {
    jest.clearAllMocks();
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        FeatureflagService,
        { provide: SharedService,useValue: SharedServiceMock },
      ],
    }).compile();

    service = module.get<FeatureflagService>(FeatureflagService);
    sharedService = module.get<SharedService>(SharedService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  it('should set feature flag state', async () => {
    const testFeatureName = 'TestFeatureFlag';
    const testFeatureState = true;
    const setConfigMock = jest
      .spyOn(sharedService,'setConfig')
      .mockImplementation((name: string, value: string) => Promise.resolve({ key: name, value: value}));
    await service.setFeatureFlag(testFeatureName, testFeatureState);
    expect(setConfigMock).toHaveBeenCalledWith(`featureflag/${testFeatureName}`, testFeatureState.toString());
    expect(setConfigMock).toHaveBeenCalled();
  });

  it('should return state of the feature flag', async () => {
    const testFeatureName = 'TestFeatureFlag';
    const testFeatureState = true;
    const getConfigMock = jest
      .spyOn(sharedService, 'getConfig')
      .mockImplementation((key: string) => Promise.resolve({ key:key, value: testFeatureState.toString() }));
    await service.getFeatureFlag(testFeatureName);

    expect(getConfigMock).toHaveBeenCalledTimes(1);
    expect(getConfigMock).toHaveBeenCalledWith(`featureflag/${testFeatureName}`);

  });
});
