Files
nairobi/spec/system/admin/poll/questions/answers/documents/documents_spec.rb
Javi Martín 24099e880b Fix crash when adding invalid documents to answers
We were rendering the `new` action, but that action doesn't exist.
Before commit ec861ca8e, we were rendering the `edit` action of an
answer, which was confusing as well.

Note that, when adding an invalid document, `@answer.documents` contains
that invalid document (which is not present in the database). Since
we're rendering the index, this new document would appear in the list of
the documents that can be deleted; to avoid that, we're kind of
"reloading" the answer object in the component by finding the record in
the database. We aren't using `@answer.reload` because doing so would
remove the validation errors.
2022-09-20 17:50:49 +02:00

69 lines
2.1 KiB
Ruby

require "rails_helper"
describe "Documents", :admin do
let(:future_poll) { create(:poll, :future) }
context "Index" do
scenario "Answer with no documents" do
answer = create(:poll_question_answer)
document = create(:document)
visit admin_answer_documents_path(answer)
expect(page).not_to have_content(document.title)
expect(page).to have_link "Go back", href: admin_question_path(answer.question)
end
scenario "Answer with documents" do
answer = create(:poll_question_answer)
document = create(:document, documentable: answer)
visit admin_answer_documents_path(answer)
expect(page).to have_content(document.title)
end
end
describe "Create document for answer" do
scenario "with valid data" do
answer = create(:poll_question_answer, poll: future_poll)
visit admin_answer_documents_path(answer)
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf"))
click_button "Save"
expect(page).to have_content "Document uploaded successfully"
expect(page).to have_link "clippy.pdf"
end
scenario "with invalid data" do
answer = create(:poll_question_answer, poll: future_poll)
visit admin_answer_documents_path(answer)
documentable_attach_new_file(Rails.root.join("spec/fixtures/files/clippy.pdf"))
fill_in "Title", with: ""
click_button "Save"
expect(page).to have_content "1 error prevented this Answer from being saved"
expect(page).to have_content "Documents list"
end
end
scenario "Remove document from answer" do
answer = create(:poll_question_answer, poll: future_poll)
document = create(:document, documentable: answer)
visit admin_answer_documents_path(answer)
expect(page).to have_content(document.title)
accept_confirm("Are you sure? This action will delete \"#{document.title}\" and can't be undone.") do
click_button "Delete"
end
expect(page).to have_content "Document was deleted successfully."
expect(page).not_to have_content(document.title)
end
end