Skip to content

Commit ef90105

Browse files
authored
fix(smart-action-hook): value injected to an enum field of type is now correctly handled (#414)
1 parent 0ab5381 commit ef90105

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

app/controllers/forest_liana/actions_controller.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ def handle_result(result, formatted_fields, action)
5555
# Apply result on fields (transform the object back to an array), preserve order.
5656
fields = action.fields.map do |field|
5757
updated_field = result[field[:field]]
58+
5859
# Reset `value` when not present in `enums` (which means `enums` has changed).
59-
if updated_field[:enums].is_a?(Array) && !updated_field[:enums].include?(updated_field[:value])
60-
updated_field[:value] = nil
60+
if updated_field[:enums].is_a?(Array)
61+
# `value` can be an array if the type of fields is `[x]`
62+
if updated_field[:type].is_a?(Array) && updated_field[:value].is_a?(Array) && !(updated_field[:value] - updated_field[:enums]).empty?
63+
updated_field[:value] = nil
64+
end
65+
66+
# `value` can be any other value
67+
if !updated_field[:type].is_a?(Array) && !updated_field[:enums].include?(updated_field[:value])
68+
updated_field[:value] = nil
69+
end
6170
end
71+
6272
updated_field
6373
end
6474

spec/requests/actions_controller_spec.rb

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
type: 'Enum',
3636
enums: %w[a b c],
3737
}
38+
multiple_enum = {
39+
field: 'multipleEnum',
40+
type: ['Enum'],
41+
enums: %w[a b c],
42+
}
3843

3944
action_definition = {
4045
name: 'my_action',
@@ -95,12 +100,28 @@
95100
}
96101
}
97102
}
103+
104+
multiple_enums_action_definition = {
105+
name: 'multiple_enums_action',
106+
fields: [foo, multiple_enum],
107+
hooks: {
108+
:change => {
109+
'foo' => -> (context) {
110+
fields = context[:fields]
111+
fields['multipleEnum'][:enums] = %w[c d z]
112+
return fields
113+
}
114+
}
115+
}
116+
}
117+
98118
action = ForestLiana::Model::Action.new(action_definition)
99119
fail_action = ForestLiana::Model::Action.new(fail_action_definition)
100120
cheat_action = ForestLiana::Model::Action.new(cheat_action_definition)
101121
enums_action = ForestLiana::Model::Action.new(enums_action_definition)
122+
multiple_enums_action = ForestLiana::Model::Action.new(multiple_enums_action_definition)
102123
island = ForestLiana.apimap.find {|collection| collection.name.to_s == ForestLiana.name_for(Island)}
103-
island.actions = [action, fail_action, cheat_action, enums_action]
124+
island.actions = [action, fail_action, cheat_action, enums_action, multiple_enums_action]
104125

105126
describe 'call /load' do
106127
params = {recordIds: [1], collectionName: 'Island'}
@@ -169,6 +190,33 @@
169190
expect(JSON.parse(response.body)).to eq({'fields' => [expected_foo.stringify_keys, expected_enum.stringify_keys]})
170191
end
171192

193+
it 'should not reset value when every enum values are in the enums definition' do
194+
updated_multiple_enum = multiple_enum.clone.merge({:previousValue => nil, :value => %w[c]})
195+
p = {recordIds: [1], fields: [foo, updated_multiple_enum], collectionName: 'Island', changedField: 'foo'}
196+
post '/forest/actions/multiple_enums_action/hooks/change', params: JSON.dump(p), headers: { 'CONTENT_TYPE' => 'application/json' }
197+
expect(response.status).to eq(200)
198+
199+
expected_multiple_enum = updated_multiple_enum.clone.merge({ :enums => %w[c d z], :widgetEdit => nil, :value => %w[c]})
200+
expected_multiple_enum.delete(:widget)
201+
expected_foo = foo.clone.merge({ :widgetEdit => nil})
202+
expected_foo.delete(:widget)
203+
204+
expect(JSON.parse(response.body)).to eq({'fields' => [expected_foo.stringify_keys, expected_multiple_enum.stringify_keys]})
205+
end
206+
207+
it 'should reset value when one of the enum values is not in the enums definition' do
208+
wrongly_updated_multiple_enum = multiple_enum.clone.merge({:previousValue => nil, :value => %w[a b]})
209+
p = {recordIds: [1], fields: [foo, wrongly_updated_multiple_enum], collectionName: 'Island', changedField: 'foo'}
210+
post '/forest/actions/multiple_enums_action/hooks/change', params: JSON.dump(p), headers: { 'CONTENT_TYPE' => 'application/json' }
211+
expect(response.status).to eq(200)
212+
213+
expected_multiple_enum = wrongly_updated_multiple_enum.clone.merge({ :enums => %w[c d z], :widgetEdit => nil, :value => nil })
214+
expected_multiple_enum.delete(:widget)
215+
expected_foo = foo.clone.merge({ :widgetEdit => nil})
216+
expected_foo.delete(:widget)
217+
218+
expect(JSON.parse(response.body)).to eq({'fields' => [expected_foo.stringify_keys, expected_multiple_enum.stringify_keys]})
219+
end
172220
end
173221
end
174222
end

0 commit comments

Comments
 (0)