Documenting and addl testing FeatureFlag.enabled? (#16420)

Prior to this commit, we had a single test for `FeatureFlag.enabled?`,
namely that it delegates to `Flipper.enabled?`.  This commit adds some
tests to both verify behavior and communicate to developers the
intentions of the related methods:

- `FeatureFlag.enabled?`
- `FeatureFlag.accessible?`

My hope is the documentation and tests will help address the horrors
mentioned in conversation in #16416:

> I have lived the horrors of having a feature flag accidently
> flipped or incorrectly setup in the first place and half baked
> functionality going live.

Note: I'm not proposing that we test other `Flipper` methods, but
instead to ensure that we're testing and documenting the behavior of
what I believe to be the two primary mechanisms of "putting something
behind a feature flag."

Related to #16406, #16416
This commit is contained in:
Jeremy Friesen 2022-02-07 08:53:30 -05:00 committed by GitHub
parent 75ae759465
commit 446e74ca83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View file

@ -5,7 +5,22 @@ module FeatureFlag
class << self
delegate :add, :disable, :enable, :enabled?, :exist?, :remove, to: Flipper
# Unless the given :feature_flag_name is _explicitly_ disabled,
# @!method FeatureFlag.enabled?(feature_flag_name, *args)
#
# Answers if the :feature_flag_name has been _explicitly_ **enabled**.
#
# @param feature_flag_name [Symbol]
# @param args [Array] passed to Flipper.enabled?
#
# @return [TrueClass] the feature is enabled
# @return [FalseClass] the feature is not enabled.
#
# @see FeatureFlag.accessible?
#
# @see https://rubydoc.info/gems/yard/file/docs/Tags.md#method for details on the @!method
# macro used to compose this documentation.
# Unless the given :feature_flag_name is _explicitly_ **disabled**,
# this method returns true.
#
# @param feature_flag_name [Symbol]
@ -19,6 +34,8 @@ module FeatureFlag
# enabled, disabled, or has been removed), the feature is
# accessible.
#
# @see FeatureFlag.enabled?
#
# @see https://github.com/forem/forem/pull/8149
# for further discussion.
def accessible?(feature_flag_name, *args)

View file

@ -20,12 +20,32 @@ describe FeatureFlag, type: :service do
end
describe ".enabled?" do
subject(:method_call) { described_class.enabled?(flag) }
let(:flag) { "foo" }
it "calls Flipper's enabled? method" do
allow(Flipper).to receive(:enabled?).with("foo")
allow(Flipper).to receive(:enabled?).with(flag)
described_class.enabled?("foo")
described_class.enabled?(flag)
expect(Flipper).to have_received(:enabled?).with("foo")
expect(Flipper).to have_received(:enabled?).with(flag)
end
context "when flag explicitly enabled" do
before { described_class.enable(flag) }
it { is_expected.to be_truthy }
end
context "when flag doesn't exist" do
it { is_expected.to be_falsey }
end
context "when flag explicitly disabled" do
before { described_class.disable(flag) }
it { is_expected.to be_falsey }
end
end