Files
nairobi/spec/features/dashboard/polls_spec.rb
Senén Rodero Rodríguez 5e98c23be5 Fix HTML markup
We cannot use 'id' html attributes on nested answers
because there will be many answers form each question so
this would have generated invalid HTML.
2019-03-21 14:51:17 +01:00

212 lines
5.8 KiB
Ruby

require 'rails_helper'
feature 'Polls' do
let!(:proposal) { create(:proposal, :draft) }
before do
login_as(proposal.author)
visit proposal_dashboard_path(proposal)
end
scenario 'Has a link to polls feature' do
expect(page).to have_link('Polls')
end
scenario 'Create a poll', :js do
click_link 'Polls'
click_link 'Create poll'
start_date = 1.week.from_now
end_date = 2.weeks.from_now
fill_in "poll_name", with: 'Upcoming poll'
fill_in 'poll_starts_at', with: start_date.strftime('%d/%m/%Y')
fill_in 'poll_ends_at', with: end_date.strftime('%d/%m/%Y')
fill_in 'poll_description', with: "Upcomming poll's description. This poll..."
expect(page).not_to have_css('#poll_results_enabled')
expect(page).not_to have_css('#poll_stats_enabled')
click_link 'Add question'
fill_in 'Question', with: 'First question'
click_link 'Add answer'
fill_in 'Title', with: 'First answer'
click_button 'Create poll'
expect(page).to have_content 'Poll created successfully'
expect(page).to have_content 'Upcoming poll'
expect(page).to have_content I18n.l(start_date.to_date)
end
scenario 'Create a poll redirects back to form when invalid data', js: true do
click_link 'Polls'
click_link 'Create poll'
click_button 'Create poll'
expect(page).to have_content('New poll')
end
scenario 'Edit poll is allowed for upcoming polls' do
poll = create(:poll, :incoming, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).to have_content('Edit survey')
click_link 'Edit survey'
end
click_button 'Update poll'
expect(page).to have_content 'Poll updated successfully'
end
scenario 'Edit poll redirects back when invalid data', js: true do
poll = create(:poll, :incoming, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).to have_content('Edit survey')
click_link 'Edit survey'
end
fill_in "poll_name", with: ''
click_button 'Update poll'
expect(page).to have_content('Edit poll')
end
scenario 'Edit poll is not allowed for current polls' do
poll = create(:poll, :current, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).not_to have_content('Edit survey')
end
end
scenario 'Edit poll is not allowed for expired polls' do
poll = create(:poll, :expired, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).not_to have_content('Edit survey')
end
end
scenario "Edit poll should allow to remove questions", :js do
poll = create(:poll, :incoming, related: proposal)
create(:poll_question, poll: poll)
create(:poll_question, poll: poll)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
click_link "Edit survey"
end
within ".js-questions" do
expect(page).to have_css ".nested-fields", count: 2
within first(".nested-fields") do
find("a.delete").click
end
expect(page).to have_css ".nested-fields", count: 1
end
click_button "Update poll"
visit edit_proposal_dashboard_poll_path(proposal, poll)
expect(page).to have_css ".nested-fields", count: 1
end
scenario "Edit poll should allow to remove answers", :js do
poll = create(:poll, :incoming, related: proposal)
question = create(:poll_question, poll: poll)
create(:poll_question_answer, question: question)
create(:poll_question_answer, question: question)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
click_link "Edit survey"
end
within ".js-questions .js-answers" do
expect(page).to have_css ".nested-fields", count: 2
within first(".nested-fields") do
find("a.delete").click
end
expect(page).to have_css ".nested-fields", count: 1
end
click_button "Update poll"
visit edit_proposal_dashboard_poll_path(proposal, poll)
within ".js-questions .js-answers" do
expect(page).to have_css ".nested-fields", count: 1
end
end
scenario 'View results not available for upcoming polls' do
poll = create(:poll, :incoming, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).not_to have_content('View results')
end
end
scenario 'View results available for current polls' do
poll = create(:poll, :current, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).to have_content('View results')
end
end
scenario 'View results available for expired polls' do
poll = create(:poll, :expired, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).to have_content('View results')
end
end
scenario 'View results redirects to results in public zone', js: true do
poll = create(:poll, :expired, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
click_link 'View results'
end
page.driver.browser.switch_to.window page.driver.browser.window_handles.last do
expect(page.current_path).to eq(results_poll_path(poll))
end
end
scenario 'Poll card' do
poll = create(:poll, :expired, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).to have_content(I18n.l(poll.starts_at.to_date))
expect(page).to have_content(I18n.l(poll.ends_at.to_date))
expect(page).to have_link(poll.title)
end
end
end