Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

357 - Add testing for other college field manipulation #358

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/views/partials/_publications_colleges.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group col-md-6">
<div class="form-group col-md-6" id="colleges-group">
<%= form.label(:college_ids, author_or_artist_label + " College(s)") %><br />
<%= form.collection_check_boxes :college_ids, College.all, :id, :name do |c| %>
<div class="form-check">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# 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 '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
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