Skip to content

Commit

Permalink
fix: Playground variants don't show correctly (#1829)
Browse files Browse the repository at this point in the history
* Refactor: use the `ALL` constant for tests

* Fix: context is not passed to `getVariant` call

This commit fixes a bug wherein a toggle with variants would show as
enabled but without variants. This was because the `getVariant` call
didn't receive the provided context as an argument.
  • Loading branch information
thomasheartman committed Jul 18, 2022
1 parent 19b95b1 commit 05c390e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/services/playground-service.ts
Expand Up @@ -58,7 +58,7 @@ export class PlaygroundService {
projectId: await this.featureToggleService.getProjectId(
feature.name,
),
variant: client.getVariant(feature.name),
variant: client.getVariant(feature.name, clientContext),
name: feature.name,
};
}),
Expand Down
65 changes: 60 additions & 5 deletions src/test/e2e/api/admin/playground.e2e.test.ts
Expand Up @@ -6,6 +6,7 @@ import { IUnleashTest, setupAppWithAuth } from '../../helpers/test-helper';
import { FeatureToggle, WeightType } from '../../../../lib/types/model';
import getLogger from '../../../fixtures/no-logger';
import {
ALL,
ApiTokenType,
IApiToken,
} from '../../../../lib/types/models/api-token';
Expand All @@ -25,8 +26,8 @@ beforeAll(async () => {
token = await apiTokenService.createApiTokenWithProjects({
type: ApiTokenType.ADMIN,
username: 'tester',
environment: '*',
projects: ['*'],
environment: ALL,
projects: [ALL],
});
});

Expand Down Expand Up @@ -172,7 +173,7 @@ describe('Playground API E2E', () => {
// get a subset of projects that exist among the features
const [projects] = fc.sample(
fc.oneof(
fc.constant('*' as '*'),
fc.constant(ALL as '*'),
fc.uniqueArray(
fc.constantFrom(
...features.map(
Expand All @@ -194,7 +195,7 @@ describe('Playground API E2E', () => {
);

switch (projects) {
case '*':
case ALL:
// no features have been filtered out
return body.features.length === features.length;
case []:
Expand Down Expand Up @@ -228,7 +229,7 @@ describe('Playground API E2E', () => {
app,
token.secret,
{
projects: '*',
projects: ALL,
environment: 'default',
context: {
appName: 'playground-test',
Expand Down Expand Up @@ -576,5 +577,59 @@ describe('Playground API E2E', () => {
testParams,
);
});

test('context is applied to variant checks', async () => {
const environment = 'development';
const featureName = 'feature-name';
const customContextFieldName = 'customField';
const customContextValue = 'customValue';

const features = [
{
project: 'any-project',
strategies: [
{
name: 'default',
constraints: [
{
contextName: customContextFieldName,
operator: 'IN' as 'IN',
values: [customContextValue],
},
],
},
],
stale: false,
enabled: true,
name: featureName,
type: 'experiment',
variants: [
{
name: 'a',
weight: 1000,
weightType: 'variable',
stickiness: 'default',
overrides: [],
},
],
},
];

await seedDatabase(db, features, environment);

const request = {
projects: ALL as '*',
environment,
context: {
appName: 'playground',
[customContextFieldName]: customContextValue,
},
};

const body = await playgroundRequest(app, token.secret, request);

// when enabled, this toggle should have one of the variants
expect(body.features[0].variant.name).toBe('a');
});
});
});

0 comments on commit 05c390e

Please sign in to comment.