require 'assert' require 'mr/after_commit/record_procs_methods' module MR::AfterCommit::RecordProcsMethods class UnitTests < Assert::Context desc "MR::AfterCommit::RecordProcsMethods" subject{ MR::AfterCommit::RecordProcsMethods } should "know its valid callback types" do exp = [ :create, :update, :save, :destroy ] assert_equal exp, VALID_CALLBACK_TYPES end should "know its default callback type" do assert_equal :save, DEFAULT_CALLBACK_TYPE end end class MixinAndInitTests < UnitTests desc "when mixed in and init" setup do record_class = Class.new do include MR::AfterCommit::RecordProcsMethods end @record = record_class.new end subject{ @record } should have_imeths :after_commit_procs should have_imeths :add_after_commit_proc, :prepend_after_commit_proc should have_imeths :clear_after_commit_procs should "not have any after commit procs by default" do assert_equal [], subject.after_commit_procs assert_equal [], subject.after_commit_procs(VALID_CALLBACK_TYPES.sample) end should "allow adding after commit procs" do callback_type = VALID_CALLBACK_TYPES.sample callback_proc = proc{ Factory.string } subject.add_after_commit_proc(callback_type, &callback_proc) assert_equal 1, subject.after_commit_procs.size assert_includes callback_proc, subject.after_commit_procs assert_equal 1, subject.after_commit_procs(callback_type).size assert_includes callback_proc, subject.after_commit_procs(callback_type) end should "add the after commit proc to save by default" do callback_proc = proc{ Factory.string } subject.add_after_commit_proc(&callback_proc) assert_equal 1, subject.after_commit_procs.size assert_includes callback_proc, subject.after_commit_procs assert_equal 1, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE).size assert_includes callback_proc, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE) end should "append after commit procs when adding them" do callback_type = VALID_CALLBACK_TYPES.sample callbacks_procs = Factory.integer(3).times.map do cp = proc{ Factory.string } subject.add_after_commit_proc(callback_type, &cp) cp end assert_equal callbacks_procs, subject.after_commit_procs(callback_type) end should "allow prepending after commit procs" do callback_type = VALID_CALLBACK_TYPES.sample callbacks_procs = Factory.integer(3).times.map do cp = proc{ Factory.string } subject.prepend_after_commit_proc(callback_type, &cp) cp end exp = callbacks_procs.reverse assert_equal exp, subject.after_commit_procs(callback_type) end should "prepend after commit procs to save by default" do callback_proc = proc{ Factory.string } subject.prepend_after_commit_proc(&callback_proc) assert_equal 1, subject.after_commit_procs.size assert_includes callback_proc, subject.after_commit_procs assert_equal 1, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE).size assert_includes callback_proc, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE) end should "allow getting after commit procs" do callbacks_hash = VALID_CALLBACK_TYPES.inject({}) do |h, ct| cps = Factory.integer(3).times.map do cp = proc{ Factory.string } subject.add_after_commit_proc(ct, &cp) cp end h.merge!(ct => cps) end callback_type, exp = callbacks_hash.to_a.sample assert_equal exp, subject.after_commit_procs(callback_type) callback_types = callbacks_hash.keys.shuffle[0, 2] exp = callback_types.map{ |ct| callbacks_hash[ct] }.flatten assert_equal exp, subject.after_commit_procs(*callback_types) end should "allow clearing after commit procs" do VALID_CALLBACK_TYPES.each do |ct| Factory.integer(3).times.each do subject.add_after_commit_proc(ct){ Factory.string } end end callback_types = VALID_CALLBACK_TYPES.shuffle[0, 2] assert_not_empty subject.after_commit_procs(*callback_types) subject.clear_after_commit_procs(*callback_types) assert_empty subject.after_commit_procs(*callback_types) assert_not_empty subject.after_commit_procs subject.clear_after_commit_procs assert_empty subject.after_commit_procs end should "provide a helper for calling after commit procs" do callback_type = VALID_CALLBACK_TYPES.sample callback_called = false callback_proc = proc{ callback_called = true } subject.add_after_commit_proc(callback_type, &callback_proc) subject.instance_eval{ mr_after_commit_call_procs(callback_type) } assert_true callback_called assert_equal [], subject.after_commit_procs(callback_type) assert_equal [callback_proc], subject.called_after_commit_procs(callback_type) end should "allow checking which after commit procs have been called" do callback_type = VALID_CALLBACK_TYPES.sample callback_proc = proc{ Factory.string } subject.add_after_commit_proc(callback_type, &callback_proc) assert_equal [], subject.called_after_commit_procs assert_equal [], subject.called_after_commit_procs(callback_type) subject.instance_eval{ mr_after_commit_call_procs(callback_type) } assert_equal [callback_proc], subject.called_after_commit_procs assert_equal [callback_proc], subject.called_after_commit_procs(callback_type) end should "raise an argument error when using an invalid callback type" do assert_raises(ArgumentError) do subject.add_after_commit_proc(Factory.string.to_sym){ Factory.string } end assert_raises(ArgumentError) do subject.prepend_after_commit_proc(Factory.string.to_sym){ Factory.string } end assert_raises(ArgumentError) do subject.after_commit_procs(Factory.string.to_sym) end assert_raises(ArgumentError) do subject.called_after_commit_procs(Factory.string.to_sym) end end end end