# frozen_string_literal: true require "spec_helpers" describe Wayfarer::Callbacks, redis: true do before do stub_const("DummyJob", Class.new(ActiveJob::Base).include(Wayfarer::Base)) stub_const("DummyHandler", Class.new.include(Wayfarer::Handler)) end describe "job callbacks" do before do DummyJob.class_eval do route.path "alpha", to: :alpha route.path "beta", to: :beta def callbacks_fired @callbacks_fired ||= [] end before_fetch do |job| callbacks_fired << [:before_fetch, job] end before_fetch if: -> { %i[alpha beta].exclude?(action) } do |job| callbacks_fired << [:before_fetch_except_alpha_beta, job] end before_action do |job| callbacks_fired << [:before_action, job] end before_action if: -> { action == :alpha } do |job| callbacks_fired << [:before_action_only_alpha, job] end before_action if: -> { action == :beta } do |job| callbacks_fired << [:before_action_only_beta, job] end before_action if: -> { %i[alpha gamma].include?(action) } do |job| callbacks_fired << [:before_action_only_alpha_gamma, job] end before_action if: -> { action != :alpha } do |job| callbacks_fired << [:before_action_except_alpha, job] end before_action if: -> { %i[beta gamma].exclude?(action) } do |job| callbacks_fired << [:before_action_except_beta_gamma, job] end before_action :callback_a, if: -> { action == :alpha } before_action :callback_b before_action :callback_c, if: -> { action != :beta } def alpha; end def beta; end private def callback_a callbacks_fired << :before_action_callback_a end def callback_b callbacks_fired << :before_action_callback_b end def callback_c callbacks_fired << :before_action_callback_c end end end specify do job = DummyJob.new job.perform(build(:task, url: test_app_path("alpha"))) expect(job.callbacks_fired).to eq([ [:before_fetch, job], [:before_action, job], [:before_action_only_alpha, job], [:before_action_only_alpha_gamma, job], [:before_action_except_beta_gamma, job], :before_action_callback_a, :before_action_callback_b, :before_action_callback_c ]) job = DummyJob.new job.perform(build(:task, url: test_app_path("beta"))) expect(job.callbacks_fired).to eq([ [:before_fetch, job], [:before_action, job], [:before_action_only_beta, job], [:before_action_except_alpha, job], :before_action_callback_b ]) end end describe "job callbacks with handler" do before { stub_const("CallbacksFired", []) } before do DummyJob.class_eval do route.to [DummyHandler, :index] before_fetch do |job| CallbacksFired << [:before_fetch, job.class] end around_fetch do |job, block| CallbacksFired << [:around_fetch, job.class] block.call end after_fetch do |job| CallbacksFired << [:after_fetch, job.class] end before_action do |job| CallbacksFired << [:before_action, job.class] end around_action do |job, block| CallbacksFired << [:around_action, job.class] block.call end after_action do |job| CallbacksFired << [:after_action, job.class] end end DummyHandler.class_eval do before_action do |handler| CallbacksFired << [:before_action, handler.class] end around_action do |handler, block| CallbacksFired << [:around_action, handler.class] block.call end after_action do |handler| CallbacksFired << [:after_action, handler.class] end def index; end end end specify do DummyJob.crawl(test_app_path("/")) perform_enqueued_jobs expect(CallbacksFired).to eq([ [:before_fetch, DummyJob], [:around_fetch, DummyJob], [:after_fetch, DummyJob], [:before_action, DummyJob], [:around_action, DummyJob], [:before_action, DummyHandler], [:around_action, DummyHandler], [:after_action, DummyHandler], [:after_action, DummyJob] ]) end end describe "accessing user agent in before_fetch" do before do DummyJob.class_eval do include RSpec::Matchers route.to :index before_fetch do expect(user_agent).to be_a(Net::HTTP::Persistent) end def index; end end end specify do DummyJob.new.perform(build(:task, url: test_app_path("alpha"))) end end end