magicka 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,298 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Magicka::Aggregator do
6
+ subject(:aggregator) { aggregator_class.new(renderer, model) }
7
+
8
+ let(:aggregator_class) { Class.new(described_class) }
9
+ let(:model) { :my_model }
10
+ let(:renderer) { instance_double('renderer') }
11
+ let(:template) { 'templates/forms/input' }
12
+ let(:locals) { {} }
13
+
14
+ describe '.type' do
15
+ let(:type) { %w[form display other].sample }
16
+
17
+ it 'sets aggregator type' do
18
+ expect { aggregator_class.type(type) }
19
+ .to change(aggregator_class, :type)
20
+ .from(nil).to(type.to_sym)
21
+ end
22
+
23
+ context 'when type has not been set' do
24
+ let(:aggregator_class) do
25
+ Class.new(described_class) do
26
+ def self.name
27
+ 'Magicka::MyClass'
28
+ end
29
+ end
30
+ end
31
+
32
+ it 'Uses class name as type' do
33
+ expect(aggregator_class.type)
34
+ .to eq(:my_class)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '.with_element' do
40
+ context 'when seeting element class only' do
41
+ it do
42
+ expect { aggregator_class.with_element(Magicka::Input) }
43
+ .to add_method(:input)
44
+ .to(aggregator)
45
+ end
46
+
47
+ context 'when built method is called' do
48
+ let(:template) { 'templates/forms/input' }
49
+ let(:field) { :field }
50
+ let(:label) { 'Label' }
51
+ let(:placeholder) { 'Value' }
52
+
53
+ let(:locals) do
54
+ {
55
+ field: field,
56
+ label: label,
57
+ ng_errors: 'my_model.errors.field',
58
+ ng_model: 'my_model.field',
59
+ placeholder: placeholder
60
+ }
61
+ end
62
+
63
+ let(:arguments) do
64
+ {
65
+ label: label,
66
+ placeholder: placeholder
67
+ }
68
+ end
69
+
70
+ before do
71
+ aggregator_class.with_element(Magicka::Input)
72
+
73
+ allow(renderer)
74
+ .to receive(:render)
75
+ .with(partial: template, locals: locals)
76
+ end
77
+
78
+ it 'renders an input' do
79
+ aggregator.input(field, arguments)
80
+
81
+ expect(renderer).to have_received(:render)
82
+ end
83
+
84
+ context 'when passing a custom model' do
85
+ let(:locals) do
86
+ {
87
+ field: field,
88
+ label: label,
89
+ ng_errors: 'my_custom_model.errors.field',
90
+ ng_model: 'my_custom_model.field',
91
+ placeholder: placeholder
92
+ }
93
+ end
94
+
95
+ it 'renders an input' do
96
+ aggregator.input(field, model: 'my_custom_model', **arguments)
97
+
98
+ expect(renderer).to have_received(:render)
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ context 'when seeting element class and method' do
105
+ it do
106
+ expect { aggregator_class.with_element(Magicka::Input, :my_input) }
107
+ .to add_method(:my_input)
108
+ .to(aggregator)
109
+ end
110
+
111
+ context 'when built method is called' do
112
+ let(:template) { 'templates/forms/input' }
113
+ let(:field) { :field }
114
+ let(:label) { 'Label' }
115
+ let(:placeholder) { 'Value' }
116
+
117
+ let(:locals) do
118
+ {
119
+ field: field,
120
+ label: label,
121
+ ng_errors: 'my_model.errors.field',
122
+ ng_model: 'my_model.field',
123
+ placeholder: placeholder
124
+ }
125
+ end
126
+
127
+ let(:arguments) do
128
+ {
129
+ label: label,
130
+ placeholder: placeholder
131
+ }
132
+ end
133
+
134
+ before do
135
+ aggregator_class.with_element(Magicka::Input, :my_input)
136
+
137
+ allow(renderer)
138
+ .to receive(:render)
139
+ .with(partial: template, locals: locals)
140
+ end
141
+
142
+ it 'renders an input' do
143
+ aggregator.my_input(field, arguments)
144
+
145
+ expect(renderer).to have_received(:render)
146
+ end
147
+
148
+ context 'when passing a custom model' do
149
+ let(:locals) do
150
+ {
151
+ field: field,
152
+ label: label,
153
+ ng_errors: 'my_custom_model.errors.field',
154
+ ng_model: 'my_custom_model.field',
155
+ placeholder: placeholder
156
+ }
157
+ end
158
+
159
+ it 'renders an input' do
160
+ aggregator.my_input(field, model: 'my_custom_model', **arguments)
161
+
162
+ expect(renderer).to have_received(:render)
163
+ end
164
+ end
165
+ end
166
+ end
167
+
168
+ context 'when seeting element class and template' do
169
+ it do
170
+ expect do
171
+ aggregator_class.with_element(Magicka::Input, template: template)
172
+ end
173
+ .to add_method(:input)
174
+ .to(aggregator)
175
+ end
176
+
177
+ context 'when built method is called' do
178
+ let(:template) { 'my_templates/my_input' }
179
+ let(:field) { :field }
180
+ let(:label) { 'Label' }
181
+ let(:placeholder) { 'Value' }
182
+
183
+ let(:locals) do
184
+ {
185
+ field: field,
186
+ label: label,
187
+ ng_errors: 'my_model.errors.field',
188
+ ng_model: 'my_model.field',
189
+ placeholder: placeholder
190
+ }
191
+ end
192
+
193
+ let(:arguments) do
194
+ {
195
+ label: label,
196
+ placeholder: placeholder
197
+ }
198
+ end
199
+
200
+ before do
201
+ aggregator_class.with_element(Magicka::Input, template: template)
202
+
203
+ allow(renderer)
204
+ .to receive(:render)
205
+ .with(partial: template, locals: locals)
206
+ end
207
+
208
+ it 'renders an input' do
209
+ aggregator.input(field, arguments)
210
+
211
+ expect(renderer).to have_received(:render)
212
+ end
213
+
214
+ context 'when passing a custom model' do
215
+ let(:locals) do
216
+ {
217
+ field: field,
218
+ label: label,
219
+ ng_errors: 'my_custom_model.errors.field',
220
+ ng_model: 'my_custom_model.field',
221
+ placeholder: placeholder
222
+ }
223
+ end
224
+
225
+ it 'renders an input' do
226
+ aggregator.input(field, model: 'my_custom_model', **arguments)
227
+
228
+ expect(renderer).to have_received(:render)
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ describe '#with_model' do
236
+ let(:expected_aggregator) do
237
+ aggregator_class.new(renderer, 'my_model.inner')
238
+ end
239
+
240
+ it do
241
+ aggregator.with_model(:inner) do |new_aggregator|
242
+ expect(new_aggregator).to eq(expected_aggregator)
243
+ end
244
+ end
245
+ end
246
+
247
+ describe '#only' do
248
+ let(:aggregator_class) do
249
+ Class.new(described_class) do
250
+ type :included
251
+ end
252
+ end
253
+
254
+ context 'when the type is included in the list' do
255
+ it 'executes the block' do
256
+ value = 0
257
+
258
+ aggregator.only(:not_included, :included, :other) { value += 1 }
259
+ expect(value).to eq(1)
260
+ end
261
+ end
262
+
263
+ context 'when the type is not included in the list' do
264
+ it 'does not execute the block' do
265
+ value = 0
266
+
267
+ aggregator.only(:not_included, :other) { value += 1 }
268
+ expect(value).to be_zero
269
+ end
270
+ end
271
+ end
272
+
273
+ describe '#except' do
274
+ let(:aggregator_class) do
275
+ Class.new(described_class) do
276
+ type :included
277
+ end
278
+ end
279
+
280
+ context 'when the type is included in the list' do
281
+ it 'does not execute the block' do
282
+ value = 0
283
+
284
+ aggregator.except(:not_included, :included, :other) { value += 1 }
285
+ expect(value).to be_zero
286
+ end
287
+ end
288
+
289
+ context 'when the type is not included in the list' do
290
+ it 'executes the block' do
291
+ value = 0
292
+
293
+ aggregator.except(:not_included, :other) { value += 1 }
294
+ expect(value).to eq(1)
295
+ end
296
+ end
297
+ end
298
+ end
@@ -16,6 +16,12 @@ describe Magicka::Display do
16
16
  .with(partial: template, locals: locals)
