summaryrefslogtreecommitdiff
path: root/spec/support/helpers/stub_experiments.rb
blob: ff3b02dc3f6e5d40b05123f3bb6c92fb0ce980dd (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
# frozen_string_literal: true

module StubExperiments
  # Stub Experiment with `key: true/false`
  #
  # @param [Hash] experiment where key is feature name and value is boolean whether enabled or not.
  #
  # Examples
  # - `stub_experiment(signup_flow: false)` ... Disable `signup_flow` experiment globally.
  def stub_experiment(experiments)
    allow(Gitlab::Experimentation).to receive(:enabled?).and_call_original

    experiments.each do |experiment_key, enabled|
      allow(Gitlab::Experimentation).to receive(:enabled?).with(experiment_key) { enabled }
    end
  end

  # Stub Experiment for user with `key: true/false`
  #
  # @param [Hash] experiment where key is feature name and value is boolean whether enabled or not.
  #
  # Examples
  # - `stub_experiment_for_user(signup_flow: false)` ... Disable `signup_flow` experiment for user.
  def stub_experiment_for_user(experiments)
    allow(Gitlab::Experimentation).to receive(:enabled_for_user?).and_call_original

    experiments.each do |experiment_key, enabled|
      allow(Gitlab::Experimentation).to receive(:enabled_for_user?).with(experiment_key, anything) { enabled }
    end
  end
end