From 791384ddb1ca1e69999524ea2d792ca607f406db Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Wed, 15 Dec 2021 14:20:32 +0100 Subject: [PATCH] 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 --- src/lib/db/feature-strategy-store.ts | 1 + .../api/admin/project/features.e2e.test.ts | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/lib/db/feature-strategy-store.ts b/src/lib/db/feature-strategy-store.ts index fd2d7c9ee7..d2bd270df5 100644 --- a/src/lib/db/feature-strategy-store.ts +++ b/src/lib/db/feature-strategy-store.ts @@ -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; } diff --git a/src/test/e2e/api/admin/project/features.e2e.test.ts b/src/test/e2e/api/admin/project/features.e2e.test.ts index df87a15ccd..edec135748 100644 --- a/src/test/e2e/api/admin/project/features.e2e.test.ts +++ b/src/test/e2e/api/admin/project/features.e2e.test.ts @@ -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'); + }); +});