-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpresenter_spec.rb
More file actions
285 lines (229 loc) · 8.52 KB
/
presenter_spec.rb
File metadata and controls
285 lines (229 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
describe Curly::Presenter do
class CircusPresenter < Curly::Presenter
module MonkeyComponents
def monkey
end
end
exposes_helper :foo
include MonkeyComponents
presents :midget, :clown, default: nil
presents :elephant, default: "Dumbo"
presents :puma, default: -> { 'block' }
presents(:lion) { @elephant.upcase }
presents(:something) { self }
attr_reader :midget, :clown, :elephant, :puma, :lion, :something
end
class FrenchCircusPresenter < CircusPresenter
presents :elephant, default: "Babar"
end
class FancyCircusPresenter < CircusPresenter
presents :champagne
end
class CircusPresenter::MonkeyPresenter < Curly::Presenter
end
module PresenterContainer
class NestedPresenter < Curly::Presenter
end
module PresenterSubcontainer
class SubNestedPresenter < Curly::Presenter
end
end
end
describe "#initialize" do
let(:context) { double("context") }
it "sets the presented identifiers as instance variables" do
presenter = CircusPresenter.new(context,
midget: "Meek Harolson",
clown: "Bubbles"
)
expect(presenter.midget).to eq "Meek Harolson"
expect(presenter.clown).to eq "Bubbles"
end
it "raises an exception if a required identifier is not specified" do
expect {
FancyCircusPresenter.new(context, {})
}.to raise_exception(ArgumentError, "required identifier `champagne` missing")
end
it "allows specifying default values for identifiers" do
# Make sure subclasses can change default values.
french_presenter = FrenchCircusPresenter.new(context)
expect(french_presenter.elephant).to eq "Babar"
expect(french_presenter.lion).to eq 'BABAR'
expect(french_presenter.puma).to be_a Proc
# The subclass shouldn't change the superclass' defaults, though.
presenter = CircusPresenter.new(context)
expect(presenter.elephant).to eq "Dumbo"
expect(presenter.lion).to eq 'DUMBO'
expect(presenter.puma).to be_a Proc
end
it "doesn't call a block if given as a value for identifiers" do
lion = proc { 'Simba' }
presenter = CircusPresenter.new(context, lion: lion)
expect(presenter.lion).to be lion
end
it "calls default blocks in the instance of the presenter" do
presenter = CircusPresenter.new(context)
expect(presenter.something).to be presenter
end
end
describe "#method_missing" do
let(:context) { double("context") }
subject {
CircusPresenter.new(context,
midget: "Meek Harolson",
clown: "Bubbles")
}
it "delegates calls to the context" do
expect(context).to receive(:undefined).once
subject.undefined
end
it "allows method calls on context-defined methods" do
expect(context).to receive(:respond_to?).
with(:undefined, false).once.and_return(true)
subject.method(:undefined)
end
end
describe ".exposes_helper" do
let(:context) { double("context") }
subject {
CircusPresenter.new(context,
midget: "Meek Harolson",
clown: "Bubbles")
}
it "allows a method as a component" do
CircusPresenter.component_available?(:foo)
end
it "delegates the call to the context" do
expect(context).to receive(:foo).once
expect(subject).not_to receive(:method_missing)
subject.foo
end
it "doesn't delegate other calls to the context" do
expect { subject.bar }.to raise_error RSpec::Mocks::MockExpectationError
end
end
describe ".presenter_for_path" do
it "returns the presenter class for the given path" do
presenter = double("presenter")
stub_const("Foo::BarPresenter", presenter)
expect(Curly::Presenter.presenter_for_path("foo/bar")).to eq presenter
end
it "returns nil if there is no presenter for the given path" do
expect(Curly::Presenter.presenter_for_path("foo/bar")).to be_nil
end
end
describe ".presenter_for_name" do
it 'looks through the container namespaces' do
expect(PresenterContainer::PresenterSubcontainer::SubNestedPresenter.presenter_for_name('nested')).to eq PresenterContainer::NestedPresenter
end
it 'looks through the container namespaces' do
expect(Curly::Presenter.presenter_for_name('presenter_container/presenter_subcontainer/nested', [])).to eq(PresenterContainer::NestedPresenter)
end
it "returns the presenter class for the given name" do
expect(CircusPresenter.presenter_for_name("monkey")).to eq CircusPresenter::MonkeyPresenter
end
it "looks in the namespace" do
expect(CircusPresenter.presenter_for_name("french_circus")).to eq FrenchCircusPresenter
end
it "returns Curly::PresenterNameError if the presenter class doesn't exist" do
expect { CircusPresenter.presenter_for_name("clown") }.to raise_exception(Curly::PresenterNameError)
end
end
describe ".available_components" do
it "includes the methods on the presenter" do
expect(CircusPresenter.available_components).to include("midget")
end
it "does not include methods on the Curly::Presenter base class" do
expect(CircusPresenter.available_components).not_to include("cache_key")
end
end
describe ".component_available?" do
it "returns true if the method is available" do
expect(CircusPresenter.component_available?("midget")).to eq true
end
it "returns false if the method is not available" do
expect(CircusPresenter.component_available?("bear")).to eq false
end
end
describe ".version" do
it "sets the version of the presenter" do
presenter1 = Class.new(Curly::Presenter) do
version 42
end
presenter2 = Class.new(Curly::Presenter) do
version 1337
end
expect(presenter1.version).to eq 42
expect(presenter2.version).to eq 1337
end
it "returns 0 if no version has been set" do
presenter = Class.new(Curly::Presenter)
expect(presenter.version).to eq 0
end
end
describe ".cache_key" do
it "includes the presenter's class name and version" do
presenter = Class.new(Curly::Presenter) { version 42 }
stub_const("Foo::BarPresenter", presenter)
expect(Foo::BarPresenter.cache_key).to eq "Foo::BarPresenter/42"
end
it "includes the cache keys of presenters in the dependency list" do
presenter = Class.new(Curly::Presenter) do
version 42
depends_on 'foo/bum'
end
dependency = Class.new(Curly::Presenter) do
version 1337
end
stub_const("Foo::BarPresenter", presenter)
stub_const("Foo::BumPresenter", dependency)
cache_key = Foo::BarPresenter.cache_key
expect(cache_key).to eq "Foo::BarPresenter/42/Foo::BumPresenter/1337"
end
it "includes the cache keys of all presenters in the dependency list" do
presenter = Class.new(Curly::Presenter) do
version 42
depends_on 'foo/bum'
depends_on 'foo/aum'
end
dependency = Class.new(Curly::Presenter) do
version 1337
end
dependency_2 = Class.new(Curly::Presenter) do
version 1338
end
stub_const("Foo::BarPresenter", presenter)
stub_const("Foo::BumPresenter", dependency)
stub_const("Foo::AumPresenter", dependency_2)
cache_key = Foo::BarPresenter.cache_key
expect(cache_key).to eq "Foo::BarPresenter/42/Foo::AumPresenter/1338/Foo::BumPresenter/1337"
end
it "uses the view path of a dependency if there is no presenter for it" do
presenter = Class.new(Curly::Presenter) do
version 42
depends_on 'foo/bum'
end
stub_const("Foo::BarPresenter", presenter)
cache_key = Foo::BarPresenter.cache_key
expect(cache_key).to eq "Foo::BarPresenter/42/foo/bum"
end
end
describe ".dependencies" do
it "returns the dependencies defined for the presenter" do
presenter = Class.new(Curly::Presenter) { depends_on 'foo' }
expect(presenter.dependencies.to_a).to eq ['foo']
end
it "includes the dependencies defined for parent classes" do
Curly::Presenter.dependencies
parent = Class.new(Curly::Presenter) { depends_on 'foo' }
presenter = Class.new(parent) { depends_on 'bar' }
expect(presenter.dependencies).to eq ['bar', 'foo']
end
it "doesn’t include duplicates" do
Curly::Presenter.dependencies
parent = Class.new(Curly::Presenter) { depends_on 'foo' }
presenter = Class.new(parent) { depends_on 'bar'; depends_on 'foo' }
expect(presenter.dependencies).to eq ['bar', 'foo']
end
end
end