mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-10 17:53:36 +02:00
Chore: extract variant creation arbitrary.
This commit is contained in:
parent
4332072db4
commit
cb56846f7f
@ -2,7 +2,7 @@ import fc, { Arbitrary } from 'fast-check';
|
|||||||
|
|
||||||
import { ALL_OPERATORS } from '../lib/util/constants';
|
import { ALL_OPERATORS } from '../lib/util/constants';
|
||||||
import { ClientFeatureSchema } from '../lib/openapi/spec/client-feature-schema';
|
import { ClientFeatureSchema } from '../lib/openapi/spec/client-feature-schema';
|
||||||
import { WeightType } from '../lib/types/model';
|
import { IVariant, WeightType } from '../lib/types/model';
|
||||||
import { FeatureStrategySchema } from '../lib/openapi/spec/feature-strategy-schema';
|
import { FeatureStrategySchema } from '../lib/openapi/spec/feature-strategy-schema';
|
||||||
import { ConstraintSchema } from 'lib/openapi/spec/constraint-schema';
|
import { ConstraintSchema } from 'lib/openapi/spec/constraint-schema';
|
||||||
|
|
||||||
@ -92,6 +92,43 @@ export const strategies = (): Arbitrary<FeatureStrategySchema[]> =>
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const variant = (): Arbitrary<IVariant> =>
|
||||||
|
fc.record({
|
||||||
|
name: urlFriendlyString(),
|
||||||
|
weight: fc.nat({ max: 1000 }),
|
||||||
|
weightType: fc.constant(WeightType.VARIABLE),
|
||||||
|
stickiness: fc.constant('default'),
|
||||||
|
payload: fc.option(
|
||||||
|
fc.oneof(
|
||||||
|
fc.record({
|
||||||
|
type: fc.constant('json'),
|
||||||
|
value: fc.json(),
|
||||||
|
}),
|
||||||
|
fc.record({
|
||||||
|
type: fc.constant('csv'),
|
||||||
|
value: fc.array(fc.lorem()).map((words) => words.join(',')),
|
||||||
|
}),
|
||||||
|
fc.record({
|
||||||
|
type: fc.constant('string'),
|
||||||
|
value: fc.string(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const variants = (): Arbitrary<IVariant[]> =>
|
||||||
|
fc
|
||||||
|
.uniqueArray(variant(), {
|
||||||
|
maxLength: 1000,
|
||||||
|
selector: (variantInstance) => variantInstance.name,
|
||||||
|
})
|
||||||
|
.map((allVariants) =>
|
||||||
|
allVariants.map((variantInstance) => ({
|
||||||
|
...variantInstance,
|
||||||
|
weight: Math.round(1000 / allVariants.length),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
export const clientFeature = (name?: string): Arbitrary<ClientFeatureSchema> =>
|
export const clientFeature = (name?: string): Arbitrary<ClientFeatureSchema> =>
|
||||||
fc.record(
|
fc.record(
|
||||||
{
|
{
|
||||||
@ -111,32 +148,7 @@ export const clientFeature = (name?: string): Arbitrary<ClientFeatureSchema> =>
|
|||||||
stale: fc.boolean(),
|
stale: fc.boolean(),
|
||||||
impressionData: fc.option(fc.boolean()),
|
impressionData: fc.option(fc.boolean()),
|
||||||
strategies: strategies(),
|
strategies: strategies(),
|
||||||
variants: fc.array(
|
variants: variants(),
|
||||||
fc.record({
|
|
||||||
name: urlFriendlyString(),
|
|
||||||
weight: fc.nat({ max: 1000 }),
|
|
||||||
weightType: fc.constant(WeightType.VARIABLE),
|
|
||||||
stickiness: fc.constant('default'),
|
|
||||||
payload: fc.option(
|
|
||||||
fc.oneof(
|
|
||||||
fc.record({
|
|
||||||
type: fc.constant('json'),
|
|
||||||
value: fc.json(),
|
|
||||||
}),
|
|
||||||
fc.record({
|
|
||||||
type: fc.constant('csv'),
|
|
||||||
value: fc
|
|
||||||
.array(fc.lorem())
|
|
||||||
.map((words) => words.join(',')),
|
|
||||||
}),
|
|
||||||
fc.record({
|
|
||||||
type: fc.constant('string'),
|
|
||||||
value: fc.string(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
{ requiredKeys: ['name', 'enabled', 'project', 'strategies'] },
|
{ requiredKeys: ['name', 'enabled', 'project', 'strategies'] },
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user