2023-05-18 10:38:59 +02:00
|
|
|
import { PayloadType } from 'unleash-client';
|
2022-12-20 16:10:06 +01:00
|
|
|
import { defaultExperimentalOptions, IFlagKey } from '../types/experimental';
|
2023-05-18 10:38:59 +02:00
|
|
|
import FlagResolver, { getVariantValue } from './flag-resolver';
|
2022-12-20 16:10:06 +01:00
|
|
|
import { IExperimentalOptions } from '../types/experimental';
|
2023-05-18 18:04:55 +02:00
|
|
|
import { getDefaultVariant } from 'unleash-client/lib/variant';
|
2022-08-26 08:22:42 +02:00
|
|
|
|
|
|
|
test('should produce empty exposed flags', () => {
|
|
|
|
const resolver = new FlagResolver(defaultExperimentalOptions);
|
|
|
|
|
|
|
|
const result = resolver.getAll();
|
|
|
|
|
2023-04-25 10:13:06 +02:00
|
|
|
expect(result.anonymiseEventLog).toBe(false);
|
2022-08-26 08:22:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should produce UI flags with extra dynamic flags', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultExperimentalOptions,
|
|
|
|
flags: { extraFlag: false },
|
|
|
|
};
|
2022-12-20 16:10:06 +01:00
|
|
|
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
const result = resolver.getAll() as typeof config.flags;
|
2022-08-26 08:22:42 +02:00
|
|
|
|
|
|
|
expect(result.extraFlag).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use external resolver for dynamic flags', () => {
|
|
|
|
const externalResolver = {
|
|
|
|
isEnabled: (name: string) => {
|
|
|
|
if (name === 'extraFlag') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2023-05-18 18:04:55 +02:00
|
|
|
getVariant: () => getDefaultVariant(),
|
2022-08-26 08:22:42 +02:00
|
|
|
};
|
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
const config = {
|
|
|
|
flags: { extraFlag: false },
|
2022-08-26 08:22:42 +02:00
|
|
|
externalResolver,
|
2022-12-20 16:10:06 +01:00
|
|
|
};
|
2022-08-26 08:22:42 +02:00
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
|
|
|
|
const result = resolver.getAll() as typeof config.flags;
|
2022-08-26 08:22:42 +02:00
|
|
|
|
|
|
|
expect(result.extraFlag).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not use external resolver for enabled experiments', () => {
|
|
|
|
const externalResolver = {
|
|
|
|
isEnabled: () => {
|
|
|
|
return false;
|
|
|
|
},
|
2023-05-18 18:04:55 +02:00
|
|
|
getVariant: () => getDefaultVariant(),
|
2022-08-26 08:22:42 +02:00
|
|
|
};
|
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
const config = {
|
|
|
|
flags: { should_be_enabled: true, extraFlag: false },
|
2022-08-26 08:22:42 +02:00
|
|
|
externalResolver,
|
2022-12-20 16:10:06 +01:00
|
|
|
};
|
2022-08-26 08:22:42 +02:00
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
|
|
|
|
const result = resolver.getAll() as typeof config.flags;
|
2022-08-26 08:22:42 +02:00
|
|
|
|
|
|
|
expect(result.should_be_enabled).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should load experimental flags', () => {
|
|
|
|
const externalResolver = {
|
|
|
|
isEnabled: () => {
|
|
|
|
return false;
|
|
|
|
},
|
2023-05-18 18:04:55 +02:00
|
|
|
getVariant: () => getDefaultVariant(),
|
2022-08-26 08:22:42 +02:00
|
|
|
};
|
2022-12-20 16:10:06 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
flags: { extraFlag: false, someFlag: true },
|
2022-08-26 08:22:42 +02:00
|
|
|
externalResolver,
|
2022-12-20 16:10:06 +01:00
|
|
|
};
|
2022-08-26 08:22:42 +02:00
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
|
|
|
|
expect(resolver.isEnabled('someFlag' as IFlagKey)).toBe(true);
|
|
|
|
expect(resolver.isEnabled('extraFlag' as IFlagKey)).toBe(false);
|
2022-08-26 08:22:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should load experimental flags from external provider', () => {
|
|
|
|
const externalResolver = {
|
|
|
|
isEnabled: (name: string) => {
|
|
|
|
if (name === 'extraFlag') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2023-05-18 18:04:55 +02:00
|
|
|
getVariant: () => getDefaultVariant(),
|
2022-08-26 08:22:42 +02:00
|
|
|
};
|
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
const config = {
|
|
|
|
flags: { extraFlag: false, someFlag: true },
|
2022-08-26 08:22:42 +02:00
|
|
|
externalResolver,
|
2022-12-20 16:10:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
2022-08-26 08:22:42 +02:00
|
|
|
|
2022-12-20 16:10:06 +01:00
|
|
|
expect(resolver.isEnabled('someFlag' as IFlagKey)).toBe(true);
|
|
|
|
expect(resolver.isEnabled('extraFlag' as IFlagKey)).toBe(true);
|
2022-08-26 08:22:42 +02:00
|
|
|
});
|
2023-05-18 10:38:59 +02:00
|
|
|
|
|
|
|
test('should support variant flags', () => {
|
|
|
|
const variant = {
|
|
|
|
enabled: true,
|
|
|
|
name: 'variant',
|
|
|
|
payload: {
|
|
|
|
type: PayloadType.STRING,
|
|
|
|
value: 'variant-A',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const externalResolver = {
|
|
|
|
isEnabled: () => true,
|
|
|
|
getVariant: (name: string) => {
|
|
|
|
if (name === 'extraFlag') {
|
|
|
|
return variant;
|
|
|
|
}
|
2023-05-18 18:04:55 +02:00
|
|
|
return getDefaultVariant();
|
2023-05-18 10:38:59 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
flags: { extraFlag: undefined, someFlag: true, otherflag: false },
|
|
|
|
externalResolver,
|
|
|
|
};
|
|
|
|
|
|
|
|
const resolver = new FlagResolver(config as IExperimentalOptions);
|
|
|
|
|
2023-05-18 18:04:55 +02:00
|
|
|
expect(resolver.getVariant('someFlag' as IFlagKey)).toStrictEqual(
|
|
|
|
getDefaultVariant(),
|
|
|
|
);
|
|
|
|
expect(resolver.getVariant('otherFlag' as IFlagKey)).toStrictEqual(
|
|
|
|
getDefaultVariant(),
|
|
|
|
);
|
2023-05-18 10:38:59 +02:00
|
|
|
expect(resolver.getVariant('extraFlag' as IFlagKey)).toStrictEqual(variant);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should expose an helper to get variant value', () => {
|
2023-05-18 18:04:55 +02:00
|
|
|
expect(
|
|
|
|
getVariantValue({
|
|
|
|
enabled: true,
|
|
|
|
name: 'variant-A',
|
|
|
|
}),
|
|
|
|
).toBe('variant-A');
|
|
|
|
|
2023-05-18 10:38:59 +02:00
|
|
|
expect(
|
|
|
|
getVariantValue({
|
|
|
|
enabled: true,
|
|
|
|
name: 'variant',
|
|
|
|
payload: {
|
|
|
|
type: PayloadType.STRING,
|
2023-05-18 18:04:55 +02:00
|
|
|
value: 'variant-B',
|
2023-05-18 10:38:59 +02:00
|
|
|
},
|
|
|
|
}),
|
2023-05-18 18:04:55 +02:00
|
|
|
).toBe('variant-B');
|
2023-05-18 10:38:59 +02:00
|
|
|
|
|
|
|
expect(
|
|
|
|
getVariantValue({
|
|
|
|
enabled: true,
|
|
|
|
name: 'variant',
|
|
|
|
payload: {
|
|
|
|
type: PayloadType.JSON,
|
|
|
|
value: `{"foo": "bar"}`,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
).toStrictEqual({
|
|
|
|
foo: 'bar',
|
|
|
|
});
|
|
|
|
});
|