/
collection_decorator_spec.rb
312 lines (255 loc) · 8.98 KB
/
collection_decorator_spec.rb
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
require 'spec_helper'
describe Draper::CollectionDecorator do
before { ApplicationController.new.view_context }
subject { Draper::CollectionDecorator.new(source, with: ProductDecorator) }
let(:source){ [Product.new, Product.new] }
let(:non_active_model_source){ NonActiveModelProduct.new }
it "decorates a collection's items" do
subject.each do |item|
item.should be_decorated_with ProductDecorator
end
end
it "sets the decorated items' source models" do
subject.map{|item| item.source}.should == source
end
describe "#source" do
it "duplicates the source collection" do
subject.source.should == source
subject.source.should_not be source
end
it "is frozen" do
subject.source.should be_frozen
end
it "is aliased to #to_source" do
subject.to_source.should == source
end
end
describe "#decorated_collection" do
it "is frozen" do
subject.decorated_collection.should be_frozen
end
end
context "with context" do
subject { Draper::CollectionDecorator.new(source, with: ProductDecorator, context: {some: 'context'}) }
its(:context) { should == {some: 'context'} }
it "passes context to the individual decorators" do
subject.each do |item|
item.context.should == {some: 'context'}
end
end
it "does not tie the individual decorators' contexts together" do
subject.each do |item|
item.context.should == {some: 'context'}
item.context = {alt: 'context'}
item.context.should == {alt: 'context'}
end
end
describe "#context=" do
it "updates the collection decorator's context" do
subject.context = {other: 'context'}
subject.context.should == {other: 'context'}
end
context "when the collection is already decorated" do
it "updates the items' context" do
subject.decorated_collection
subject.context = {other: 'context'}
subject.each do |item|
item.context.should == {other: 'context'}
end
end
end
context "when the collection has not yet been decorated" do
it "does not trigger decoration" do
subject.should_not_receive(:decorated_collection)
subject.context = {other: 'context'}
end
it "sets context after decoration is triggered" do
subject.context = {other: 'context'}
subject.each do |item|
item.context.should == {other: 'context'}
end
end
end
end
end
describe "#initialize" do
describe "options validation" do
let(:valid_options) { {with: ProductDecorator, context: {}} }
it "does not raise error on valid options" do
expect { Draper::CollectionDecorator.new(source, valid_options) }.to_not raise_error
end
it "raises error on invalid options" do
expect { Draper::CollectionDecorator.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
end
end
end
describe "item decoration" do
subject { subject_class.new(source, options) }
let(:decorator_classes) { subject.decorated_collection.map(&:class) }
let(:source) { [Product.new, Widget.new] }
context "when the :with option was given" do
let(:options) { {with: SpecificProductDecorator} }
context "and the decorator can't be inferred from the class" do
let(:subject_class) { Draper::CollectionDecorator }
it "uses the :with option" do
decorator_classes.should == [SpecificProductDecorator, SpecificProductDecorator]
end
end
context "and the decorator is inferrable from the class" do
let(:subject_class) { ProductsDecorator }
it "uses the :with option" do
decorator_classes.should == [SpecificProductDecorator, SpecificProductDecorator]
end
end
end
context "when the :with option was not given" do
let(:options) { {} }
context "and the decorator can't be inferred from the class" do
let(:subject_class) { Draper::CollectionDecorator }
it "infers the decorator from each item" do
decorator_classes.should == [ProductDecorator, WidgetDecorator]
end
end
context "and the decorator is inferrable from the class" do
let(:subject_class) { ProductsDecorator}
it "infers the decorator" do
decorator_classes.should == [ProductDecorator, ProductDecorator]
end
end
end
end
describe "#find" do
context "with a block" do
it "decorates Enumerable#find" do
subject.stub decorated_collection: []
subject.decorated_collection.should_receive(:find)
subject.find {|p| p.title == "title" }
end
end
context "without a block" do
it "decorates Model.find" do
source.should_not_receive(:find)
Product.should_receive(:find).with(1).and_return(:product)
subject.find(1).should == ProductDecorator.new(:product)
end
end
end
describe "#helpers" do
it "returns a HelperProxy" do
subject.helpers.should be_a Draper::HelperProxy
end
it "is aliased to #h" do
subject.h.should be subject.helpers
end
it "initializes the wrapper only once" do
helper_proxy = subject.helpers
helper_proxy.stub(:test_method) { "test_method" }
subject.helpers.test_method.should == "test_method"
subject.helpers.test_method.should == "test_method"
end
end
describe "#localize" do
before { subject.helpers.should_receive(:localize).with(:an_object, {some: 'parameter'}) }
it "delegates to helpers" do
subject.localize(:an_object, some: 'parameter')
end
it "is aliased to #l" do
subject.l(:an_object, some: 'parameter')
end
end
describe ".helpers" do
it "returns a HelperProxy" do
subject.class.helpers.should be_a Draper::HelperProxy
end
it "is aliased to .h" do
subject.class.h.should be subject.class.helpers
end
end
describe "#to_ary" do
# required for `render @collection` in Rails
it "delegates to the decorated collection" do
subject.stub decorated_collection: double(to_ary: :an_array)
subject.to_ary.should == :an_array
end
end
describe "#respond_to?" do
it "returns true for its own methods" do
subject.should respond_to :decorated_collection
end
it "returns true for the wrapped collection's methods" do
source.stub(:respond_to?).with(:whatever, true).and_return(true)
subject.respond_to?(:whatever, true).should be_true
end
end
it "delegates array methods to the decorated collection" do
subject.stub decorated_collection: []
subject.decorated_collection.should_receive(:[]).with(42).and_return(:the_answer)
subject[42].should == :the_answer
end
describe "#==" do
context "when comparing to a collection decorator with the same source" do
it "returns true" do
a = Draper::CollectionDecorator.new(source, with: ProductDecorator)
b = ProductsDecorator.new(source)
a.should == b
end
end
context "when comparing to a collection decorator with a different source" do
it "returns false" do
a = Draper::CollectionDecorator.new(source, with: ProductDecorator)
b = ProductsDecorator.new([Product.new])
a.should_not == b
end
end
context "when comparing to a collection of the same items" do
it "returns true" do
a = Draper::CollectionDecorator.new(source, with: ProductDecorator)
b = source.dup
a.should == b
end
end
context "when comparing to a collection of different items" do
it "returns true" do
a = Draper::CollectionDecorator.new(source, with: ProductDecorator)
b = [Product.new]
a.should_not == b
end
end
end
it "pretends to be the source class" do
subject.kind_of?(source.class).should be_true
subject.is_a?(source.class).should be_true
end
it "is still its own class" do
subject.kind_of?(subject.class).should be_true
subject.is_a?(subject.class).should be_true
end
describe "#method_missing" do
before do
class << source
def page_number
42
end
end
end
it "proxies unknown methods to the source collection" do
subject.page_number.should == 42
end
end
describe "#to_s" do
subject { Draper::CollectionDecorator.new(source, options) }
let(:source) { ["a", "b", "c"] }
context "when :with option was given" do
let(:options) { {with: ProductDecorator} }
it "returns a string representation of the CollectionDecorator" do
subject.to_s.should == '#<CollectionDecorator of ProductDecorator for ["a", "b", "c"]>'
end
end
context "when :with option was not given" do
let(:options) { {} }
it "returns a string representation of the CollectionDecorator" do
subject.to_s.should == '#<CollectionDecorator of inferred decorators for ["a", "b", "c"]>'
end
end
end
end