Commit 87c50552 authored by Vitaly Slobodin's avatar Vitaly Slobodin

Merge branch 'jejacks0n/add-experiemnt-contexts-util' into 'master'

Fetch snowplow contexts for the named experiments

See merge request gitlab-org/gitlab!60037
parents 394e1384 6376115e
// This file only applies to use of experiments through https://gitlab.com/gitlab-org/gitlab-experiment
import { get } from 'lodash';
import { DEFAULT_VARIANT, CANDIDATE_VARIANT } from './constants';
import { DEFAULT_VARIANT, CANDIDATE_VARIANT, TRACKING_CONTEXT_SCHEMA } from './constants';
export function getExperimentData(experimentName) {
return get(window, ['gon', 'experiment', experimentName]);
}
export function getExperimentContexts(...experimentNames) {
return experimentNames
.map((name) => {
const data = getExperimentData(name);
return data && { schema: TRACKING_CONTEXT_SCHEMA, data };
})
.filter((context) => context);
}
export function isExperimentVariant(experimentName, variantName) {
return getExperimentData(experimentName)?.variant === variantName;
}
......
import { assignGitlabExperiment } from 'helpers/experimentation_helper';
import { DEFAULT_VARIANT, CANDIDATE_VARIANT } from '~/experimentation/constants';
import {
DEFAULT_VARIANT,
CANDIDATE_VARIANT,
TRACKING_CONTEXT_SCHEMA,
} from '~/experimentation/constants';
import * as experimentUtils from '~/experimentation/utils';
describe('experiment Utilities', () => {
......@@ -19,6 +23,20 @@ describe('experiment Utilities', () => {
});
});
describe('getExperimentContexts', () => {
describe.each`
gon | input | output
${[TEST_KEY, '_data_']} | ${[TEST_KEY]} | ${[{ schema: TRACKING_CONTEXT_SCHEMA, data: { variant: '_data_' } }]}
${[]} | ${[TEST_KEY]} | ${[]}
`('with input=$input and gon=$gon', ({ gon, input, output }) => {
assignGitlabExperiment(...gon);
it(`returns ${output}`, () => {
expect(experimentUtils.getExperimentContexts(...input)).toEqual(output);
});
});
});
describe('isExperimentVariant', () => {
describe.each`
gon | input | output
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment