Skip to content

Commit

Permalink
test form views with ChangeSet style form objects--create
Browse files Browse the repository at this point in the history
recent work has extended the ChangeSet style `Hyrax::ResourceForm` to support
most of the view behavior for work create forms. this adds tests of the relevant
form partials with the new form object.

the partial that handles collection membership is stubbed, since its behavior is
more complex and reproducing it in the new form should be considered separately.

likewise, tests for these partials as an edit form (for existing objects) are
left out; mainly because versions/optimistic locking becomes an issue in this
case. there's more groundwork to lay there.

to support this, a registration step is added to the new model generator. this
sets up the routes when we bulid the test app.
  • Loading branch information
tom johnson committed May 12, 2020
1 parent 3d5984c commit 75466ce
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .regen
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15
16
24 changes: 24 additions & 0 deletions lib/generators/hyrax/work_resource/work_resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def create_model_spec
rspec_installed?
end

# Inserts after the last registered work, or at the top of the config block
def register_work
config = 'config/initializers/hyrax.rb'
lastmatch = nil
in_root do
File.open(config).each_line do |line|
lastmatch = line if line.match?(/config.register_curation_concern :(?!#{file_name})/)
end
content = " # Injected via `rails g hyrax:work_resource #{class_name}`\n" \
" config.register_curation_concern #{registration_path_symbol}\n"
anchor = lastmatch || "Hyrax.config do |config|\n"
inject_into_file config, after: anchor do
content
end
end
end

def create_indexer
template('indexer.rb.erb', File.join('app/indexers/', class_path, "#{file_name}_indexer.rb"))
end
Expand Down Expand Up @@ -67,6 +84,13 @@ def rspec_installed?
defined?(RSpec) && defined?(RSpec::Rails)
end

def registration_path_symbol
return ":#{file_name}" if class_path.blank?
# creates a symbol with a path like "abc/scholarly_paper" where abc
# is the namespace and scholarly_paper is the resource name
":\"#{File.join(class_path, file_name)}\""
end

def revoking?
behavior == :revoke
end
Expand Down
151 changes: 99 additions & 52 deletions spec/views/hyrax/base/_form.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,127 @@
RSpec.describe 'hyrax/base/_form.html.erb', type: :view do
let(:work) do
stub_model(GenericWork, id: '456')
end
let(:ability) { double }

let(:form) do
Hyrax::GenericWorkForm.new(work, ability, controller)
end
let(:controller_action) { 'new' }
let(:controller_class) { Hyrax::MonographsController }
let(:options_presenter) { double(select_options: []) }

before do
# mock the admin set options presenter to avoid hitting Solr
allow(Hyrax::AdminSetOptionsPresenter).to receive(:new).and_return(options_presenter)
stub_template('hyrax/base/_form_progress.html.erb' => 'Progress')
# TODO: stub_model is not stubbing new_record? correctly on ActiveFedora models.
allow(work).to receive(:new_record?).and_return(true)
allow(work).to receive(:member_ids).and_return([1, 2])
allow(view).to receive(:curation_concern).and_return(work)
allow(controller).to receive(:current_user).and_return(stub_model(User))
assign(:form, form)
allow(controller).to receive(:controller_name).and_return('batch_uploads')
allow(controller).to receive(:action_name).and_return('new')
allow(controller).to receive(:repository).and_return(Hyrax::GenericWorksController.new.repository)
allow(controller).to receive(:blacklight_config).and_return(Hyrax::GenericWorksController.new.blacklight_config)

allow(form).to receive(:permissions).and_return([])
allow(form).to receive(:visibility).and_return('public')
stub_template 'hyrax/base/_form_files.html.erb' => 'files'
allow(controller).to receive(:action_name).and_return(controller_action)
allow(controller).to receive(:repository).and_return(controller_class.new.repository)
allow(controller).to receive(:blacklight_config).and_return(controller_class.new.blacklight_config)
end

context "for a new object" do
let(:work) { GenericWork.new }
context 'with a change_set style form' do
let(:form) { Hyrax::Forms::ResourceForm.for(work) }
let(:work) { build(:monograph, title: 'comet in moominland') }

context 'with batch_upload on' do
before do
allow(Flipflop).to receive(:batch_upload?).and_return(true)
before do
# TODO: unstub these when they are supported by the form
stub_template('hyrax/base/_form_member_of_collections.html.erb' => 'collection membership')
end

context 'for a new object' do
it 'renders a form' do
render

expect(rendered).to have_selector("form[action='/concern/monographs']")
end
it 'shows batch uploads' do
expect(rendered).to have_link('Batch upload', href: hyrax.new_batch_upload_path(payload_concern: 'GenericWork'))
expect(rendered).to have_selector("form[action='/concern/generic_works'][data-param-key='generic_work']")
# Draws the "Share" tab, with data for the javascript.
expect(rendered).to have_selector('#share[data-param-key="generic_work"]')

context 'with batch_upload off' do
before do
allow(Flipflop).to receive(:batch_upload?).and_return(false)
end

it 'hides batch uploads' do
render
expect(rendered).not_to have_link('Batch upload', href: hyrax.new_batch_upload_path(payload_concern: 'GenericWork'))
end
end
end

context 'with batch_upload off' do
before do
allow(Flipflop).to receive(:batch_upload?).and_return(false)
context 'with an existing object' do
let(:work) { FactoryBot.valkyrie_create(:monograph) }

xit 'renders a form' do
render
end
it 'hides batch uploads' do
expect(rendered).not_to have_link('Batch upload', href: hyrax.new_batch_upload_path(payload_concern: 'GenericWork'))

expect(rendered).to have_selector("form[action='/concern/monographs/#{work.id}']")
end
end
end

context "for a persisted object" do
let(:work) { stub_model(GenericWork, id: '456') }
context 'with a legacy GenericWork' do
let(:work) do
stub_model(GenericWork, id: '456')
end
let(:ability) { double }

let(:form) do
Hyrax::GenericWorkForm.new(work, ability, controller)
end

before do
# Add an error to the work
work.errors.add :base, 'broken'
work.errors.add :visibility, 'visibility_error'
allow(form).to receive(:select_files).and_return([])
render
stub_template('hyrax/base/_form_progress.html.erb' => 'Progress')
# TODO: stub_model is not stubbing new_record? correctly on ActiveFedora models.
allow(work).to receive(:new_record?).and_return(true)
allow(work).to receive(:member_ids).and_return([1, 2])
allow(view).to receive(:curation_concern).and_return(work)
allow(controller).to receive(:controller_name).and_return('batch_uploads')
allow(form).to receive(:permissions).and_return([])
allow(form).to receive(:visibility).and_return('public')
stub_template 'hyrax/base/_form_files.html.erb' => 'files'
end

context "for a new object" do
let(:work) { GenericWork.new }

context 'with batch_upload on' do
before do
allow(Flipflop).to receive(:batch_upload?).and_return(true)
render
end
it 'shows batch uploads' do
expect(rendered).to have_link('Batch upload', href: hyrax.new_batch_upload_path(payload_concern: 'GenericWork'))
expect(rendered).to have_selector("form[action='/concern/generic_works'][data-param-key='generic_work']")
# Draws the "Share" tab, with data for the javascript.
expect(rendered).to have_selector('#share[data-param-key="generic_work"]')
end
end

context 'with batch_upload off' do
before do
allow(Flipflop).to receive(:batch_upload?).and_return(false)
render
end
it 'hides batch uploads' do
expect(rendered).not_to have_link('Batch upload', href: hyrax.new_batch_upload_path(payload_concern: 'GenericWork'))
end
end
end

it "draws the page" do
expect(rendered).to have_selector("form[action='/concern/generic_works/456']")
expect(rendered).to have_selector("select#generic_work_resource_type", count: 1)
expect(rendered).to have_selector("select#generic_work_thumbnail_id", count: 1)
expect(rendered).to have_selector("select#generic_work_representative_id", count: 1)
context "for a persisted object" do
let(:work) { stub_model(GenericWork, id: '456') }

# It diplays form errors
expect(rendered).to have_content("broken")
expect(rendered).to have_content("visibility_error")
before do
# Add an error to the work
work.errors.add :base, 'broken'
work.errors.add :visibility, 'visibility_error'
allow(form).to receive(:select_files).and_return([])
render
end

it "draws the page" do
expect(rendered).to have_selector("form[action='/concern/generic_works/456']")
expect(rendered).to have_selector("select#generic_work_resource_type", count: 1)
expect(rendered).to have_selector("select#generic_work_thumbnail_id", count: 1)
expect(rendered).to have_selector("select#generic_work_representative_id", count: 1)

# It diplays form errors
expect(rendered).to have_content("broken")
expect(rendered).to have_content("visibility_error")
end
end
end
end

0 comments on commit 75466ce

Please sign in to comment.