2021-08-26 13:59:11 +02:00
|
|
|
import EventService from '../../../lib/services/event-service';
|
|
|
|
import { FEATURE_UPDATED } from '../../../lib/types/events';
|
2021-07-07 10:46:50 +02:00
|
|
|
import FeatureToggleServiceV2 from '../../../lib/services/feature-toggle-service-v2';
|
|
|
|
import { IStrategyConfig } from '../../../lib/types/model';
|
|
|
|
import { createTestConfig } from '../../config/test-config';
|
|
|
|
import dbInit from '../helpers/database-init';
|
2021-08-26 13:59:11 +02:00
|
|
|
import { GLOBAL_ENV } from '../../../lib/types/environment';
|
2021-07-07 10:46:50 +02:00
|
|
|
|
|
|
|
let stores;
|
|
|
|
let db;
|
|
|
|
let service: FeatureToggleServiceV2;
|
2021-08-26 13:59:11 +02:00
|
|
|
let eventService: EventService;
|
2021-07-07 10:46:50 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const config = createTestConfig();
|
|
|
|
db = await dbInit(
|
|
|
|
'feature_toggle_service_v2_service_serial',
|
|
|
|
config.getLogger,
|
|
|
|
);
|
|
|
|
stores = db.stores;
|
|
|
|
service = new FeatureToggleServiceV2(stores, config);
|
2021-08-26 13:59:11 +02:00
|
|
|
eventService = new EventService(stores, config);
|
2021-07-07 10:46:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await db.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should create feature toggle strategy configuration', async () => {
|
|
|
|
const config: Omit<IStrategyConfig, 'id'> = {
|
|
|
|
name: 'default',
|
|
|
|
constraints: [],
|
|
|
|
parameters: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
await service.createFeatureToggle(
|
|
|
|
'default',
|
|
|
|
{
|
|
|
|
name: 'Demo',
|
|
|
|
},
|
|
|
|
'test',
|
|
|
|
);
|
|
|
|
|
|
|
|
const createdConfig = await service.createStrategy(
|
|
|
|
config,
|
|
|
|
'default',
|
|
|
|
'Demo',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(createdConfig.name).toEqual('default');
|
|
|
|
expect(createdConfig.id).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should be able to update existing strategy configuration', async () => {
|
|
|
|
const config: Omit<IStrategyConfig, 'id'> = {
|
|
|
|
name: 'default',
|
|
|
|
constraints: [],
|
|
|
|
parameters: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
await service.createFeatureToggle(
|
|
|
|
'default',
|
|
|
|
{
|
|
|
|
name: 'update-existing-strategy',
|
|
|
|
},
|
|
|
|
'test',
|
|
|
|
);
|
|
|
|
|
|
|
|
const createdConfig = await service.createStrategy(
|
|
|
|
config,
|
|
|
|
'default',
|
|
|
|
'update-existing-strategy',
|
|
|
|
);
|
|
|
|
expect(createdConfig.name).toEqual('default');
|
|
|
|
const updatedConfig = await service.updateStrategy(createdConfig.id, {
|
|
|
|
parameters: { b2b: true },
|
|
|
|
});
|
|
|
|
expect(createdConfig.id).toEqual(updatedConfig.id);
|
|
|
|
expect(updatedConfig.parameters).toEqual({ b2b: true });
|
|
|
|
});
|
|
|
|
|
2021-08-26 13:59:11 +02:00
|
|
|
test('Should include legacy props in event log when updating strategy configuration', async () => {
|
|
|
|
const userName = 'event-tester';
|
|
|
|
const featureName = 'update-existing-strategy-events';
|
|
|
|
const config: Omit<IStrategyConfig, 'id'> = {
|
|
|
|
name: 'default',
|
|
|
|
constraints: [],
|
|
|
|
parameters: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
await service.createFeatureToggle(
|
|
|
|
'default',
|
|
|
|
{
|
|
|
|
name: featureName,
|
|
|
|
},
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
|
|
|
|
await service.createStrategy(config, 'default', featureName);
|
|
|
|
await service.updateEnabled(featureName, GLOBAL_ENV, true, userName);
|
|
|
|
|
|
|
|
const events = await eventService.getEventsForToggle(featureName);
|
|
|
|
expect(events[0].type).toBe(FEATURE_UPDATED);
|
|
|
|
expect(events[0].data.enabled).toBe(true);
|
|
|
|
expect(events[0].data.strategies).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
test('Should be able to get strategy by id', async () => {
|
|
|
|
const config: Omit<IStrategyConfig, 'id'> = {
|
|
|
|
name: 'default',
|
|
|
|
constraints: [],
|
|
|
|
parameters: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
await service.createFeatureToggle(
|
|
|
|
'default',
|
|
|
|
{
|
|
|
|
name: 'get-strategy-by-id',
|
|
|
|
},
|
|
|
|
'test',
|
|
|
|
);
|
|
|
|
|
|
|
|
const createdConfig = await service.createStrategy(
|
|
|
|
config,
|
|
|
|
'default',
|
|
|
|
'Demo',
|
|
|
|
);
|
|
|
|
const fetchedConfig = await service.getStrategy(createdConfig.id);
|
|
|
|
expect(fetchedConfig).toEqual(createdConfig);
|
|
|
|
});
|