debian-mirror-gitlab/spec/frontend/experimentation/utils_spec.js

146 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-04-29 21:17:54 +05:30
import { assignGitlabExperiment } from 'helpers/experimentation_helper';
2021-06-08 01:23:25 +05:30
import {
DEFAULT_VARIANT,
CANDIDATE_VARIANT,
TRACKING_CONTEXT_SCHEMA,
} from '~/experimentation/constants';
2021-04-17 20:07:23 +05:30
import * as experimentUtils from '~/experimentation/utils';
describe('experiment Utilities', () => {
2021-04-29 21:17:54 +05:30
const TEST_KEY = 'abc';
2021-04-17 20:07:23 +05:30
describe('getExperimentData', () => {
2021-04-29 21:17:54 +05:30
describe.each`
gon | input | output
${[TEST_KEY, '_data_']} | ${[TEST_KEY]} | ${{ variant: '_data_' }}
${[]} | ${[TEST_KEY]} | ${undefined}
`('with input=$input and gon=$gon', ({ gon, input, output }) => {
assignGitlabExperiment(...gon);
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
it(`returns ${output}`, () => {
expect(experimentUtils.getExperimentData(...input)).toEqual(output);
});
2021-04-17 20:07:23 +05:30
});
});
2021-11-11 11:23:49 +05:30
describe('getAllExperimentContexts', () => {
const schema = TRACKING_CONTEXT_SCHEMA;
let origGon;
beforeEach(() => {
origGon = window.gon;
});
afterEach(() => {
window.gon = origGon;
});
it('collects all of the experiment contexts into a single array', () => {
const experiments = [
{ experiment: 'abc', variant: 'candidate' },
{ experiment: 'def', variant: 'control' },
{ experiment: 'ghi', variant: 'blue' },
];
window.gon = {
experiment: experiments.reduce((collector, { experiment, variant }) => {
return { ...collector, [experiment]: { experiment, variant } };
}, {}),
};
expect(experimentUtils.getAllExperimentContexts()).toEqual(
experiments.map((data) => ({ schema, data })),
);
});
it('returns an empty array if there are no experiments', () => {
window.gon.experiment = {};
expect(experimentUtils.getAllExperimentContexts()).toEqual([]);
});
it('includes all additional experiment data', () => {
const experiment = 'experimentWithCustomData';
const data = { experiment, variant: 'control', color: 'blue', style: 'rounded' };
window.gon.experiment[experiment] = data;
expect(experimentUtils.getAllExperimentContexts()).toContainEqual({ schema, data });
});
});
2021-04-17 20:07:23 +05:30
describe('isExperimentVariant', () => {
2021-04-29 21:17:54 +05:30
describe.each`
gon | input | output
${[TEST_KEY, DEFAULT_VARIANT]} | ${[TEST_KEY, DEFAULT_VARIANT]} | ${true}
${[TEST_KEY, '_variant_name']} | ${[TEST_KEY, '_variant_name']} | ${true}
${[TEST_KEY, '_variant_name']} | ${[TEST_KEY, '_bogus_name']} | ${false}
${[TEST_KEY, '_variant_name']} | ${['boguskey', '_variant_name']} | ${false}
${[]} | ${[TEST_KEY, '_variant_name']} | ${false}
`('with input=$input and gon=$gon', ({ gon, input, output }) => {
assignGitlabExperiment(...gon);
it(`returns ${output}`, () => {
expect(experimentUtils.isExperimentVariant(...input)).toEqual(output);
});
});
});
describe('experiment', () => {
const controlSpy = jest.fn();
const candidateSpy = jest.fn();
const getUpStandUpSpy = jest.fn();
const variants = {
use: controlSpy,
try: candidateSpy,
get_up_stand_up: getUpStandUpSpy,
};
describe('when there is no experiment data', () => {
it('calls control variant', () => {
experimentUtils.experiment('marley', variants);
expect(controlSpy).toHaveBeenCalled();
});
});
describe('when experiment variant is "control"', () => {
assignGitlabExperiment('marley', DEFAULT_VARIANT);
it('calls the control variant', () => {
experimentUtils.experiment('marley', variants);
expect(controlSpy).toHaveBeenCalled();
});
});
describe('when experiment variant is "candidate"', () => {
assignGitlabExperiment('marley', CANDIDATE_VARIANT);
it('calls the candidate variant', () => {
experimentUtils.experiment('marley', variants);
expect(candidateSpy).toHaveBeenCalled();
});
});
describe('when experiment variant is "get_up_stand_up"', () => {
assignGitlabExperiment('marley', 'get_up_stand_up');
it('calls the get-up-stand-up variant', () => {
experimentUtils.experiment('marley', variants);
expect(getUpStandUpSpy).toHaveBeenCalled();
});
});
});
describe('getExperimentVariant', () => {
2021-04-17 20:07:23 +05:30
it.each`
2021-04-29 21:17:54 +05:30
gon | input | output
${{ experiment: { [TEST_KEY]: { variant: DEFAULT_VARIANT } } }} | ${[TEST_KEY]} | ${DEFAULT_VARIANT}
${{ experiment: { [TEST_KEY]: { variant: CANDIDATE_VARIANT } } }} | ${[TEST_KEY]} | ${CANDIDATE_VARIANT}
${{}} | ${[TEST_KEY]} | ${DEFAULT_VARIANT}
2021-04-17 20:07:23 +05:30
`('with input=$input and gon=$gon, returns $output', ({ gon, input, output }) => {
window.gon = gon;
2021-04-29 21:17:54 +05:30
expect(experimentUtils.getExperimentVariant(...input)).toEqual(output);
2021-04-17 20:07:23 +05:30
});
});
});