Files
grecia/spec/components/admin/allowed_table_actions_component_spec.rb
Javi Martín 518af3eb97 Remove duplication in poll actions tables
We were using the same logic in four different places, so we're creating
a new class to handle that logic.

Note that I didn't find a way to delegate the `content` method to a
`Admin::TableActionsComponent`, so we're delegating the `action` method
instead. That means we need to create a method returning an
`Admin::TableActionsComponent`. We also need to cache this object;
otherwise we were getting an error when calling `actions.action` from
the `Admin::Poll::Questions::TableActionsComponent`.
2022-09-20 17:50:49 +02:00

58 lines
1.8 KiB
Ruby

require "rails_helper"
describe Admin::AllowedTableActionsComponent, controller: Admin::BaseController do
before do
sign_in(create(:administrator).user)
allow_any_instance_of(Admin::AllowedTableActionsComponent).to receive(:can?).and_return true
end
let(:record) { create(:banner, title: "Important!") }
it "renders edit and destroy actions by default if they're allowed" do
component = Admin::AllowedTableActionsComponent.new(record)
render_inline component
expect(page).to have_link count: 1
expect(page).to have_link "Edit"
expect(page).to have_button count: 1
expect(page).to have_button "Delete"
end
it "accepts an actions parameter" do
render_inline Admin::AllowedTableActionsComponent.new(record, actions: [:edit])
expect(page).to have_link "Edit"
expect(page).not_to have_button "Delete"
end
it "accepts custom options" do
render_inline Admin::AllowedTableActionsComponent.new(record, edit_text: "change", edit_path: "/myedit")
expect(page).to have_link "change", href: "/myedit"
end
it "accepts custom content" do
render_inline Admin::AllowedTableActionsComponent.new(record) do
"<a href='/'>Main</a>".html_safe
end
expect(page).to have_link count: 2
expect(page).to have_link "Main", href: "/"
expect(page).to have_link "Edit"
expect(page).to have_button count: 1
expect(page).to have_button "Delete"
end
it "only renders the allowed actions" do
component = Admin::AllowedTableActionsComponent.new(record)
allow(component).to receive(:can?).with(:edit, record).and_return true
allow(component).to receive(:can?).with(:destroy, record).and_return false
render_inline component
expect(page).to have_link "Edit"
expect(page).not_to have_button "Delete"
end
end