17
17
  end
18
18
 
19
+ describe '.type' do
20
+ it do
21
+ expect(described_class.type).to eq(:display)
22
+ end
23
+ end
24
+
19
25
  describe '#input' do
20
26
  let(:template) { 'templates/display/text' }
21
27
  let(:field) { :field }
@@ -166,4 +172,44 @@ describe Magicka::Display do
166
172
  end
167
173
  end
168
174
  end
175
+
176
+ describe '#only' do
177
+ context 'when the type is included in the list' do
178
+ it 'executes the block' do
179
+ value = 0
180
+
181
+ form.only(:not_included, :display, :other) { value += 1 }
182
+ expect(value).to eq(1)
183
+ end
184
+ end
185
+
186
+ context 'when the type is not included in the list' do
187
+ it 'does not execute the block' do
188
+ value = 0
189
+
190
+ form.only(:not_included, :other) { value += 1 }
191
+ expect(value).to be_zero
192
+ end
193
+ end
194
+ end
195
+
196
+ describe '#except' do
197
+ context 'when the type is included in the list' do
198
+ it 'does not execute the block' do
199
+ value = 0
200
+
201
+ form.except(:not_included, :display, :other) { value += 1 }
202
+ expect(value).to be_zero
203
+ end
204
+ end
205
+
206
+ context 'when the type is not included in the list' do
207
+ it 'executes the block' do
208
+ value = 0
209
+
210
+ form.except(:not_included, :other) { value += 1 }
211
+ expect(value).to eq(1)
212
+ end
213
+ end
214
+ end
169
215
  end
