Files
nairobi/spec/system/admin/legislation/draft_versions_spec.rb
Javi Martín cfc60b5de4 Warn for changes just in markdown editor
This is the reason why this feature was implemented in the first
place: it's easy to open the editor, make some changes, close it, and
continue without realizing the changes have not been saved.

In the rest of the forms, this functionality is quite lacking. For
starters, some forms warn if there are unsaved changes, while some forms
don't, which is highly inconsistent and disorients users.

Furthermore, we were having problems with this feature after upgrading
Turbolinks, particularly in forms using CKEditor. In these cases, a lot
of hacking needs to be done in order to make this feature work properly,
since CKEditor adds some formatting automatically, and if this is done
after the form is serialized, we'll get some unexpected behavior. On the
other hand, comparing the value of a textarea against its `defaultValue`
property will work on every edge case, including using the browser's
back button or reloading the page.

Finally, users are used to the way web forms work, and aren't used to be
asked for confirmation when they change their mind and decide to leave
the page without saving the changes. Asking them for confirmation will
be annoying in most cases. Besides that, if they accidentally leave the
page, they can use the browser's back button and they'll recover the
unsaved changes.

It's true this won't happen it they accidentally close the browser's
window, but our WatchFormChanges functionality didn't work in this case
either. Using the "beforeunload" event adds more problems than it
solves, since it doesn't support custom messages (or, to be more
precise, modern browsers ignore custom messages), and it doesn't get
along with turbolinks.

Co-Authored-By: Senén Rodero Rodríguez <senenrodero@gmail.com>
2020-08-05 14:10:22 +02:00

151 lines
4.2 KiB
Ruby

require "rails_helper"
describe "Admin legislation draft versions" do
before do
admin = create(:administrator)
login_as(admin.user)
end
context "Feature flag" do
scenario "Disabled with a feature flag" do
Setting["process.legislation"] = nil
process = create(:legislation_process)
expect { visit admin_legislation_process_draft_versions_path(process) }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
context "Index" do
scenario "Displaying legislation process draft versions" do
process = create(:legislation_process, title: "An example legislation process")
draft_version = create(:legislation_draft_version, process: process, title: "Version 1")
visit admin_legislation_processes_path(filter: "all")
click_link "An example legislation process"
click_link "Drafting"
click_link "Version 1"
expect(page).to have_content(draft_version.title)
expect(page).to have_content(draft_version.changelog)
end
end
context "Create" do
scenario "Valid legislation draft version" do
create(:legislation_process, title: "An example legislation process")
visit admin_root_path
within("#side_menu") do
click_link "Collaborative Legislation"
end
click_link "All"
expect(page).to have_content "An example legislation process"
click_link "An example legislation process"
click_link "Drafting"
click_link "Create version"
fill_in "Version title", with: "Version 3"
fill_in "Changes", with: "Version 3 changes"
fill_in "Text", with: "Version 3 body"
within("form .end") do
click_button "Create version"
end
expect(page).to have_content "An example legislation process"
expect(page).to have_content "Version 3"
end
end
context "Update" do
scenario "Valid legislation draft version", :js do
process = create(:legislation_process, title: "An example legislation process")
create(:legislation_draft_version, title: "Version 1", process: process)
visit admin_root_path
within("#side_menu") do
click_link "Collaborative Legislation"
end
click_link "All"
expect(page).not_to have_link "All"
click_link "An example legislation process"
click_link "Drafting"
click_link "Version 1"
fill_in "Version title", with: "Version 1b"
fill_in_markdown_editor "Text", with: "# Version 1 body\r\n\r\nParagraph\r\n\r\n>Quote"
click_button "Save changes"
expect(page).to have_content "Version 1b"
end
end
context "Changing content with the markdown editor", :js do
let(:prompt) { "You've edited the text without saving it. Do you confirm to leave the page?" }
let(:version) { create(:legislation_draft_version, body: "Version 1") }
let(:path) do
edit_admin_legislation_process_draft_version_path(version.process, version)
end
scenario "asks for confimation when the content is modified" do
visit path
fill_in_markdown_editor "Text", with: "Version 1b"
dismiss_confirm(prompt) do
click_link "Proposals", match: :first
end
expect(page).to have_current_path(path)
end
scenario "asks for confimation after the page is restored from browser history" do
visit path
fill_in_markdown_editor "Text", with: "Version 1b"
accept_confirm(prompt) do
click_link "Proposals", match: :first
end
expect(page).to have_css("h2", text: "Proposals")
go_back
expect(page).to have_content version.process.title
accept_confirm(prompt) do
click_link "Proposals", match: :first
end
expect(page).to have_css("h2", text: "Proposals")
end
scenario "does not ask for confirmation when restoring the original content" do
visit path
fill_in_markdown_editor "Text", with: "Version 1b"
accept_confirm(prompt) do
click_link "Proposals", match: :first
end
expect(page).to have_css("h2", text: "Proposals")
go_back
fill_in_markdown_editor "Text", with: "Version 1"
click_link "Proposals", match: :first
expect(page).to have_css("h2", text: "Proposals")
end
end
end