/
edit_builder_spec.rb
322 lines (273 loc) · 10.4 KB
/
edit_builder_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
313
314
315
316
317
318
319
320
321
322
require "rails_helper"
describe Releaf::Builders::EditBuilder, type: :class do
class TranslationsEditBuilderTestHelper < ActionView::Base
include Releaf::ApplicationHelper
include Releaf::ButtonHelper
include FontAwesome::Rails::IconHelper
def protect_against_forgery?
true
end
def form_authenticity_token(_)
"xxx"
end
def request_forgery_protection_token
"yyy"
end
end
let(:template){ TranslationsEditBuilderTestHelper.new(ActionView::LookupContext.new(nil), {}, nil) }
let(:subject){ described_class.new(template) }
let(:controller){ Releaf::ActionController.new }
let(:resource){ Book.new }
before do
allow(template).to receive(:controller).and_return(controller)
allow(controller).to receive(:action_name).and_return(:edit)
allow(subject).to receive(:resource).and_return(resource)
end
describe "#section_content" do
before do
allow(subject).to receive(:section_attributes).and_return(a: "b")
allow(subject).to receive(:form_options).and_return(url: "xxx", builder: Releaf::Builders::FormBuilder)
allow(subject).to receive(:index_path_preserver).and_return("_index_path_")
allow(subject).to receive(:section_blocks).and_return(["_section_","_blocks_"])
end
it "returns section with index url preserver and section blocks" do
expect(subject.section_content).to eq('<form class="new_book" id="new_book" action="xxx" accept-charset="UTF-8" method="post"><input type="hidden" name="yyy" value="xxx" />_index_path__section__blocks_</form>')
end
it "assigns form instance to builder" do
expect{ subject.section_content }.to change{ subject.form }.from(nil)
expect(subject.form).to be_instance_of Releaf::Builders::FormBuilder
end
end
describe "#index_path_preserver" do
it "returns hidden index url input" do
allow(subject).to receive(:params).and_return(index_path: "?asd=23&lf=dd")
result = '<input type="hidden" name="index_path" id="index_path" value="?asd=23&lf=dd" />'
expect(subject.index_path_preserver).to eq(result)
end
end
describe "#section_body_blocks" do
it "returns array with error notices and form fields" do
allow(subject).to receive(:error_notices).and_return("err")
allow(subject).to receive(:form_fields).and_return("fields")
expect(subject.section_body_blocks).to eq(["err", "fields"])
end
end
describe "#error_notices" do
before do
allow(subject).to receive(:error_notices_header).and_return("<error_notice_header />".html_safe)
end
context "when errors exists" do
it "returns errors block" do
resource.valid?
expect(subject.error_notices).to match_html(%Q[
<div class="form-error-box">
<error_notice_header />
<ul>
<li class="error">Title can't be blank</li>
</ul>
</div>
])
end
end
context "when no errors present" do
it "returns nil" do
expect(subject.error_notices).to be nil
end
end
end
describe "#error_notices_header" do
it "returns validation errors notices header" do
resource.valid?
expect(subject.error_notices_header).to eq('<strong>1 validation error occured:</strong>')
resource.title = "xx"
resource.valid?
expect(subject.error_notices_header).to eq('<strong>0 validation errors occured:</strong>')
end
end
describe "#footer_primary_tools" do
before do
allow(subject).to receive(:save_button).and_return("_svbtn_")
allow(subject).to receive(:save_and_create_another_button).and_return("_svacrbtn_")
end
context "when create_another is availble" do
it "returns an array with save_and_create_another and save buttons" do
allow(subject).to receive(:create_another_available?).and_return true
expect(subject.footer_primary_tools).to eq(["_svacrbtn_", "_svbtn_"])
end
end
context "when create_another is not available" do
it "returns an array with save button" do
allow(subject).to receive(:create_another_available?).and_return false
expect(subject.footer_primary_tools).to eq(["_svbtn_"])
end
end
end
describe "#create_another_available?" do
context "when editing an existing record" do
let(:resource){ FactoryBot.create(:book) }
context "when controller has create_another feature enabled" do
it "returns false" do
expect(subject.create_another_available?).to be false
end
end
context "when controller has create_another feature disabled" do
it "returns false" do
expect(subject.create_another_available?).to be false
end
end
end
context "when creating a new record" do
context "when controller has create_another feature enabled" do
it "returns true" do
expect(subject.create_another_available?).to be true
end
end
context "when controller has create_another feature disabled" do
it "returns false" do
allow(controller).to receive(:feature_available?).with(:create_another).and_return false
expect(subject.create_another_available?).to be false
end
end
end
context "when resource is not present" do
let(:resource){ nil }
it "returns false" do
expect(subject.create_another_available?).to be false
end
end
end
describe "#save_and_create_another_button" do
it "returns save and create button" do
allow(template).to receive(:fa_icon).with("plus").and_return('<plus_icon />'.html_safe)
allow(subject).to receive(:t).with("Save and create another").and_return("Save and ccrr")
expect(subject.save_and_create_another_button).to match_html(%Q[
<button class="button with-icon secondary" title="Save and ccrr" type="submit" autocomplete="off" name="after_save" value="create_another" data-type="ok" data-disable="true">
<plus_icon />
Save and ccrr
</button>
])
end
end
describe "#form_options" do
it "returns form options" do
allow(subject).to receive(:form_builder_class).and_return("CustomFormBuilderClassHere")
allow(subject).to receive(:form_url).and_return("/some-url-here")
allow(subject).to receive(:form_attributes).and_return(some: "options_here")
allow(subject).to receive(:resource_name).and_return(:author)
options = {
builder: "CustomFormBuilderClassHere",
as: :author,
url: "/some-url-here",
html: {some: "options_here"}
}
expect(subject.form_options).to eq(options)
end
end
describe "#form_url" do
it "returns form url built from form action and resource id" do
resource.id = 23
allow(subject).to receive(:form_action).and_return("upd")
allow(subject).to receive(:url_for).with(action: "upd", id: 23).and_return("/res/new")
expect(subject.form_url).to eq("/res/new")
end
end
describe "#form_action" do
context "when new resource" do
it "returns `create`" do
allow(resource).to receive(:new_record?).and_return(true)
expect(subject.form_action).to eq("create")
end
end
context "when persisted resource" do
it "returns `update`" do
allow(resource).to receive(:new_record?).and_return(false)
expect(subject.form_action).to eq("update")
end
end
end
describe "#resource_name" do
it "returns `:resource`" do
expect(subject.resource_name).to eq(:resource)
end
end
describe "#form_url" do
it "returns form builder class" do
allow(subject).to receive(:builder_class).with(:form).and_return("x")
expect(subject.form_builder_class).to eq("x")
end
end
describe "#form_identifier" do
before do
allow(subject).to receive(:resource_name).and_return(:book)
end
context "when resource has persistance check method and it's persisted" do
it "returns resource name prefixed with `edit-`" do
allow(resource).to receive(:persisted?).and_return(true)
expect(subject.form_identifier).to eq("edit-book")
end
end
context "when resource has persistance check method and it's not persisted" do
it "returns resource name prefixed with `new-`" do
allow(resource).to receive(:persisted?).and_return(false)
expect(subject.form_identifier).to eq("new-book")
end
end
context "when resource has no persistance check method" do
it "returns resource name prefixed with `update-`" do
allow(subject).to receive(:resource).and_return(String.new)
expect(subject.form_identifier).to eq("edit-book")
end
end
end
describe "#form_classes" do
before do
allow(subject).to receive(:form_identifier).and_return("xx")
end
it "returns array with form identifier" do
expect(subject.form_classes).to eq(["xx"])
end
context "when object has any errors" do
it "adds has-error class to returned array" do
resource.title = nil
resource.valid?
expect(subject.form_classes).to eq(["xx", "has-error"])
end
end
end
describe "#form_attributes" do
it "returns form attributes" do
allow(subject).to receive(:form_identifier).and_return("xx")
allow(subject).to receive(:form_classes).and_return(["a", "b"])
attributes = {
multipart: true,
novalidate: "",
class: ["a", "b"],
id: "xx",
data: {
"remote"=>true,
"remote-validation"=>true,
"type"=>:json
}
}
expect(subject.form_attributes).to eq(attributes)
end
end
describe "#save_button" do
it "returns save button" do
allow(subject).to receive(:button)
.with("to_list", "check", {class: "primary", data: {type: "ok", disable: true}, type: "submit"})
.and_return("_btn_")
allow(subject).to receive(:t).with("Save").and_return("to_list")
expect(subject.save_button).to eq("_btn_")
end
end
describe "#form_fields" do
it "returns form `releaf_fields` output for form `field_names` casted to array" do
form = Releaf::Builders::FormBuilder.new(:book, Book.new, template, {})
subject.form = form
allow(form).to receive(:field_names).and_return({a: 1, b: 2})
allow(form).to receive(:releaf_fields).with([[:a, 1], [:b, 2]]).and_return(:x)
expect(subject.form_fields).to eq(:x)
end
end
end