summaryrefslogtreecommitdiff
path: root/spec/support_specs/helpers/stub_feature_flags_spec.rb
blob: 57dd3015f5e60dcc31dc468913087be748f6054b (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe StubFeatureFlags do
  let_it_be(:dummy_feature_flag) { :dummy_feature_flag }

  # We inject dummy feature flag defintion
  # to ensure that we strong validate it's usage
  # as well
  before(:all) do
    definition = Feature::Definition.new(
      nil,
      name: dummy_feature_flag,
      type: 'development',
      # we allow ambigious usage of `default_enabled:`
      default_enabled: [false, true]
    )

    Feature::Definition.definitions[dummy_feature_flag] = definition
  end

  after(:all) do
    Feature::Definition.definitions.delete(dummy_feature_flag)
  end

  describe '#stub_feature_flags' do
    using RSpec::Parameterized::TableSyntax

    let(:feature_name) { dummy_feature_flag }

    context 'when checking global state' do
      where(:feature_actors, :expected_result) do
        false   | false
        true    | true
        :A      | false
        %i[A]   | false
        %i[A B] | false
      end

      with_them do
        before do
          stub_feature_flags(feature_name => actor(feature_actors))
        end

        it { expect(Feature.enabled?(feature_name)).to eq(expected_result) }
        it { expect(Feature.disabled?(feature_name)).not_to eq(expected_result) }

        context 'default_enabled does not impact feature state' do
          it { expect(Feature.enabled?(feature_name, default_enabled: true)).to eq(expected_result) }
          it { expect(Feature.disabled?(feature_name, default_enabled: true)).not_to eq(expected_result) }
        end
      end
    end

    context 'when checking scoped state' do
      where(:feature_actors, :tested_actor, :expected_result) do
        false   | nil  | false
        true    | nil  | true
        false   | :A   | false
        true    | :A   | true
        :A      | nil  | false
        :A      | :A   | true
        :A      | :B   | false
        %i[A]   | nil  | false
        %i[A]   | :A   | true
        %i[A]   | :B   | false
        %i[A B] | nil  | false
        %i[A B] | :A   | true
        %i[A B] | :B   | true
      end

      with_them do
        before do
          stub_feature_flags(feature_name => actor(feature_actors))
        end

        it { expect(Feature.enabled?(feature_name, actor(tested_actor))).to eq(expected_result) }
        it { expect(Feature.disabled?(feature_name, actor(tested_actor))).not_to eq(expected_result) }

        context 'default_enabled does not impact feature state' do
          it { expect(Feature.enabled?(feature_name, actor(tested_actor), default_enabled: true)).to eq(expected_result) }
          it { expect(Feature.disabled?(feature_name, actor(tested_actor), default_enabled: true)).not_to eq(expected_result) }
        end
      end
    end

    context 'type handling' do
      context 'raises error' do
        where(:feature_actors) do
          ['string', 1, 1.0, OpenStruct.new]
        end

        with_them do
          subject { stub_feature_flags(feature_name => actor(feature_actors)) }

          it { expect { subject }.to raise_error(ArgumentError, /accepts only/) }
        end
      end

      context 'does not raise error' do
        where(:feature_actors) do
          [true, false, nil, stub_feature_flag_gate(100), User.new]
        end

        with_them do
          subject { stub_feature_flags(feature_name => actor(feature_actors)) }

          it { expect { subject }.not_to raise_error }
        end
      end
    end

    it 'subsquent run changes state' do
      # enable FF only on A
      stub_feature_flags({ feature_name => actor(%i[A]) })
      expect(Feature.enabled?(feature_name)).to eq(false)
      expect(Feature.enabled?(feature_name, actor(:A))).to eq(true)
      expect(Feature.enabled?(feature_name, actor(:B))).to eq(false)

      # enable FF only on B
      stub_feature_flags({ feature_name => actor(%i[B]) })
      expect(Feature.enabled?(feature_name)).to eq(false)
      expect(Feature.enabled?(feature_name, actor(:A))).to eq(false)
      expect(Feature.enabled?(feature_name, actor(:B))).to eq(true)

      # enable FF on all
      stub_feature_flags({ feature_name => true })
      expect(Feature.enabled?(feature_name)).to eq(true)
      expect(Feature.enabled?(feature_name, actor(:A))).to eq(true)
      expect(Feature.enabled?(feature_name, actor(:B))).to eq(true)

      # disable FF on all
      stub_feature_flags({ feature_name => false })
      expect(Feature.enabled?(feature_name)).to eq(false)
      expect(Feature.enabled?(feature_name, actor(:A))).to eq(false)
      expect(Feature.enabled?(feature_name, actor(:B))).to eq(false)
    end
  end

  describe 'stub timing' do
    context 'let_it_be variable' do
      let_it_be(:let_it_be_var) { Feature.enabled?(dummy_feature_flag) }

      it { expect(let_it_be_var).to eq true }
    end

    context 'before_all variable' do
      before_all do
        @suite_var = Feature.enabled?(dummy_feature_flag)
      end

      it { expect(@suite_var).to eq true }
    end

    context 'before(:all) variable' do
      before(:all) do
        @suite_var = Feature.enabled?(dummy_feature_flag)
      end

      it { expect(@suite_var).to eq true }
    end

    context 'with stub_feature_flags meta' do
      let(:var) { Feature.enabled?(dummy_feature_flag) }

      context 'as true', :stub_feature_flags do
        it { expect(var).to eq true }
      end

      context 'as false', stub_feature_flags: false do
        it { expect(var).to eq false }
      end
    end
  end

  def actor(actor)
    case actor
    when Array
      actor.map(&method(:actor))
    when Symbol # convert to flipper compatible object
      stub_feature_flag_gate(actor)
    else
      actor
    end
  end
end