11# rubocop: disable Style/GlobalVars
22
33if ENV [ "APPRAISAL_INITIALIZED" ]
4- RSpec . describe "before_validation , before_ & after_commit hooks" , type : :controller do
4+ RSpec . describe "after_graph_persist , before_ & after_commit hooks" , type : :controller do
55 class Callbacks
66 class << self
77 attr_accessor :fired , :in_transaction_during , :entities
@@ -27,7 +27,7 @@ def in_transaction?
2727 Callbacks . fired = { }
2828 Callbacks . in_transaction_during = { }
2929 Callbacks . entities = [ ]
30- $raise_on_before_validation = { employee : false }
30+ $raise_on_after_graph_persist = { employee : false }
3131 $raise_on_before_commit = { employee : true }
3232 end
3333
@@ -76,9 +76,9 @@ class EmployeeResource < ApplicationResource
7676
7777 attribute :first_name , :string
7878
79- before_validation do |employee |
80- Callbacks . add ( :before_validation , employee )
81- if $raise_on_before_validation [ :employee ]
79+ after_graph_persist do |employee |
80+ Callbacks . add ( :after_graph_persist , employee )
81+ if $raise_on_after_graph_persist [ :employee ]
8282 raise "rollitback"
8383 end
8484 end
@@ -226,7 +226,7 @@ def json
226226 expect {
227227 post :create , params : payload
228228 } . to raise_error ( "whoops" )
229- expect ( Callbacks . entities ) . to be_empty
229+ expect ( Callbacks . entities ) . to include ( :after_graph_persist )
230230 end
231231 end
232232
@@ -238,7 +238,7 @@ def json
238238 it "fires all before and after_commit hooks" do
239239 post :create , params : payload
240240 expect ( Callbacks . entities ) . to eq ( [
241- :before_validation ,
241+ :after_graph_persist ,
242242 :before_create ,
243243 :stacked_before_create ,
244244 :employee_after_create ,
@@ -247,20 +247,20 @@ def json
247247 end
248248 end
249249
250- context "when an error is raised before_validation " do
250+ context "when an error is raised after_graph_persist " do
251251 before do
252- $raise_on_before_validation = { employee : true }
252+ $raise_on_after_graph_persist = { employee : true }
253253 end
254254
255255 it "does not run before_commit callbacks" do
256256 expect_any_instance_of ( Graphiti ::Util ::ValidationResponse )
257- . to receive ( :validate! )
257+ . to_not receive ( :validate! )
258258 expect {
259259 post :create , params : payload
260260 } . to raise_error ( "rollitback" )
261261 expect ( Employee . count ) . to be_zero
262262 expect ( Callbacks . entities . length ) . to eq ( 1 )
263- expect ( Callbacks . fired [ :before_validation ] ) . to be_a ( Employee )
263+ expect ( Callbacks . fired [ :after_graph_persist ] ) . to be_a ( Employee )
264264 end
265265 end
266266 end
@@ -307,7 +307,7 @@ def json
307307 post :create , params : payload
308308
309309 expect ( Callbacks . entities ) . to eq ( [
310- :before_validation ,
310+ :after_graph_persist ,
311311 :before_create ,
312312 :stacked_before_create ,
313313 :before_position ,
@@ -337,7 +337,7 @@ def json
337337 expect ( Callbacks . fired [ :employee_after_create_eval_test ] ) . to be_a ( IntegrationHooks ::EmployeeResource )
338338 end
339339
340- it "can access children resources from before_validation " do
340+ it "can access children resources from after_graph_persist " do
341341 post :create , params : payload
342342 expect ( Callbacks . fired [ :employee_after_create ] . positions . length ) . to eq ( 1 )
343343 expect ( Callbacks . fired [ :employee_after_create ] . positions [ 0 ] ) . to be_a ( Position )
0 commit comments