import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { Config } from './config';
import { ConfigLoader } from './config-loaders';
import { CORE_CONFIG_LOADER } from './tokens';

class TestConfigLoader implements ConfigLoader {
  load() {
    return Promise.resolve({});
  }
}

// Unit test Config
describe('Config', () => {
  let spectator: SpectatorService<Config>;
  const createService = createServiceFactory({
    service: Config,
    providers: [{ provide: CORE_CONFIG_LOADER, useClass: TestConfigLoader }],
  });
  let configLoader: ConfigLoader;

  beforeEach(() => {
    spectator = createService();
    configLoader = spectator.inject(CORE_CONFIG_LOADER);
  });

  it('should create', () => {
    expect(spectator.service).toBeTruthy();
  });

  describe('init()', () => {
    it('should load config and assigns it to _config', async () => {
      const config = { unit: 'test' };
      spyOn(configLoader, 'load').and.returnValue(Promise.resolve(config));
      await spectator.service.init();
      expect(spectator.service['_config']).toEqual(config);
    });
  });

  describe('get()', () => {
    it('should return config value', () => {
      spectator.service['_config'] = { test: 'test' };
      expect(spectator.service.get('test')).toEqual('test');
    });
  });
});
