summaryrefslogtreecommitdiff
path: root/spec/frontend/experimentation/utils_spec.js
blob: de060f5eb8c1d9e79e63832dbcab280331680e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { assignGitlabExperiment } from 'helpers/experimentation_helper';
import {
  DEFAULT_VARIANT,
  CANDIDATE_VARIANT,
  TRACKING_CONTEXT_SCHEMA,
} from '~/experimentation/constants';
import * as experimentUtils from '~/experimentation/utils';

describe('experiment Utilities', () => {
  const TEST_KEY = 'abc';

  describe('getExperimentData', () => {
    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);

      it(`returns ${output}`, () => {
        expect(experimentUtils.getExperimentData(...input)).toEqual(output);
      });
    });
  });

  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 });
    });
  });

  describe('isExperimentVariant', () => {
    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', () => {
    it.each`
      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}
    `('with input=$input and gon=$gon, returns $output', ({ gon, input, output }) => {
      window.gon = gon;

      expect(experimentUtils.getExperimentVariant(...input)).toEqual(output);
    });
  });
});