|
35 | 35 | type: 'Enum',
|
36 | 36 | enums: %w[a b c],
|
37 | 37 | }
|
| 38 | + multiple_enum = { |
| 39 | + field: 'multipleEnum', |
| 40 | + type: ['Enum'], |
| 41 | + enums: %w[a b c], |
| 42 | + } |
38 | 43 |
|
39 | 44 | action_definition = {
|
40 | 45 | name: 'my_action',
|
|
95 | 100 | }
|
96 | 101 | }
|
97 | 102 | }
|
| 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 | + |
98 | 118 | action = ForestLiana::Model::Action.new(action_definition)
|
99 | 119 | fail_action = ForestLiana::Model::Action.new(fail_action_definition)
|
100 | 120 | cheat_action = ForestLiana::Model::Action.new(cheat_action_definition)
|
101 | 121 | enums_action = ForestLiana::Model::Action.new(enums_action_definition)
|
| 122 | + multiple_enums_action = ForestLiana::Model::Action.new(multiple_enums_action_definition) |
102 | 123 | 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] |
104 | 125 |
|
105 | 126 | describe 'call /load' do
|
106 | 127 | params = {recordIds: [1], collectionName: 'Island'}
|
|
169 | 190 | expect(JSON.parse(response.body)).to eq({'fields' => [expected_foo.stringify_keys, expected_enum.stringify_keys]})
|
170 | 191 | end
|
171 | 192 |
|
| 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 |
172 | 220 | end
|
173 | 221 | end
|
174 | 222 | end
|
0 commit comments