summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/experimentation_helper.js
blob: d5044be88d7bea3d5abc7109e4856fc7eccc8712 (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
import { merge } from 'lodash';

// This helper is for specs that use `gitlab/experimentation` module
export function withGonExperiment(experimentKey, value = true) {
  let origGon;

  beforeEach(() => {
    origGon = window.gon;
    window.gon = merge({}, window.gon || {}, { experiments: { [experimentKey]: value } });
  });

  afterEach(() => {
    window.gon = origGon;
  });
}

// The following helper is for specs that use `gitlab-experiment` utilities,
// which have a different schema that gets pushed to the frontend compared to
// the `Experimentation` Module.
//
// Usage: stubExperiments({ experiment_feature_flag_name: 'variant_name', ... })
export function stubExperiments(experiments = {}) {
  // Deprecated
  window.gon = window.gon || {};
  window.gon.experiment = window.gon.experiment || {};
  // Preferred
  window.gl = window.gl || {};
  window.gl.experiments = window.gl.experiments || {};

  Object.entries(experiments).forEach(([name, variant]) => {
    const experimentData = { experiment: name, variant };

    // Deprecated
    window.gon.experiment[name] = experimentData;
    // Preferred
    window.gl.experiments[name] = experimentData;
  });
}