1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Fix/variants ordering (#1187)

* fix: sort variants on output

We found that for existing variants, the sorting by name wasn't applied, so the index in the patches were wrong. This PR adds the sort also when we're getting variants as part of the getFeature call, not just when we're getting the variants. This should make the UX consistent, and prevent our patches from hitting the wrong index in the variants array.

Co-authored-by: Youssef Khedher <khedher.youssef@hotmail.fr>
This commit is contained in:
Christopher Kolstad 2021-12-15 14:20:32 +01:00 committed by GitHub
parent e061ccccb4
commit 791384ddb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View File

@ -287,6 +287,7 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore {
return e;
});
featureToggle.variants = featureToggle.variants || [];
featureToggle.variants.sort((a, b) => a.name.localeCompare(b.name));
featureToggle.archived = archived;
return featureToggle;
}

View File

@ -13,6 +13,7 @@ import {
import ApiUser from '../../../../../lib/types/api-user';
import { ApiTokenType } from '../../../../../lib/types/models/api-token';
import IncompatibleProjectError from '../../../../../lib/error/incompatible-project-error';
import { IVariant, WeightType } from '../../../../../lib/types/model';
let app: IUnleashTest;
let db: ITestDb;
@ -1827,3 +1828,49 @@ test('Should allow changing project to target project with the same enabled envi
),
).resolves;
});
test(`a feature's variants should be sorted by name in increasing order`, async () => {
const featureName = 'variants.are.sorted';
const project = 'default';
await app.request
.post(`/api/admin/projects/${project}/features`)
.send({
name: featureName,
})
.expect(201);
const newVariants: IVariant[] = [
{
name: 'z',
stickiness: 'default',
weight: 250,
weightType: WeightType.FIX,
},
{
name: 'f',
stickiness: 'default',
weight: 375,
weightType: WeightType.VARIABLE,
},
{
name: 'a',
stickiness: 'default',
weight: 450,
weightType: WeightType.VARIABLE,
},
];
await app.request
.put(`/api/admin/projects/${project}/features/${featureName}/variants`)
.send(newVariants)
.expect(200);
await app.request
.get(`/api/admin/projects/${project}/features/${featureName}`)
.expect(200)
.expect((res) => {
expect(res.body.variants[0].name).toBe('a');
expect(res.body.variants[1].name).toBe('f');
expect(res.body.variants[2].name).toBe('z');
});
});