Files
nairobi/spec/components/admin/table_actions_component_spec.rb
Javi Martín 6510cb9615 Add a more detailed confirmation message
The message "Are you sure?" is usually followed by blindly clicking
"Yes" without really thinking about what one is doing. So we're
including a bit more information about what's about to happen. That way
it's more likely users will notice it when they accidentally click the
wrong button.

Ideally we would offer the option to undo every common action and then
we wouldn't have to ask for confirmation. But since that isn't the case,
for now we're adding a better confirmation message.

Note we're removing the `resource_name` parameter from the translation
to confirm the action of deleting a record. The reason is, in many
languages it only makes sense to add the model name when it's got an
associated article, and, unlike in English (where "the" is used for
every word), that article is different depending on the noun it's
related to. So we'd have to provide a translation like "name with
article, when singular" for every model.

The complexity of these translations could scalate quickly. And, given
the context, IMHO it isn't essential to add the resouce name. When we're
in the proposals index and there's a proposal named "Improve XYZ", and
we click on "Delete" and see a message saying "This action will delete
XYZ", it is implied that XYZ is a proposal.

So instead we're changing the message so it works for every record with
no need of noun-dependent articles.
2021-09-20 20:27:37 +02:00

81 lines
2.9 KiB
Ruby

require "rails_helper"
describe Admin::TableActionsComponent, controller: Admin::BaseController do
let(:record) { create(:banner, title: "Important!") }
it "renders links to edit and destroy a record by default" do
render_inline Admin::TableActionsComponent.new(record)
expect(page).to have_css "a", count: 2
expect(page).to have_css "a[href*='edit']", exact_text: "Edit"
expect(page).to have_css "a[aria-label='Edit Important!']", exact_text: "Edit"
expect(page).to have_css "a[data-method='delete']", exact_text: "Delete"
expect(page).to have_css "a[aria-label='Delete Important!']", exact_text: "Delete"
end
context "actions parameter is passed" do
it "renders a link to edit a record if passed" do
render_inline Admin::TableActionsComponent.new(record, actions: [:edit])
expect(page).to have_link "Edit"
expect(page).not_to have_link "Delete"
end
it "renders a link to destroy a record if passed" do
render_inline Admin::TableActionsComponent.new(record, actions: [:destroy])
expect(page).to have_link "Delete"
expect(page).not_to have_link "Edit"
end
end
it "allows custom texts for actions" do
render_inline Admin::TableActionsComponent.new(record, edit_text: "change", destroy_text: "annihilate")
expect(page).to have_link "annihilate"
expect(page).to have_link "change"
expect(page).not_to have_link "Delete"
expect(page).not_to have_link "Edit"
end
it "allows custom URLs" do
render_inline Admin::TableActionsComponent.new(record, edit_path: "/myedit", destroy_path: "/mydestroy")
expect(page).to have_link "Edit", href: "/myedit"
expect(page).to have_link "Delete", href: "/mydestroy"
end
it "allows custom confirmation text" do
render_inline Admin::TableActionsComponent.new(record, destroy_confirmation: "Are you mad? Be careful!")
expect(page).to have_css "a[data-confirm='Are you mad? Be careful!']"
end
it "allows custom options" do
render_inline Admin::TableActionsComponent.new(record, edit_options: { id: "edit_me" })
expect(page).to have_css "a#edit_me"
end
it "allows custom content" do
render_inline Admin::TableActionsComponent.new(record) do
"<a href='/'>Main</a>".html_safe
end
expect(page).to have_css "a", count: 3
expect(page).to have_link "Main", href: "/"
expect(page).to have_link "Edit"
expect(page).to have_link "Delete"
end
context "different namespace" do
it "generates links to different namespaces", controller: SDGManagement::BaseController do
render_inline Admin::TableActionsComponent.new(create(:sdg_local_target))
expect(page).to have_css "a", count: 2
expect(page).to have_css "a[href^='/sdg_management/'][href*='edit']", text: "Edit"
expect(page).to have_css "a[href^='/sdg_management/'][data-method='delete']", text: "Delete"
end
end
end