mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-15 17:50:48 +02:00
task: Sort variants by name (#1132)
This commit is contained in:
parent
620aff23b4
commit
5829ec7b3d
@ -4,7 +4,7 @@ import metricsHelper from '../util/metrics-helper';
|
|||||||
import { DB_TIME } from '../metric-events';
|
import { DB_TIME } from '../metric-events';
|
||||||
import NotFoundError from '../error/notfound-error';
|
import NotFoundError from '../error/notfound-error';
|
||||||
import { Logger, LogProvider } from '../logger';
|
import { Logger, LogProvider } from '../logger';
|
||||||
import { FeatureToggleDTO, FeatureToggle, IVariant } from '../types/model';
|
import { FeatureToggle, FeatureToggleDTO, IVariant } from '../types/model';
|
||||||
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
|
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
|
||||||
|
|
||||||
const FEATURE_COLUMNS = [
|
const FEATURE_COLUMNS = [
|
||||||
@ -155,13 +155,15 @@ export default class FeatureToggleStore implements IFeatureToggleStore {
|
|||||||
if (!row) {
|
if (!row) {
|
||||||
throw new NotFoundError('No feature toggle found');
|
throw new NotFoundError('No feature toggle found');
|
||||||
}
|
}
|
||||||
|
const sortedVariants = (row.variants as unknown as IVariant[]) || [];
|
||||||
|
sortedVariants.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
return {
|
return {
|
||||||
name: row.name,
|
name: row.name,
|
||||||
description: row.description,
|
description: row.description,
|
||||||
type: row.type,
|
type: row.type,
|
||||||
project: row.project,
|
project: row.project,
|
||||||
stale: row.stale,
|
stale: row.stale,
|
||||||
variants: (row.variants as unknown as IVariant[]) || [],
|
variants: sortedVariants,
|
||||||
createdAt: row.created_at,
|
createdAt: row.created_at,
|
||||||
lastSeenAt: row.last_seen_at,
|
lastSeenAt: row.last_seen_at,
|
||||||
};
|
};
|
||||||
@ -171,7 +173,10 @@ export default class FeatureToggleStore implements IFeatureToggleStore {
|
|||||||
if (!row) {
|
if (!row) {
|
||||||
throw new NotFoundError('No feature toggle found');
|
throw new NotFoundError('No feature toggle found');
|
||||||
}
|
}
|
||||||
return (row.variants as unknown as IVariant[]) || [];
|
const sortedVariants = (row.variants as unknown as IVariant[]) || [];
|
||||||
|
sortedVariants.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
|
|
||||||
|
return sortedVariants;
|
||||||
}
|
}
|
||||||
|
|
||||||
dtoToRow(project: string, data: FeatureToggleDTO): FeaturesTable {
|
dtoToRow(project: string, data: FeatureToggleDTO): FeaturesTable {
|
||||||
@ -183,7 +188,11 @@ export default class FeatureToggleStore implements IFeatureToggleStore {
|
|||||||
archived: data.archived || false,
|
archived: data.archived || false,
|
||||||
stale: data.stale,
|
stale: data.stale,
|
||||||
variants: data.variants
|
variants: data.variants
|
||||||
? JSON.stringify(data.variants)
|
? JSON.stringify(
|
||||||
|
data.variants.sort((a, b) =>
|
||||||
|
a.name.localeCompare(b.name),
|
||||||
|
),
|
||||||
|
)
|
||||||
: JSON.stringify([]),
|
: JSON.stringify([]),
|
||||||
created_at: data.createdAt,
|
created_at: data.createdAt,
|
||||||
};
|
};
|
||||||
|
@ -714,3 +714,102 @@ test('PUT endpoint validates uniqueness of variant names', async () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Variants should be sorted by their name when PUT', async () => {
|
||||||
|
const featureName = 'variants-sort-by-name';
|
||||||
|
await db.stores.featureToggleStore.create('default', {
|
||||||
|
name: featureName,
|
||||||
|
});
|
||||||
|
|
||||||
|
await app.request
|
||||||
|
.put(`/api/admin/projects/default/features/${featureName}/variants`)
|
||||||
|
.send([
|
||||||
|
{
|
||||||
|
name: 'zvariant',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'variant-a',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'g-variant',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'variant-g',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.expect(200)
|
||||||
|
.expect((res) => {
|
||||||
|
expect(res.body.variants[0].name).toBe('g-variant');
|
||||||
|
expect(res.body.variants[1].name).toBe('variant-a');
|
||||||
|
expect(res.body.variants[2].name).toBe('variant-g');
|
||||||
|
expect(res.body.variants[3].name).toBe('zvariant');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Variants should be sorted by name when PATCHed as well', async () => {
|
||||||
|
const featureName = 'variants-patch-sort-by-name';
|
||||||
|
await db.stores.featureToggleStore.create('default', {
|
||||||
|
name: featureName,
|
||||||
|
});
|
||||||
|
|
||||||
|
const variants: IVariant[] = [];
|
||||||
|
const observer = jsonpatch.observe(variants);
|
||||||
|
variants.push({
|
||||||
|
name: 'g-variant',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
});
|
||||||
|
variants.push({
|
||||||
|
name: 'a-variant',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
});
|
||||||
|
const patch = jsonpatch.generate(observer);
|
||||||
|
await app.request
|
||||||
|
.patch(`/api/admin/projects/default/features/${featureName}/variants`)
|
||||||
|
.send(patch)
|
||||||
|
.expect(200)
|
||||||
|
.expect((res) => {
|
||||||
|
expect(res.body.variants[0].name).toBe('a-variant');
|
||||||
|
expect(res.body.variants[1].name).toBe('g-variant');
|
||||||
|
});
|
||||||
|
variants.push({
|
||||||
|
name: '00-variant',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
});
|
||||||
|
variants.push({
|
||||||
|
name: 'z-variant',
|
||||||
|
weightType: 'variable',
|
||||||
|
weight: 500,
|
||||||
|
stickiness: 'default',
|
||||||
|
});
|
||||||
|
const secondPatch = jsonpatch.generate(observer);
|
||||||
|
expect(secondPatch).toHaveLength(2);
|
||||||
|
await app.request
|
||||||
|
.patch(`/api/admin/projects/default/features/${featureName}/variants`)
|
||||||
|
.send(secondPatch)
|
||||||
|
.expect(200)
|
||||||
|
.expect((res) => {
|
||||||
|
expect(res.body.variants).toHaveLength(4);
|
||||||
|
expect(res.body.variants[0].name).toBe('00-variant');
|
||||||
|
expect(res.body.variants[1].name).toBe('a-variant');
|
||||||
|
expect(res.body.variants[2].name).toBe('g-variant');
|
||||||
|
expect(res.body.variants[3].name).toBe('z-variant');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user