Skip to content

Commit

Permalink
refactor CollectionTypeForm specs to test stubbed behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tamsin johnson authored and Tom Johnson committed Jul 13, 2020
1 parent 21ec90b commit 0a553ec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
4 changes: 4 additions & 0 deletions app/forms/hyrax/forms/admin/collection_type_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ class CollectionTypeForm
:assigns_visibility, :id, :collection_type_participants, :persisted?,
:admin_set?, :user_collection?, :badge_color, to: :collection_type

##
# @return [Boolean]
def all_settings_disabled?
collections? || admin_set? || user_collection?
end

##
# @return [Boolean]
def share_options_disabled?
all_settings_disabled? || !sharable
end
Expand Down
34 changes: 13 additions & 21 deletions spec/forms/hyrax/forms/admin/collection_type_form_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
RSpec.describe Hyrax::Forms::Admin::CollectionTypeForm do
subject(:form) { described_class.new }
RSpec.describe Hyrax::Forms::Admin::CollectionTypeForm, :clean_repo do
subject(:form) { described_class.new(collection_type: collection_type) }
let(:collection_type) { FactoryBot.build(:collection_type) }

shared_context 'with a collection' do
Expand Down Expand Up @@ -28,13 +28,9 @@
it { is_expected.to delegate_method(:badge_color).to(:collection_type) }

describe '#all_settings_disabled?' do
before do
allow(form).to receive(:collection_type).and_return(collection_type)
end

context 'when editing admin set collection type' do
before do
allow(form).to receive(:admin_set?).and_return(true)
allow(collection_type).to receive(:admin_set?).and_return(true)
end

it 'returns true' do
Expand All @@ -44,7 +40,7 @@

context 'when editing user collection type' do
before do
allow(form).to receive(:user_collection?).and_return(true)
allow(collection_type).to receive(:user_collection?).and_return(true)
end

it 'returns true' do
Expand All @@ -62,8 +58,8 @@

context 'when not admin set collection type AND not user collection type AND there are no collections of this collection type' do
before do
allow(form).to receive(:admin_set?).and_return(false)
allow(form).to receive(:user_collection?).and_return(false)
allow(collection_type).to receive(:admin_set?).and_return(false)
allow(collection_type).to receive(:user_collection?).and_return(false)
end

it 'returns false' do
Expand All @@ -72,14 +68,10 @@
end
end

describe 'share_options_disabled?' do
before do
allow(form).to receive(:collection_type).and_return(collection_type)
end

describe '#share_options_disabled?' do
context 'when all settings are disabled' do
before do
allow(form).to receive(:all_settings_disabled?).and_return(true)
allow(collection_type).to receive(:user_collection?).and_return(true)
end

it 'returns true' do
Expand All @@ -89,22 +81,22 @@

context 'when collection type sharable setting is off' do
before do
allow(form).to receive(:sharable).and_return(false)
allow(collection_type).to receive(:sharable).and_return(false)
end

it 'returns true' do
expect(subject.share_options_disabled?).to be true
expect(form.share_options_disabled?).to be true
end
end

context 'when all options are not disabled and the collection type sharable setting is on' do
before do
allow(form).to receive(:all_settings_disabled?).and_return(false)
allow(form).to receive(:sharable).and_return(true)
allow(collection_type).to receive(:all_settings_disabled?).and_return(false)
allow(collection_type).to receive(:sharable).and_return(true)
end

it 'returns false' do
expect(subject.share_options_disabled?).to be false
expect(form.share_options_disabled?).to be false
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
collection_type_allow_multiple_membership
].freeze

let(:collection_type_form) { Hyrax::Forms::Admin::CollectionTypeForm.new }
let(:collection_type_form) { Hyrax::Forms::Admin::CollectionTypeForm.new(collection_type: collection_type) }
let(:collection_type) { stub_model(Hyrax::CollectionType) }

let(:form) do
Expand All @@ -24,15 +24,14 @@
end

before do
collection_type_form.collection_type = collection_type
allow(view).to receive(:f).and_return(form)
allow(form).to receive(:object).and_return(collection_type_form)
end

context 'for non-special collection types' do
context "when collection_type.collections? is false" do
before do
allow(collection_type).to receive(:collections?).and_return(false)
allow(collection_type_form).to receive(:collections?).and_return(false)
render
end

Expand All @@ -52,8 +51,7 @@

context "when collection_type.collections? is true" do
before do
collection_type_form.collection_type = collection_type
allow(collection_type).to receive(:collections?).and_return(true)
allow(collection_type_form).to receive(:collections?).and_return(true)
assign(:form, collection_type_form)
allow(view).to receive(:f).and_return(form)
render
Expand All @@ -72,7 +70,7 @@
context 'when all_settings_disabled? is true (admin_set or user collection type)' do
before do
allow(collection_type_form).to receive(:all_settings_disabled?).and_return(true)
allow(collection_type).to receive(:collections?).and_return(false)
allow(collection_type_form).to receive(:collections?).and_return(false)
render
end

Expand Down

0 comments on commit 0a553ec

Please sign in to comment.