import { Test, TestingModule } from '@nestjs/testing';
import { FeatureflagController } from './featureflag.controller';
import {FeatureflagService} from "./featureflag.service";
import {FeatureflagServiceMock} from "../mocks/featureflag-service.mock";

describe('FeatureflagController', () => {
  let controller: FeatureflagController;
  let featureflagService: FeatureflagService;

  beforeEach(async () => {
    jest.resetAllMocks();
    const module: TestingModule = await Test.createTestingModule({
      controllers: [FeatureflagController],
      providers: [
        { provide: FeatureflagService, useValue: FeatureflagServiceMock },
      ]
    }).compile();
    controller = module.get<FeatureflagController>(FeatureflagController);
    featureflagService = module.get<FeatureflagService>(FeatureflagService);
  });

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

  it('should call feature flag service to get state', async() => {
    const ffNameToTest = "TestFeature";
    const getFFMock = jest.spyOn(featureflagService, 'getFeatureFlag')
      .mockImplementation(
        (name) =>  Promise.resolve({ name: name, state: true})
      );

    await controller.getFeatureFlag({ ffname: ffNameToTest });

    expect(getFFMock).toHaveBeenCalled();
    expect(getFFMock).toHaveBeenCalledWith(ffNameToTest);
  });

  it('should call feature flag service to set state', async () => {
    const ffNameToTest = "TestFeature";
    const setFFMock = jest.spyOn(featureflagService, 'setFeatureFlag')
      .mockImplementation((id, status) => Promise.resolve({ name: id, state: false}));

    await controller.setFeatureFlag({ name: ffNameToTest, state: true });

    expect(setFFMock).toHaveBeenCalled();
    expect(setFFMock).toHaveBeenCalledWith(ffNameToTest, true);
  });
});
