forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog_field_sorted_item_spec.rb
411 lines (330 loc) · 13.4 KB
/
dialog_field_sorted_item_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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
describe DialogFieldSortedItem do
describe "#initialize_with_values" do
let(:dialog_field) do
described_class.new(
:name => "potato_name",
:default_value => default_value,
:dynamic => true,
:load_values_on_init => load_values_on_init,
:show_refresh_button => show_refresh_button,
:values => [%w(test test), %w(test2 test2)]
)
end
let(:default_value) { "test2" }
context "when show_refresh_button is true" do
before(:each) do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end
let(:show_refresh_button) { true }
context "when load_values_on_init is true" do
let(:load_values_on_init) { true }
context "when the dialog values match the automate key name" do
let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} }
it "uses the values given" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq("dialog potato value")
end
end
context "when the dialog values match the dialog name" do
let(:dialog_values) { {"potato_name" => "potato value"} }
it "uses the values given" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq("potato value")
end
end
context "when the values from the passed in dialog values do not match either the automate or dialog name" do
let(:dialog_values) { {"not_potato_name" => "not potato value"} }
it "defaults to nil" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq(nil)
end
end
end
context "when load_values_on_init is false" do
let(:load_values_on_init) { false }
let(:dialog_values) { "potato" }
it "sets the raw values to the initial values" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.instance_variable_get(:@raw_values)).to eq([[nil, "<None>"]])
end
end
end
context "when show_refresh_button is false" do
let(:show_refresh_button) { false }
before(:each) do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end
context "when load_values_on_init is true" do
let(:load_values_on_init) { true }
context "when the dialog values match the automate key name" do
let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} }
it "uses the values given" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq("dialog potato value")
end
end
context "when the dialog values match the dialog name" do
let(:dialog_values) { {"potato_name" => "potato value"} }
it "uses the values given" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq("potato value")
end
end
context "when the values from the passed in dialog values do not match either the automate or dialog name" do
let(:dialog_values) { {"not_potato_name" => "not potato value"} }
context "when the default value does not exist in the list of options" do
let(:default_value) { "default value" }
it "defaults to nil" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq(nil)
end
end
end
end
context "when load_values_on_init is false" do
let(:load_values_on_init) { false }
context "when the dialog values match the automate key name" do
let(:dialog_values) { {"dialog_potato_name" => "dialog potato value"} }
it "uses the values given" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq("dialog potato value")
end
end
context "when the dialog values match the dialog name" do
let(:dialog_values) { {"potato_name" => "potato value"} }
it "uses the values given" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq("potato value")
end
end
context "when the values from the passed in dialog values do not match either the automate or dialog name" do
let(:dialog_values) { {"not_potato_name" => "not potato value"} }
it "defaults to nil" do
dialog_field.initialize_with_values(dialog_values)
expect(dialog_field.value).to eq(nil)
end
end
end
end
end
describe "#values" do
let(:dialog_field) do
described_class.new(
:default_value => default_value,
:dynamic => dynamic,
:required => required,
:sort_by => :none,
:values => values
)
end
let(:values) { [%w(test test), %w(abc abc)] }
let(:required) { false }
context "when the field is dynamic" do
let(:dynamic) { true }
before do
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return(values)
end
context "when the force_multi_value is set to false" do
let(:default_value) { "abc" }
before do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end
it "sets the default_value" do
dialog_field.values
expect(dialog_field.default_value).to eq("abc")
end
end
context "when the force_multi_value is set to true" do
let(:default_value) { "abc" }
before do
allow(dialog_field).to receive(:force_multi_value).and_return(true)
# FIXME (rblanco): this is a workaround for the tested class being
# DialogFieldSortedItem and not DialogFieldDropDownList as it is
# in real usecase
#
# The test should be moved to dialog_field_drop_down_list_spec.rb, as
# https://github.com/ManageIQ/manageiq/pull/17272#discussion_r180468970
# says.
allow(dialog_field).to receive(:default_value_included?).and_return(true)
end
it "sets the default_value" do
dialog_field.values
# while reproducing the BZ, the method
# `determine_selected_default_value` is called twice,
# not just once - could be a difference here
expect(dialog_field.default_value).to eq("[\"abc\"]")
end
end
context "when the default_value is included in the list of returned values" do
before do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end
let(:default_value) { "abc" }
it "sets the value to the default value" do
dialog_field.values
expect(dialog_field.default_value).to eq("abc")
end
end
context "when the default_value is not included in the list of returned values" do
before do
allow(dialog_field).to receive(:force_multi_value).and_return(false)
end
let(:default_value) { "123" }
it "sets the default value to the first value" do
dialog_field.values
expect(dialog_field.default_value).to eq("test")
end
it "sets the value to the default value" do
dialog_field.values
expect(dialog_field.value).to eq("test")
end
end
end
context "when the field is not dynamic" do
let(:dynamic) { false }
context "when the data type is not integer" do
context "when the default_value is set" do
let(:default_value) { "abc" }
context "when the field is required" do
let(:required) { true }
it "returns the values without the first option being a nil option" do
expect(dialog_field.values).to eq([%w(test test), %w(abc abc)])
end
end
context "when the field is not required" do
it "returns the values with the first option being a nil 'None' option" do
expect(dialog_field.values).to eq([[nil, "<None>"], %w(test test), %w(abc abc)])
end
end
end
end
context "when the data type is an integer" do
let(:data_type) { "integer" }
before do
dialog_field.data_type = data_type
end
context "when there is a default value that matches a value in the values list" do
let(:default_value) { "2" }
let(:values) { [%w(1 1), %w(2 2), %w(3 3)] }
it "sets the default value to the matching value" do
dialog_field.values
expect(dialog_field.default_value).to eq("2")
end
it "returns the values with the first option being a nil 'None' option" do
expect(dialog_field.values).to eq([[nil, "<None>"], %w(1 1), %w(2 2), %w(3 3)])
end
end
context "when there is a default value that does not match a value in the values list" do
let(:default_value) { "4" }
let(:values) { [%w(1 1), %w(2 2), %w(3 3)] }
it "sets the default value to nil" do
dialog_field.values
expect(dialog_field.default_value).to eq(nil)
end
it "returns the values with the first option being a nil 'None' option" do
expect(dialog_field.values).to eq([[nil, "<None>"], %w(1 1), %w(2 2), %w(3 3)])
end
end
context "when the default value is nil" do
let(:default_value) { nil }
context "when the field is required" do
let(:required) { true }
it "returns the values with the first option being a nil 'Choose' option" do
expect(dialog_field.values).to eq([[nil, "<Choose>"], %w(test test), %w(abc abc)])
end
end
context "when the field is not required" do
it "returns the values with the first option being a nil 'None' option" do
expect(dialog_field.values).to eq([[nil, "<None>"], %w(test test), %w(abc abc)])
end
end
end
end
end
end
describe "#get_default_value" do
let(:dialog_field) { described_class.new(:default_value => default_value, :values => values) }
let(:values) { [%w(value1 text1), %w(value2 text2)] }
context "when the default value is set to nil" do
let(:default_value) { nil }
it "returns nil as the default value" do
expect(dialog_field.get_default_value).to eq(nil)
end
end
context "when the default value exists" do
context "when the default value matches with a value in the list" do
let(:default_value) { "value2" }
it "returns the matched value" do
expect(dialog_field.get_default_value).to eq("value2")
end
end
context "when the default value does not match with a value in the list" do
let(:default_value) { "value3" }
it "returns nil" do
expect(dialog_field.get_default_value).to eq(nil)
end
end
end
end
describe "#script_error_values" do
let(:dialog_field) { described_class.new }
it "returns the script error values" do
expect(dialog_field.script_error_values).to eq([[nil, "<Script error>"]])
end
end
describe "#normalize_automate_values" do
let(:dialog_field) { DialogFieldRadioButton.new }
let(:automate_hash) do
{
"sort_by" => "none",
"sort_order" => "descending",
"data_type" => "datatype",
"default_value" => "default",
"description" => "description",
"required" => true,
"read_only" => true,
"values" => values
}
end
shared_examples_for "DialogFieldSortedItem#normalize_automate_values" do
before do
dialog_field.normalize_automate_values(automate_hash)
end
it "sets the sort_by" do
expect(dialog_field.sort_by).to eq(:none)
end
it "sets the sort_order" do
expect(dialog_field.sort_order).to eq(:descending)
end
it "sets the description" do
expect(dialog_field.description).to eq("description")
end
it "sets the data_type" do
expect(dialog_field.data_type).to eq("datatype")
end
it "sets the default_value" do
expect(dialog_field.default_value).to eq("default")
end
it "sets the required" do
expect(dialog_field.required).to be_truthy
end
it "sets the read_only" do
expect(dialog_field.read_only).to be_truthy
end
end
context "when the automate hash values passed in are blank" do
let(:values) { nil }
it_behaves_like "DialogFieldSortedItem#normalize_automate_values"
it "returns the initial values of the dialog field" do
expect(dialog_field.normalize_automate_values(automate_hash)).to eq([[nil, "<None>"]])
end
end
context "when the automate hash values passed in are not blank" do
let(:values) { {"lol" => "123"} }
it_behaves_like "DialogFieldSortedItem#normalize_automate_values"
it "normalizes the values to an array" do
expect(dialog_field.normalize_automate_values(automate_hash)).to eq([%w(lol 123)])
end
end
end
end