debian-mirror-gitlab/spec/lib/gitlab/experiment/rollout/feature_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.5 KiB
Ruby
Raw Normal View History

2022-04-04 11:22:00 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Experiment::Rollout::Feature, :experiment do
subject { described_class.new.for(subject_experiment) }
let(:subject_experiment) { experiment('namespaced/stub') }
describe "#enabled?" do
before do
2022-05-07 20:08:51 +05:30
stub_feature_flags(gitlab_experiment: true)
allow(subject).to receive(:feature_flag_defined?).and_return(true)
allow(Gitlab).to receive(:com?).and_return(true)
allow(subject).to receive(:feature_flag_instance).and_return(double(state: :on))
2022-04-04 11:22:00 +05:30
end
it "is enabled when all criteria are met" do
expect(subject).to be_enabled
end
it "isn't enabled if the feature definition doesn't exist" do
2022-05-07 20:08:51 +05:30
expect(subject).to receive(:feature_flag_defined?).and_return(false)
2022-04-04 11:22:00 +05:30
expect(subject).not_to be_enabled
end
it "isn't enabled if we're not in dev or dotcom environments" do
2022-05-07 20:08:51 +05:30
expect(Gitlab).to receive(:com?).and_return(false)
2022-04-04 11:22:00 +05:30
expect(subject).not_to be_enabled
end
it "isn't enabled if the feature flag state is :off" do
2022-05-07 20:08:51 +05:30
expect(subject).to receive(:feature_flag_instance).and_return(double(state: :off))
expect(subject).not_to be_enabled
end
it "isn't enabled if the gitlab_experiment feature flag is false" do
stub_feature_flags(gitlab_experiment: false)
2022-04-04 11:22:00 +05:30
expect(subject).not_to be_enabled
end
end
describe "#execute_assignment" do
before do
allow(Feature).to receive(:enabled?).with('namespaced_stub', any_args).and_return(true)
end
it "uses the default value as specified in the yaml" do
expect(Feature).to receive(:enabled?).with(
'namespaced_stub',
subject,
2022-07-16 23:28:13 +05:30
type: :experiment
2022-04-04 11:22:00 +05:30
).and_return(false)
expect(subject.execute_assignment).to be_nil
end
it "returns an assigned name" do
allow(subject).to receive(:behavior_names).and_return([:variant1, :variant2])
expect(subject.execute_assignment).to eq(:variant2)
end
end
describe "#flipper_id" do
it "returns the expected flipper id if the experiment doesn't provide one" do
subject.instance_variable_set(:@experiment, double(id: '__id__'))
expect(subject.flipper_id).to eq('Experiment;__id__')
end
it "lets the experiment provide a flipper id so it can override the default" do
allow(subject_experiment).to receive(:flipper_id).and_return('_my_overridden_id_')
expect(subject.flipper_id).to eq('_my_overridden_id_')
end
end
end