@@ -82,5 +82,46 @@ describe Magicka::Element do
82
82
  expect(renderer).to have_received(:render)
83
83
  end
84
84
  end
85
+
86
+ context 'when class has a template but is initialized with template' do
87
+ subject(:element) do
88
+ klass.new(renderer: renderer, template: custom_template)
89
+ end
90
+
91
+ let(:custom_template) { 'custom_folder/custom_template' }
92
+ let(:expected_template) { custom_template }
93
+
94
+ before do
95
+ klass.template(template)
96
+ end
97
+
98
+ it do
99
+ element.render
100
+
101
+ expect(renderer).to have_received(:render)
102
+ end
103
+ end
104
+
105
+ context 'when class has folder but is initialized with template' do
106
+ subject(:element) do
107
+ klass.new(renderer: renderer, template: custom_template)
108
+ end
109
+
110
+ let(:custom_template) { 'custom_folder/custom_template' }
111
+ let(:expected_template) { custom_template }
112
+
113
+ before do
114
+ klass.template_folder(folder)
115
+
116
+ method_builder.add_class_method(:name) { 'Magicka::MyElement' }
117
+ method_builder.build
118
+ end
119
+
120
+ it do
121
+ element.render
122
+
123
+ expect(renderer).to have_received(:render)
124
+ end
125
+ end
85
126
  end
86
127
  end
@@ -16,6 +16,12 @@ describe Magicka::Form do
16
16
  .with(partial: template, locals: locals)
17
17
  end
18
18
 
19
+ describe '.type' do
20
+ it do
21
+ expect(described_class.type).to eq(:form)
22
+ end
23
+ end
24
+
19
25
  describe '#input' do
20
26
  let(:template) { 'templates/forms/input' }
21
27
  let(:field) { :field }
@@ -180,4 +186,44 @@ describe Magicka::Form do
180
186
  end
181
187
  end
182
188
  end
189
+
190
+ describe '#only' do
191
+ context 'when the type is included in the list' do
192
+ it 'executes the block' do
193
+ value = 0
194
+
195
+ form.only(:not_included, :form, :other) { value += 1 }
196
+ expect(value).to eq(1)
197
+ end
198
+ end
199
+
200
+ context 'when the type is not included in the list' do
201
+ it 'does not execute the block' do
202
+ value = 0
203
+
204
+ form.only(:not_included, :other) { value += 1 }
205
+ expect(value).to be_zero
206
+ end
207
+ end
208
+ end
209
+
210
+ describe '#except' do
211
+ context 'when the type is included in the list' do
212
+ it 'does not execute the block' do
213
+ value = 0
214
+
215
+ form.except(:not_included, :form, :other) { value += 1 }
216
+ expect(value).to be_zero
217
+ end
218
+ end
219
+
220
+ context 'when the type is not included in the list' do
221
+ it 'executes the block' do
222
+ value = 0
223
+
224
+ form.except(:not_included, :other) { value += 1 }
225
+ expect(value).to eq(1)
226
+ end
227
+ end
228
+ end
183
229
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Magicka::Helper do
6
+ subject(:object) { klass.new }
7
+
8
+ let(:klass) do
9
+ Class.new do
10
+ include Magicka::Helper
11
+ end
12
+ end
13
+
14
+ let(:model) { 'model' }
15
+
16
+ describe '.with' do
17
+ let(:aggregator_class) do
18
+ Class.new(Magicka::Aggregator) do
19
+ def self.name
20
+ 'Magicka::MyClass'
21
+ end
22
+ end
23
+ end
24
+
25
+ it do
26
+ expect { described_class.with(aggregator_class) }
27
+ .to add_method('magicka_my_class')
28
+ .to(object)
29
+ end
30
+ end
31
+
32
+ describe '#magicka_display' do
33
+ it do
34
+ object.magicka_display(model) do |display|
35
+ expect(display).to be_a(Magicka::Display)
36
+ end
37
+ end
38
+
39
+ it 'populates the model' do
40
+ object.magicka_display(model) do |display|
41
+ expect(display.model).to eq(model)
42
+ end
43
+ end
44
+
45
+ it 'populates the renderer' do
46
+ object.magicka_display(model) do |display|
47
+ expect(display.send(:renderer)).to eq(object)
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#magicka_form' do
53
+ it do
54
+ object.magicka_form(model) do |form|
55
+ expect(form).to be_a(Magicka::Form)
56
+ end
57
+ end
58
+
59
+ it 'populates the model' do
60
+ object.magicka_form(model) do |form|
61
+ expect(form.model).to eq(model)
62
+ end
63
+ end
64
+
65
+ it 'populates the renderer' do
66
+ object.magicka_form(model) do |form|
67
+ expect(form.send(:renderer)).to eq(object)
68
+ end
69
+ end
70
+ end
71
+ end