8000 Extract some common test logic for later reuse · activeadmin/activeadmin@9736856 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9736856

Browse files
Extract some common test logic for later reuse
1 parent a496b8c commit 9736856

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

spec/support/active_admin_integration_spec_helper.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ def mock_action_view(base = MockActionView)
5555
base.new(view_paths, {}, controller)
5656
end
5757

58+
# Instantiates a fake decorated controller ready to unit test for a specific action
59+
def controller_with_decorator(action, decorator_class)
60+
method = action == "index" ? :apply_collection_decorator : :apply_decorator
61+
62+
controller_class = Class.new do
63+
include ActiveAdmin::ResourceController::Decorators
64+
65+
public method
66+
end
67+
68+
active_admin_config = double(decorator_class: decorator_class)
69+
70+
if action != "index"
71+
form_presenter = double(options: { decorate: !decorator_class.nil? })
72+
73+
allow(active_admin_config).to receive(:get_page_presenter).with(:form).and_return(form_presenter)
74+
end
75+
76+
controller = controller_class.new
77+
78+
allow(controller).to receive(:active_admin_config).and_return(active_admin_config)
79+
allow(controller).to receive(:action_name).and_return(action)
80+
81+
controller
82+
end
83+
5884
def view_paths
5985
paths = ActionController::Base.view_paths
6086
# the constructor for ActionView::Base changed from Rails 6

spec/unit/resource_controller/decorators_spec.rb

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,34 @@
11
require 'rails_helper'
22

33
RSpec.describe ActiveAdmin::ResourceController::Decorators do
4-
let(:controller_class) do
5-
Class.new do
6-
include ActiveAdmin::ResourceController::Decorators
7-
8-
public :apply_decorator, :apply_collection_decorator
9-
end
10-
end
11-
12-
let(:controller) { controller_class.new }
13-
let(:active_admin_config) { double(decorator_class: decorator_class) }
14-
before do
15-
allow(controller).to receive(:active_admin_config).and_return(active_admin_config)
16-
allow(controller).to receive(:action_name).and_return(action)
17-
end
18-
194
describe '#apply_decorator' do
20-
let(:action) { 'show' }
215
let(:resource) { Post.new }
6+
let(:controller) { controller_with_decorator(action, decorator_class) }
227
subject(:applied) { controller.apply_decorator(resource) }
238

24-
context 'with a decorator configured' do
9+
context "in show action" do
10+
let(:action) { 'show' }
2511
let(:decorator_class) { PostDecorator }
26-
it { is_expected.to be_kind_of(PostDecorator) }
27-
28-
context 'with form' do
29-
let(:action) { 'update' }
3012

31-
it "does not decorate when :decorate is set to false" do
32-
form = double
33-
allow(form).to receive(:options).and_return(decorate: false)
34-
allow(active_admin_config).to receive(:get_page_presenter).and_return(form)
35-
is_expected.not_to be_kind_of(PostDecorator)
36-
end
37-
end
13+
it { is_expected.to be_kind_of(PostDecorator) }
3814
end
3915

40-
context 'with no decorator configured' do
16+
context "in update action" do
17+
let(:action) { 'update' }
4118
let(:decorator_class) { nil }
42-
it { is_expected.to be_kind_of(Post) }
19+
20+
it { is_expected.not_to be_kind_of(PostDecorator) }
4321
end
4422
end
4523

4624
describe '#apply_collection_decorator' do
4725
before { Post.create! }
48-
let(:action) { 'index' }
4926
let(:collection) { Post.where nil }
27+
let(:controller) { controller_with_decorator("index", PostDecorator) }
5028
subject(:applied) { controller.apply_collection_decorator(collection) }
5129

5230
context 'when a decorator is configured' do
5331
context 'and it is using a recent version of draper' do
54-
let(:decorator_class) { PostDecorator }
55-
5632
it 'calling certain scope collections work' do
5733
# This is an example of one of the methods that was consistently
5834
# failing before this feature existed
@@ -67,21 +43,18 @@
6743
end
6844

6945
describe 'form actions' do
70-
let(:action) { 'edit' }
7146
let(:resource) { Post.new }
72-
let(:form_presenter) { double options: { decorate: decorate_form } }
73-
let(:decorator_class) { PostDecorator }
74-
before { allow(active_admin_config).to receive(:get_page_presenter).with(:form).and_return form_presenter }
47+
let(:controller) { controller_with_decorator("edit", decorator_class) }
7548

7649
subject(:applied) { controller.apply_decorator(resource) }
7750

7851
context 'when the form is not configured to decorate' do
79-
let(:decorate_form) { false }
52+
let(:decorator_class) { nil }
8053
it { is_expected.to be_kind_of(Post) }
8154
end
8255

8356
context 'when the form is configured to decorate' do
84-
let(:decorate_form) { true }
57+
let(:decorator_class) { PostDecorator }
8558
it { is_expected.to be_kind_of(PostDecorator) }
8659
end
8760
end

0 commit comments

Comments
 (0)
0