|
39 | 39 | } |
40 | 40 |
|
41 | 41 | describe 'hooks' do |
42 | | - foo = { |
43 | | - field: 'foo', |
44 | | - type: 'String', |
45 | | - default_value: nil, |
46 | | - enums: nil, |
47 | | - is_required: false, |
48 | | - is_read_only: false, |
49 | | - reference: nil, |
50 | | - description: nil, |
51 | | - widget: nil, |
52 | | - hook: 'on_foo_changed' |
53 | | - } |
54 | | - enum = { |
55 | | - field: 'enum', |
56 | | - type: 'Enum', |
57 | | - enums: %w[a b c], |
58 | | - } |
59 | | - multiple_enum = { |
60 | | - field: 'multipleEnum', |
61 | | - type: ['Enum'], |
62 | | - enums: %w[a b c], |
63 | | - } |
64 | | - |
65 | | - action_definition = { |
66 | | - name: 'my_action', |
67 | | - fields: [foo], |
68 | | - hooks: { |
69 | | - :load => -> (context) { |
70 | | - context[:fields] |
71 | | - }, |
72 | | - :change => { |
73 | | - 'on_foo_changed' => -> (context) { |
74 | | - foo = context[:fields].find{|field| field[:field] == 'foo'} |
75 | | - foo[:value] = 'baz' |
76 | | - context[:fields] |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - fail_action_definition = { |
82 | | - name: 'fail_action', |
83 | | - fields: [foo], |
84 | | - hooks: { |
85 | | - :load => -> (context) { |
86 | | - 1 |
87 | | - }, |
88 | | - :change => { |
89 | | - 'on_foo_changed' => -> (context) { |
90 | | - 1 |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - } |
95 | | - cheat_action_definition = { |
96 | | - name: 'cheat_action', |
97 | | - fields: [foo], |
98 | | - hooks: { |
99 | | - :load => -> (context) { |
100 | | - {} |
101 | | - }, |
102 | | - :change => { |
103 | | - 'on_foo_changed' => -> (context) { |
104 | | - context[:fields]['baz'] = foo.clone.update({field: 'baz'}) |
105 | | - context[:fields] |
106 | | - } |
107 | | - } |
108 | | - } |
109 | | - } |
110 | | - enums_action_definition = { |
111 | | - name: 'enums_action', |
112 | | - fields: [foo, enum], |
113 | | - hooks: { |
114 | | - :change => { |
115 | | - 'on_foo_changed' => -> (context) { |
116 | | - fields = context[:fields] |
117 | | - enum_field = fields.find{|field| field[:field] == 'enum'} |
118 | | - enum_field[:enums] = %w[c d e] |
119 | | - fields |
120 | | - } |
121 | | - } |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - multiple_enums_action_definition = { |
126 | | - name: 'multiple_enums_action', |
127 | | - fields: [foo, multiple_enum], |
128 | | - hooks: { |
129 | | - :change => { |
130 | | - 'on_foo_changed' => -> (context) { |
131 | | - fields = context[:fields] |
132 | | - enum_field = fields.find{|field| field[:field] == 'multipleEnum'} |
133 | | - enum_field[:enums] = %w[c d z] |
134 | | - fields |
135 | | - } |
136 | | - } |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - use_user_context_action_definition = { |
141 | | - name: 'use_user_context', |
142 | | - fields: [foo], |
143 | | - hooks: { |
144 | | - :load => -> (context) { |
145 | | - foo = context[:fields].find{|field| field[:field] == 'foo'} |
146 | | - foo[:value] = context[:user]['first_name'] |
147 | | - context[:fields] |
148 | | - } |
149 | | - } |
150 | | - } |
151 | | - |
152 | | - action = ForestLiana::Model::Action.new(action_definition) |
153 | | - fail_action = ForestLiana::Model::Action.new(fail_action_definition) |
154 | | - cheat_action = ForestLiana::Model::Action.new(cheat_action_definition) |
155 | | - enums_action = ForestLiana::Model::Action.new(enums_action_definition) |
156 | | - multiple_enums_action = ForestLiana::Model::Action.new(multiple_enums_action_definition) |
157 | | - use_user_context_action = ForestLiana::Model::Action.new(use_user_context_action_definition) |
158 | 42 | island = ForestLiana.apimap.find {|collection| collection.name.to_s == ForestLiana.name_for(Island)} |
159 | | - island.actions = [action, fail_action, cheat_action, enums_action, multiple_enums_action, use_user_context_action] |
160 | 43 |
|
161 | 44 | describe 'call /load' do |
162 | 45 | params = { |
|
167 | 50 |
|
168 | 51 | it 'should respond 200' do |
169 | 52 | post '/forest/actions/my_action/hooks/load', params: JSON.dump(params), headers: headers |
| 53 | + action = island.actions.select { |action| action.name == 'my_action' }.first |
| 54 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
170 | 55 | expect(response.status).to eq(200) |
171 | 56 | expect(JSON.parse(response.body)).to eq({'fields' => [foo.merge({:value => nil}).transform_keys { |key| key.to_s.camelize(:lower) }.stringify_keys]}) |
172 | 57 | end |
|
190 | 75 |
|
191 | 76 | it 'should return the first_name of the user who call the action' do |
192 | 77 | post '/forest/actions/use_user_context/hooks/load', params: JSON.dump(params), headers: headers |
| 78 | + action = island.actions.select { |action| action.name == 'use_user_context' }.first |
| 79 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
193 | 80 | expect(response.status).to eq(200) |
194 | 81 | expect(JSON.parse(response.body)).to eq({'fields' => [foo.merge({:value => 'Michael'}).transform_keys { |key| key.to_s.camelize(:lower) }.stringify_keys]}) |
195 | 82 | end |
196 | 83 | end |
197 | 84 |
|
198 | 85 | describe 'call /change' do |
199 | | - updated_foo = foo.clone.merge({:previousValue => nil, :value => 'bar'}) |
200 | | - params = { |
201 | | - data: { |
202 | | - attributes: { |
203 | | - ids: [1], |
204 | | - fields: [updated_foo], |
205 | | - collection_name: 'Island', |
206 | | - changed_field: 'foo', |
207 | | - is_read_only: true |
| 86 | + it 'should respond 200' do |
| 87 | + action = island.actions.select { |action| action.name == 'my_action' }.first |
| 88 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
| 89 | + updated_foo = foo.clone.merge({:previousValue => nil, :value => 'bar'}) |
| 90 | + params = { |
| 91 | + data: { |
| 92 | + attributes: { |
| 93 | + ids: [1], |
| 94 | + fields: [updated_foo], |
| 95 | + collection_name: 'Island', |
| 96 | + changed_field: 'foo', |
| 97 | + is_read_only: true |
| 98 | + } |
208 | 99 | } |
209 | 100 | } |
210 | | - } |
211 | 101 |
|
212 | | - it 'should respond 200' do |
213 | 102 | post '/forest/actions/my_action/hooks/change', params: JSON.dump(params), headers: headers |
214 | 103 | expect(response.status).to eq(200) |
215 | 104 | expected = updated_foo.clone.merge({:value => 'baz'}) |
|
226 | 115 | end |
227 | 116 |
|
228 | 117 | it 'should respond 500 with bad hook result type' do |
| 118 | + action = island.actions.select { |action| action.name == 'fail_action' }.first |
| 119 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
| 120 | + updated_foo = foo.clone.merge({:previousValue => nil, :value => 'bar'}) |
| 121 | + params = { |
| 122 | + data: { |
| 123 | + attributes: { |
| 124 | + ids: [1], |
| 125 | + fields: [updated_foo], |
| 126 | + collection_name: 'Island', |
| 127 | + changed_field: 'foo', |
| 128 | + is_read_only: true |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
229 | 133 | post '/forest/actions/fail_action/hooks/change', params: JSON.dump(params), headers: headers |
230 | 134 | expect(response.status).to eq(500) |
231 | 135 | expect(JSON.parse(response.body)).to eq({'error' => 'Error in smart action load hook: hook must return an array of fields'}) |
232 | 136 | end |
233 | 137 |
|
234 | 138 | it 'should reset value when enums has changed' do |
| 139 | + action = island.actions.select { |action| action.name == 'enums_action' }.first |
| 140 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
| 141 | + enum = action.fields.select { |field| field[:field] == 'enum' }.first |
| 142 | + updated_foo = foo.clone.merge({:previousValue => nil, :value => 'bar'}) |
235 | 143 | updated_enum = enum.clone.merge({:previousValue => nil, :value => 'a'}) # set value to a |
236 | 144 | p = { |
237 | 145 | data: { |
|
243 | 151 | } |
244 | 152 | } |
245 | 153 | } |
246 | | - post '/forest/actions/enums_action/hooks/change', params: JSON.dump(p), headers: headers |
| 154 | + |
| 155 | + post '/forest/custom/islands/enums_action/hooks/change', params: JSON.dump(p), headers: headers |
247 | 156 | expect(response.status).to eq(200) |
248 | 157 |
|
249 | 158 | expected_enum = updated_enum.clone.merge({ :enums => %w[c d e], :value => nil, :widgetEdit => nil}) |
|
258 | 167 | end |
259 | 168 |
|
260 | 169 | it 'should not reset value when every enum values are in the enums definition' do |
| 170 | + action = island.actions.select { |action| action.name == 'multiple_enums_action' }.first |
| 171 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
| 172 | + multiple_enum = action.fields.select { |field| field[:field] == 'multipleEnum' }.first |
261 | 173 | updated_multiple_enum = multiple_enum.clone.merge({:previousValue => nil, :value => %w[c]}) |
262 | 174 | p = { |
263 | 175 | data: { |
|
284 | 196 | end |
285 | 197 |
|
286 | 198 | it 'should reset value when one of the enum values is not in the enums definition' do |
| 199 | + action = island.actions.select { |action| action.name == 'multiple_enums_action' }.first |
| 200 | + foo = action.fields.select { |field| field[:field] == 'foo' }.first |
| 201 | + multiple_enum = action.fields.select { |field| field[:field] == 'multipleEnum' }.first |
287 | 202 | wrongly_updated_multiple_enum = multiple_enum.clone.merge({:previousValue => nil, :value => %w[a b]}) |
288 | 203 | p = { |
289 | 204 | data: { |
|
0 commit comments