/
dialog_field_sorted_item_spec.rb
334 lines (265 loc) · 10.7 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
describe DialogFieldSortedItem do
describe "#initialize_with_values" do
let(:dialog_field) do
described_class.new(
:name => "potato_name",
:default_value => "test2",
:dynamic => true,
:load_values_on_init => load_values_on_init,
:show_refresh_button => show_refresh_button,
:values => [%w(test test), %w(test2 test2)]
)
end
context "when show_refresh_button is true" do
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 }
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 default_value is included in the list of returned values" do
let(:default_value) { "abc" }
it "sets the default value" do
dialog_field.values
expect(dialog_field.default_value).to eq("abc")
end
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
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 }
let(:default_value) { "abc" }
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
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
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",
"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 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