diff --git a/.ruby-gemset b/.ruby-gemset index 971b5415..8d6c54aa 100644 --- a/.ruby-gemset +++ b/.ruby-gemset @@ -1 +1 @@ -aaec +aaec \ No newline at end of file diff --git a/spec/features/author_management/adding_authors_spec.rb b/spec/features/author_management/adding_authors_spec.rb index e89a4993..e1f1dbef 100644 --- a/spec/features/author_management/adding_authors_spec.rb +++ b/spec/features/author_management/adding_authors_spec.rb @@ -4,6 +4,8 @@ describe 'Adding Authors', :feature, js: true do let(:submitter) { FactoryBot.create(:submitter) } + let(:other_publication) { FactoryBot.create(:other_publication, author_first_name: ['First0'], author_last_name: ['Last0'], submitter_id: submitter.id) } + let(:pub_id) { other_publication.id } before do create_submitter(submitter) @@ -11,7 +13,6 @@ it 'adds authors to a new publication' do visit new_other_publication_path - expect(page).to have_current_path(Rails.application.routes.url_helpers.new_other_publication_path) # Verify blank input fields for author's first name and last name # to be present on page load @@ -19,45 +20,33 @@ expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 1) check_field_values_by_index(0, '', '') - # Fill out the fields with the first author's name first_name_fields.last.set('First0') last_name_fields.last.set('Last0') - # Click "Add Author" and verify new and old fields - click_on 'Add Author' - - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 2) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 2) + add_author_or_artist_and_verify_field check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, '', '') - # Fill in second author's name first_name_fields.last.set('First1') last_name_fields.last.set('Last1') - # Click "Add Author" again - click_on 'Add Author' - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 3) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 3) + add_author_or_artist_and_verify_field + check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') check_field_values_by_index(2, '', '') - # Fill in third author's name first_name_fields.last.set('First2') last_name_fields.last.set('Last2') - # Click "Add Author" again - click_on 'Add Author' - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 4) + add_author_or_artist_and_verify_field + check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') check_field_values_by_index(2, 'First2', 'Last2') check_field_values_by_index(3, '', '') - # Fill in fourth author's name first_name_fields.last.set('First3') last_name_fields.last.set('Last3') @@ -72,56 +61,44 @@ # Click "Submit" and verify that we are redirected to the index page # and that a success message is displayed click_on 'Submit' - expect(page).to have_current_path(Rails.application.routes.url_helpers.publications_path) expect(page).to have_text 'Other Publication was successfully created.' + expect(page).to have_current_path(Rails.application.routes.url_helpers.publications_path) - # Click on the hyperlink on the id of the newly created publication - # and verify that the author names are correct - click_on OtherPublication.last.work_title.to_s - expect(page).to have_current_path(Rails.application.routes.url_helpers.other_publication_path(OtherPublication.last.id)) - expect(page).to have_text 'First0 Last0' - expect(page).to have_text 'First1 Last1' - expect(page).to have_text 'First2 Last2' - expect(page).to have_text 'First3 Last3' + # Check the last other_publication and verify that the author names are correct + last_other_publication = OtherPublication.last + expect(last_other_publication.author_first_name).to eq %w[First0 First1 First2 First3] + expect(last_other_publication.author_last_name).to eq %w[Last0 Last1 Last2 Last3] end it 'adds authors to an existing publication' do - # Create a new publication. Adding author functionality for a new publication - # is tested in the previous test. - create_other_publication # Defined in spec/support/helpers/feature_spec_helpers/author_management.rb - - # Click on the hyperlink on the id of the newly created publication - # and verify that the author names are correct - click_on OtherPublication.last.work_title.to_s - expect(page).to have_current_path(Rails.application.routes.url_helpers.other_publication_path(OtherPublication.last.id)) - expect(page).to have_selector('td', text: 'First0 Last0') # Information is in table format on the show page - - # Click on "Edit" and verify that we are redirected to the edit page - # and that the author names are correct - click_on 'Edit' - expect(page).to have_current_path(Rails.application.routes.url_helpers.edit_other_publication_path(OtherPublication.last.id)) + login_as_admin_feature_test + + visit edit_other_publication_path(pub_id) + expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 1) + + # Defined with the factory creation above check_field_values_by_index(0, 'First0', 'Last0') - # Add another author and verify that the author names are correct - click_on 'Add Author' + add_author_or_artist_and_verify_field + first_name_fields.last.set('First1') last_name_fields.last.set('Last1') + + add_author_or_artist_and_verify_field + check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') - # Add a third author and verify that the author names are correct - click_on 'Add Author' first_name_fields.last.set('First2') last_name_fields.last.set('Last2') - check_field_values_by_index(0, 'First0', 'Last0') - check_field_values_by_index(1, 'First1', 'Last1') - check_field_values_by_index(2, 'First2', 'Last2') - # Save the changes and verify that we are redirected to the show page - # and that the author names are correct click_on 'Submit' - expect(page).to have_current_path(Rails.application.routes.url_helpers.other_publication_path(OtherPublication.last.id)) expect(page).to have_text 'Other Publication was successfully updated.' - expect(page).to have_selector('td', text: 'First0 Last0, First1 Last1, First2 Last2') # Information is in table format on the show page + + other_publication = OtherPublication.find(pub_id) + other_publication.reload + + expect(other_publication.author_first_name).to eq %w[First0 First1 First2] + expect(other_publication.author_last_name).to eq %w[Last0 Last1 Last2] end end diff --git a/spec/features/author_management/author_vs_artist_labels_spec.rb b/spec/features/author_management/author_vs_artist_labels_spec.rb index e341c50e..d2b0f35c 100644 --- a/spec/features/author_management/author_vs_artist_labels_spec.rb +++ b/spec/features/author_management/author_vs_artist_labels_spec.rb @@ -8,6 +8,10 @@ # and deleting authors and artists. describe 'Author and Artist labels', :feature, js: true do let(:submitter) { FactoryBot.create(:submitter) } + let(:book) { FactoryBot.create(:book, author_first_name: ['First0'], author_last_name: ['Last0']) } + let(:book_id) { book.id } + let(:artwork) { FactoryBot.create(:artwork, author_first_name: ['First0'], author_last_name: ['Last0']) } + let(:artwork_id) { artwork.id } before do create_submitter(submitter) @@ -15,16 +19,17 @@ it 'uses the title of Author for new books' do visit new_book_path + expect(page).to have_selector("input[name='book[author_first_name][]']", visible: true) + expect(page).to have_content('Add Author') expect(page).not_to have_content('Add Artist') - first_name_fields.last.set('First0') - last_name_fields.last.set('Last0') - click_on 'Add Author' - first_name_fields.last.set('First1') - last_name_fields.last.set('Last1') - click_on 'Add Author' + + add_author_or_artist_and_verify_field + add_author_or_artist_and_verify_field + expect(page).to have_selector('button', text: 'Remove Author', count: 2) expect(page).not_to have_selector('button', text: 'Remove Artist') + first('button', text: 'Remove Author').click expect(page).to have_selector('button', text: 'Remove Author', count: 1) expect(page).not_to have_content('Artist') @@ -32,16 +37,57 @@ it 'has the title of Artist for new artworks' do visit new_artwork_path + expect(page).to have_selector("input[name='artwork[author_first_name][]']", visible: true) + + expect(page).to have_content('Add Artist') + expect(page).not_to have_content('Add Author') + + add_author_or_artist_and_verify_field + add_author_or_artist_and_verify_field + + expect(page).to have_selector('button', text: 'Remove Artist', count: 2) + expect(page).not_to have_selector('button', text: 'Remove Author') + + first('button', text: 'Remove Artist').click + expect(page).to have_selector('button', text: 'Remove Artist', count: 1) + expect(page).not_to have_content('Author') + end + + it 'uses the title of Author for existing books' do + login_as_admin_feature_test + + visit edit_book_path(book) + expect(page).to have_selector("input[name='book[author_first_name][]']", visible: true) + + expect(page).to have_content('Add Author') + expect(page).not_to have_content('Add Artist') + + add_author_or_artist_and_verify_field + add_author_or_artist_and_verify_field + + expect(page).to have_selector('button', text: 'Remove Author', count: 2) + expect(page).not_to have_selector('button', text: 'Remove Artist') + + first('button', text: 'Remove Author').click + expect(page).to have_selector('button', text: 'Remove Author', count: 1) + expect(page).not_to have_content('Artist') + end + + it 'has the title of Artist for existing artworks' do + login_as_admin_feature_test + + visit edit_artwork_path(artwork) + expect(page).to have_selector("input[name='artwork[author_first_name][]']", visible: true) + expect(page).to have_content('Add Artist') expect(page).not_to have_content('Add Author') - first_name_fields.last.set('First0') - last_name_fields.last.set('Last0') - click_on 'Add Artist' - first_name_fields.last.set('First1') - last_name_fields.last.set('Last1') - click_on 'Add Artist' + + add_author_or_artist_and_verify_field + add_author_or_artist_and_verify_field + expect(page).to have_selector('button', text: 'Remove Artist', count: 2) expect(page).not_to have_selector('button', text: 'Remove Author') + first('button', text: 'Remove Artist').click expect(page).to have_selector('button', text: 'Remove Artist', count: 1) expect(page).not_to have_content('Author') diff --git a/spec/features/author_management/removing_authors_spec.rb b/spec/features/author_management/removing_authors_spec.rb index de4ec0de..68cf8210 100644 --- a/spec/features/author_management/removing_authors_spec.rb +++ b/spec/features/author_management/removing_authors_spec.rb @@ -3,19 +3,16 @@ require 'rails_helper' describe 'Removing Authors', :feature, js: true do - let(:submitter) { FactoryBot.create(:submitter) } + let(:other_publication) { FactoryBot.create(:other_publication, author_first_name: %w[First0 First1 First2 First3], author_last_name: %w[Last0 Last1 Last2 Last3]) } + let(:pub_id) { other_publication.id } before do - create_submitter(submitter) - create_other_publication - add_three_more_authors_to_publication(OtherPublication.last) - visit edit_other_publication_path(OtherPublication.last) + login_as_admin_feature_test + visit edit_other_publication_path(other_publication) expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 4) end it 'removes the second author from the publication' do - # Remove the second author remove_author_at_index(1) expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 3) expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 3) diff --git a/spec/features/author_management/updating_authors_spec.rb b/spec/features/author_management/updating_authors_spec.rb index 7037e7d2..df8eef13 100644 --- a/spec/features/author_management/updating_authors_spec.rb +++ b/spec/features/author_management/updating_authors_spec.rb @@ -4,17 +4,16 @@ describe 'Adding Authors', :feature, js: true do let(:submitter) { FactoryBot.create(:submitter) } + let(:other_publication) { FactoryBot.create(:other_publication, author_first_name: %w[First0 First1 First2 First3], author_last_name: %w[Last0 Last1 Last2 Last3], submitter_id: submitter.id) } + let(:pub_id) { other_publication.id } before do - create_submitter(submitter) + login_as_admin_feature_test + visit edit_other_publication_path(pub_id) + expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) end it "allows the user to update the first author's information" do - create_other_publication - add_three_more_authors_to_publication(OtherPublication.last) - visit edit_other_publication_path(OtherPublication.last) - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 4) check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') check_field_values_by_index(2, 'First2', 'Last2') @@ -24,24 +23,15 @@ first_name_fields.first.set('First0modified') last_name_fields.first.set('Last0modified') click_on 'Submit' + expect(page).to have_content('Show Other Publication') + + other_publication.reload - expect(page).to have_current_path(other_publication_path(OtherPublication.last)) - expect(page).to have_content('First0modified') - expect(page).to have_content('Last0modified') - expect(page).to have_content('First1') - expect(page).to have_content('Last1') - expect(page).to have_content('First2') - expect(page).to have_content('Last2') - expect(page).to have_content('First3') - expect(page).to have_content('Last3') + expect(other_publication.author_first_name).to eq(%w[First0modified First1 First2 First3]) + expect(other_publication.author_last_name).to eq(%w[Last0modified Last1 Last2 Last3]) end it "allows the user to update the second author's information" do - create_other_publication - add_three_more_authors_to_publication(OtherPublication.last) - visit edit_other_publication_path(OtherPublication.last) - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 4) check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') check_field_values_by_index(2, 'First2', 'Last2') @@ -50,25 +40,16 @@ # Update the second author's name first_name_fields[1].set('First1modified') last_name_fields[1].set('Last1modified') + click_on 'Submit' + expect(page).to have_content('Show Other Publication') + + other_publication.reload - expect(page).to have_current_path(other_publication_path(OtherPublication.last)) - expect(page).to have_content('First0') - expect(page).to have_content('Last0') - expect(page).to have_content('First1modified') - expect(page).to have_content('Last1modified') - expect(page).to have_content('First2') - expect(page).to have_content('Last2') - expect(page).to have_content('First3') - expect(page).to have_content('Last3') + expect(other_publication.author_first_name).to eq(%w[First0 First1modified First2 First3]) end it "allows the user to update the last author's information" do - create_other_publication - add_three_more_authors_to_publication(OtherPublication.last) - visit edit_other_publication_path(OtherPublication.last) - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 4) check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') check_field_values_by_index(2, 'First2', 'Last2') @@ -77,31 +58,22 @@ # Update the last author's name first_name_fields.last.set('First3modified') last_name_fields.last.set('Last3modified') + click_on 'Submit' + expect(page).to have_content('Show Other Publication') + + other_publication.reload - expect(page).to have_current_path(other_publication_path(OtherPublication.last)) - expect(page).to have_content('First0') - expect(page).to have_content('Last0') - expect(page).to have_content('First1') - expect(page).to have_content('Last1') - expect(page).to have_content('First2') - expect(page).to have_content('Last2') - expect(page).to have_content('First3modified') - expect(page).to have_content('Last3modified') + expect(other_publication.author_first_name).to eq(%w[First0 First1 First2 First3modified]) + expect(other_publication.author_last_name).to eq(%w[Last0 Last1 Last2 Last3modified]) end it 'persists the changes when refreshing the page' do - create_other_publication - add_three_more_authors_to_publication(OtherPublication.last) - visit edit_other_publication_path(OtherPublication.last) - expect(page).to have_selector("input[name='other_publication[author_first_name][]']", count: 4) - expect(page).to have_selector("input[name='other_publication[author_last_name][]']", count: 4) check_field_values_by_index(0, 'First0', 'Last0') check_field_values_by_index(1, 'First1', 'Last1') check_field_values_by_index(2, 'First2', 'Last2') check_field_values_by_index(3, 'First3', 'Last3') - # Update the first author's name first_name_fields.first.set('First0modified') last_name_fields.first.set('Last0modified') first_name_fields[1].set('First1modified') @@ -111,8 +83,11 @@ first_name_fields.last.set('First3modified') last_name_fields.last.set('Last3modified') click_on 'Submit' + expect(page).to have_content('Show Other Publication') + + visit current_path + expect(page).to have_content('Show Other Publication') - page.driver.browser.navigate.refresh expect(page).to have_text('First0modified Last0modified, First1modified Last1modified, First2modified Last2modified, First3modified Last3modified') end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 418d45ae..ac94f5a3 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -75,6 +75,9 @@ # Include helpers for feature tests config.include FeatureSpecHelpers::AuthorManagement, type: :feature + + # Set Capybara's default wait time + Capybara.default_max_wait_time = 10 # seconds end def create_submitter(submitter) @@ -89,4 +92,5 @@ def create_submitter(submitter) fill_in('submitter[phone_number]', with: submitter.phone_number) fill_in('submitter[email_address]', with: submitter.email_address) click_on('Next') + expect(page).to have_content('Instructions') # Wait for the form to advance to the next page end diff --git a/spec/support/helpers/access_authorization_for_feature_tests.rb b/spec/support/helpers/access_authorization_for_feature_tests.rb index 1fd44114..4f5abbb3 100644 --- a/spec/support/helpers/access_authorization_for_feature_tests.rb +++ b/spec/support/helpers/access_authorization_for_feature_tests.rb @@ -5,6 +5,7 @@ def login_as_admin_feature_test fill_in('username', with: ENV.fetch('ADMIN_USERNAME', nil)) fill_in('password', with: ENV.fetch('ADMIN_PASSWORD', nil)) click_on('Submit') + expect(page).to have_content('Submitters') # Confirm that the admin is on the publications page end def visit_publications_page_as_submitter(submitter) diff --git a/spec/support/helpers/feature_spec_helpers/author_management.rb b/spec/support/helpers/feature_spec_helpers/author_management.rb index 64bb7d2b..2f0137ff 100644 --- a/spec/support/helpers/feature_spec_helpers/author_management.rb +++ b/spec/support/helpers/feature_spec_helpers/author_management.rb @@ -47,37 +47,48 @@ def first_author_element find('#author_group > :first-child') end - # navigates to the new other publication page and fills out the form. - def create_other_publication - visit new_other_publication_path - - # Fill out the fields with the first author's name - first_name_fields.last.set('First0') - last_name_fields.last.set('Last0') - - # Fill in the rest of the fields - fill_in 'other_publication[work_title]', with: 'Title' - fill_in 'other_publication[other_title]', with: 'Subtitle' - fill_in 'other_publication[uc_department]', with: 'Department' - fill_in 'other_publication[publication_date]', with: 'Date' - fill_in 'other_publication[url]', with: 'URL' - fill_in 'other_publication[doi]', with: 'DOI' - - click_on 'Submit' + def add_author_or_artist_and_verify_field + # Dynamically get the current count of fields before adding a new one + current_count = first_name_fields.size + + author_button_present = has_button?('Add Author', wait: false) + artist_button_present = has_button?('Add Artist', wait: false) + + raise_errors_for_incorrect_button_states(author_button_present, artist_button_present) + + # Trigger adding a new field based on which button is present + if author_button_present + click_on 'Add Author' + field_selector = "input[name$='[author_first_name][]']" + elsif artist_button_present + click_on 'Add Artist' + field_selector = "input[name$='[author_first_name][]']" # Artist first name field is still "author_first_name" + end + + # Wait for the next field to appear + expect(page).to have_selector(field_selector, count: current_count + 1) end - private + def raise_errors_for_incorrect_button_states(author_button_present, artist_button_present) + # Fail if both or none of the buttons are present + if author_button_present && artist_button_present + raise 'Both "Add Author" and "Add Artist" buttons are present, which is unexpected.' + elsif !author_button_present && !artist_button_present + raise 'Neither "Add Author" nor "Add Artist" button is present, cannot proceed.' + end + end # Returns the collection of author first name fields. # @return [Array] The collection of author first name fields. def first_name_fields - all("input[name$='[author_first_name][]']", wait: Capybara.default_max_wait_time) + expect(page).to have_selector("input[name$='[author_first_name][]']") + all("input[name$='[author_first_name][]']") end # Returns the collection of author last name fields. # @return [Array] The collection of author last name fields. def last_name_fields - all("input[name$='[author_last_name][]']", wait: Capybara.default_max_wait_time) + all("input[name$='[author_last_name][]']") end # Validates if the provided index is within the range of existing author fields. @@ -98,18 +109,5 @@ def verify_field_value(fields:, index:, expected_value:, field_name:) error_message = "#{field_name} at index #{index} does not match" expect(actual_value).to eq(expected_value), error_message end - - # Adds three more authors to a publication. Valid only within - # the context of a feature test with an already created publication. - def add_three_more_authors_to_publication(publication) - visit edit_other_publication_path(publication) - 3.times do - current_count = first_name_fields.size - click_on 'Add Author' - first_name_fields.last.set("First#{current_count}") - last_name_fields.last.set("Last#{current_count}") - end - click_on 'Submit' - end end end