From 2a93946824fc7ad4f4fb6639b0e4360f8fc6bc02 Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Mon, 29 Jan 2024 17:59:51 -0500 Subject: [PATCH 1/3] Add testing for other college field manipulation --- .../partials/_publications_colleges.html.erb | 2 +- .../other_college_field_manipulation_spec.rb | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 spec/features/publication_creation/other_college_field_manipulation_spec.rb diff --git a/app/views/partials/_publications_colleges.html.erb b/app/views/partials/_publications_colleges.html.erb index 16e3e1c7..7aee9b7c 100644 --- a/app/views/partials/_publications_colleges.html.erb +++ b/app/views/partials/_publications_colleges.html.erb @@ -1,4 +1,4 @@ -
+
<%= form.label(:college_ids, author_or_artist_label + " College(s)") %>
<%= form.collection_check_boxes :college_ids, College.all, :id, :name do |c| %>
diff --git a/spec/features/publication_creation/other_college_field_manipulation_spec.rb b/spec/features/publication_creation/other_college_field_manipulation_spec.rb new file mode 100644 index 00000000..47fdd3de --- /dev/null +++ b/spec/features/publication_creation/other_college_field_manipulation_spec.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Selecting Colleges for an Artwork', :feature, js: true do + let(:submitter) { FactoryBot.build(:submitter) } + let(:artwork) { FactoryBot.build(:artwork) } + + context 'from Artwork creation page' do + before do + create_submitter(submitter) + visit new_artwork_path + fill_in("artwork[author_first_name][]", with: artwork.author_first_name[0]) + fill_in("artwork[author_last_name][]", with: artwork.author_last_name[0]) + fill_in("artwork[work_title]", with: artwork.work_title) + end + + it 'selects an already listed college' do + within '#colleges-group' do + all('input[type="checkbox"]')[1].set(true) # Array is zero-indexed + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 1) + click_on('Submit') + + expect(page).to have_current_path(Rails.application.routes.url_helpers.publications_path) + # Click on the title of the newly created artwork + click_on(artwork.work_title) + expect(page).to have_text 'Arts and Sciences' + end + + context 'When selecting Other college' do + it 'toggles the Other College text field' do + other_checkbox = all('input[type="checkbox"]').last + other_college_field = find('#other_college_group', visible: :all) # Finds even if invisible + + # Initially, the Other College field should not be visible + expect(other_college_field).not_to be_visible + + # Select the Other checkbox + other_checkbox.set(true) + + # Now, the Other College field should be visible + expect(other_college_field).to be_visible + + # Unselect the Other checkbox + other_checkbox.set(false) + + # The Other College field should not be visible again + expect(other_college_field).not_to be_visible + end + + it 'allows user to select Other college without filling in the Other name' do + # The "Other" college is the last checkbox in the Colleges group + within '#colleges-group' do + all('input[type="checkbox"]').last.set(true) + end + + expect(page).to have_selector('input[type="checkbox"]:checked', count: 1) + click_on('Submit') + + expect(page).to have_current_path(Rails.application.routes.url_helpers.publications_path) + click_on(artwork.work_title) + expect(page).to have_text 'Other:' + end + + it 'remembers the typed in Other College when the Other checkbox is unchecked' do + within '#colleges-group' do + all('input[type="checkbox"]').last.set(true) + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 1) + + fill_in('artwork[other_college]', with: 'Other College Name') + + within '#colleges-group' do + all('input[type="checkbox"]').last.set(false) + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 0) + expect(page).to_not have_text('Other College') + + within '#colleges-group' do + all('input[type="checkbox"]').last.set(true) + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 1) + expect(page).to have_field('Other College', with: 'Other College Name') + end + + it 'saves both a listed college and Other college when selected' do + within '#colleges-group' do + all('input[type="checkbox"]')[1].set(true) # Array is zero-indexed + all('input[type="checkbox"]').last.set(true) + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 2) + fill_in('artwork[other_college]', with: 'Other College Name') + click_on('Submit') + + expect(page).to have_current_path(Rails.application.routes.url_helpers.publications_path) + click_on(artwork.work_title) + expect(page).to have_text 'Arts and Sciences' + expect(page).to have_text 'Other College Name' + end + end + end +end From 5132a4e17cae5d8e04f4bb9ac1dc36253cc1f158 Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Mon, 29 Jan 2024 18:05:28 -0500 Subject: [PATCH 2/3] Add in test that it doesn't save Other College when unchecked --- .../other_college_field_manipulation_spec.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spec/features/publication_creation/other_college_field_manipulation_spec.rb b/spec/features/publication_creation/other_college_field_manipulation_spec.rb index 47fdd3de..fa9b67ea 100644 --- a/spec/features/publication_creation/other_college_field_manipulation_spec.rb +++ b/spec/features/publication_creation/other_college_field_manipulation_spec.rb @@ -84,6 +84,27 @@ expect(page).to have_field('Other College', with: 'Other College Name') end + it 'does not save a typed in Other College if the Other checkbox is unchecked' do + within '#colleges-group' do + all('input[type="checkbox"]').last.set(true) + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 1) + + fill_in('artwork[other_college]', with: 'Other College Name') + + within '#colleges-group' do + all('input[type="checkbox"]').last.set(false) + end + expect(page).to have_selector('input[type="checkbox"]:checked', count: 0) + expect(page).to_not have_text('Other College') + + click_on('Submit') + + expect(page).to have_current_path(Rails.application.routes.url_helpers.publications_path) + click_on(artwork.work_title) + expect(page).to_not have_text 'Other College Name' + end + it 'saves both a listed college and Other college when selected' do within '#colleges-group' do all('input[type="checkbox"]')[1].set(true) # Array is zero-indexed From 20f7e3c7a545779ba69776636dac518e19ea0c8a Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Mon, 29 Jan 2024 18:08:36 -0500 Subject: [PATCH 3/3] Rubocop --- .../other_college_field_manipulation_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/features/publication_creation/other_college_field_manipulation_spec.rb b/spec/features/publication_creation/other_college_field_manipulation_spec.rb index fa9b67ea..115dc697 100644 --- a/spec/features/publication_creation/other_college_field_manipulation_spec.rb +++ b/spec/features/publication_creation/other_college_field_manipulation_spec.rb @@ -10,9 +10,9 @@ before do create_submitter(submitter) visit new_artwork_path - fill_in("artwork[author_first_name][]", with: artwork.author_first_name[0]) - fill_in("artwork[author_last_name][]", with: artwork.author_last_name[0]) - fill_in("artwork[work_title]", with: artwork.work_title) + fill_in('artwork[author_first_name][]', with: artwork.author_first_name[0]) + fill_in('artwork[author_last_name][]', with: artwork.author_last_name[0]) + fill_in('artwork[work_title]', with: artwork.work_title) end it 'selects an already listed college' do