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
|
require 'flipper/types/group'
RSpec.describe Flipper::Types::Group do
let(:fake_context) { double('FeatureCheckContext') }
subject do
Flipper.register(:admins, &:admin?)
end
describe '.wrap' do
context 'with group instance' do
it 'returns group instance' do
expect(described_class.wrap(subject)).to eq(subject)
end
end
context 'with Symbol group name' do
it 'returns group instance' do
expect(described_class.wrap(subject.name)).to eq(subject)
end
end
context 'with String group name' do
it 'returns group instance' do
expect(described_class.wrap(subject.name.to_s)).to eq(subject)
end
end
end
it 'initializes with name' do
group = described_class.new(:admins)
expect(group).to be_instance_of(described_class)
end
describe '#name' do
it 'returns name' do
expect(subject.name).to eq(:admins)
end
end
describe '#match?' do
let(:admin_actor) { double('Actor', admin?: true) }
let(:non_admin_actor) { double('Actor', admin?: false) }
it 'returns true if block matches' do
expect(subject.match?(admin_actor, fake_context)).to eq(true)
end
it 'returns false if block does not match' do
expect(subject.match?(non_admin_actor, fake_context)).to eq(false)
end
it 'returns true for truthy block results' do
group = described_class.new(:examples) do |actor|
actor.email =~ /@example\.com/
end
expect(group.match?(double('Actor', email: 'foo@example.com'), fake_context)).to be_truthy
end
it 'returns false for falsey block results' do
group = described_class.new(:examples) do |_actor|
nil
end
expect(group.match?(double('Actor'), fake_context)).to be_falsey
end
it 'returns true for truthy shortand block results' do
actor = Class.new do
def admin?
true
end
end.new
group = described_class.new(:admin, &:admin?)
expect(group.match?(actor, fake_context)).to be_truthy
end
it 'returns false for falsy shortand block results' do
actor = Class.new do
def admin?
false
end
end.new
group = described_class.new(:admin, &:admin?)
expect(group.match?(actor, fake_context)).to be_falsey
end
it 'can yield without context as block argument' do
context = Flipper::FeatureCheckContext.new(
feature_name: :my_feature,
values: Flipper::GateValues.new({}),
thing: Flipper::Types::Actor.new(Flipper::Actor.new(1))
)
group = Flipper.register(:group_with_context) { |actor| actor }
yielded_actor = group.match?(admin_actor, context)
expect(yielded_actor).to be(admin_actor)
end
it 'can yield with context as block argument' do
context = Flipper::FeatureCheckContext.new(
feature_name: :my_feature,
values: Flipper::GateValues.new({}),
thing: Flipper::Types::Actor.new(Flipper::Actor.new(1))
)
group = Flipper.register(:group_with_context) { |actor, context| [actor, context] }
yielded_actor, yielded_context = group.match?(admin_actor, context)
expect(yielded_actor).to be(admin_actor)
expect(yielded_context).to be(context)
end
end
end
|