Skip to content

Commit

Permalink
Refactor mock_step and deprecate subprocess kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshjain999 committed May 24, 2024
1 parent df0dce6 commit 2236ffa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
18 changes: 9 additions & 9 deletions lib/trailblazer/test/operation/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ def factory(operation_class, **args, &block)
call!(operation_class, args.merge(raise_on_failure: true), &block)
end

def mock_step(operation_class, id:, subprocess: nil, subprocess_path: nil)
raise ArgumentError, "Missing block: `mock_step` requires a block." unless block_given?
def mock_step(operation_class, id:, subprocess: nil, subprocess_path: nil, &block)
raise ArgumentError, "Missing block: `mock_step` requires a block." if block.nil?

block = ->(ctx, **) { yield(ctx) }
override = ->(*) { step block, replace: id, id: id }

# If deeply nested step needs to be mocked, use the `patch` provided by Subprocess
return Class.new(operation_class, &override) if subprocess_path.nil?

# Remove below check in 1.0.0
if subprocess
return Class.new(operation_class) do
patch = { subprocess_path => ->() { step block, replace: id, id: id } }
step Subprocess(subprocess, patch: patch), replace: subprocess, id: subprocess
end
subprocess_path = [subprocess, *subprocess_path]
Trailblazer::Activity::Deprecate.warn caller_locations[0], ":subprocess is deprecated and will be removed in 1.0.0. Pass `subprocess_path: #{subprocess_path}` instead."
end

Class.new(operation_class) { step block, replace: id, id: id }
Trailblazer::Activity::DSL::Linear::Patch.call(operation_class, subprocess_path, override)
end

# @private
Expand Down
52 changes: 37 additions & 15 deletions test/docs/helpers/mocking_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
require "test_helper"

class DocsMockingTest < MiniTest::Spec
class DocsMockingTest < Minitest::Spec
include Trailblazer::Test::Assertions
include Trailblazer::Test::Operation::Assertions
include Trailblazer::Test::Operation::Helper

User = Struct.new(:name)

#:mock-show-operation
class Show < Trailblazer::Activity::FastTrack
class Complexity < Trailblazer::Activity::FastTrack
class ExternalApi < Trailblazer::Activity::FastTrack
class Show < Trailblazer::Operation
class Complexity < Trailblazer::Operation
class ExternalApi < Trailblazer::Operation
step :make_call
#~method
include Testing.def_steps(:make_call)
Expand All @@ -26,19 +32,18 @@ class ExternalApi < Trailblazer::Activity::FastTrack
end
#:mock-show-operation end

include Trailblazer::Test::Operation::Helper

describe "#mock_step" do
let(:default_params) { { seq: [] } }
let(:operation) { Show }
let(:default_ctx) { { seq: [] } }
let(:expected_attrs) { {} }

#:simple-mock-step
it "mocks loading user" do
new_activity = mock_step(Show, id: :load_user) do |ctx|
ctx[:user] = Struct.new(:name).new('Mocky')
new_activity = mock_step(Show, id: :load_user) do |ctx, **|
ctx[:user] = User.new('Mocky')
end

assert_pass new_activity, default_params, {} do |(signal, (ctx, flow_options))|
assert_pass({}, {}, operation: new_activity) do |ctx|
assert_equal ctx[:user].name, 'Mocky'

#~method
Expand All @@ -56,39 +61,56 @@ class ExternalApi < Trailblazer::Activity::FastTrack
#:mock-subprocess end

#~method
assert_pass new_activity, default_params, {} do |(signal, (ctx, flow_options))|
assert_pass({}, {}, operation: new_activity) do |ctx|
assert_equal ctx[:seq], [:load_user]
end
#~method end
end

it "mocks subprocess step" do
#:mock-subprocess-step
new_activity = mock_step(Show, id: :some_complex_task, subprocess: Show::Complexity) do
new_activity = mock_step(Show, id: :some_complex_task, subprocess_path: [Show::Complexity]) do
# Mock only single step from nested activity to do nothing
true
end
#:mock-subprocess-step end

#~method
assert_pass new_activity, default_params, {} do |(signal, (ctx, flow_options))|
assert_pass({}, {}, operation: new_activity) do |ctx|
assert_equal ctx[:seq], [:load_user, :make_call]
end
#~method end
end

it "mocks nested subprocess step" do
#:mock-nested-subprocess-step
new_activity = mock_step(Show, id: :make_call, subprocess: Show::Complexity, subprocess_path: [Show::Complexity::ExternalApi]) do
new_activity = mock_step(Show, id: :make_call, subprocess_path: [Show::Complexity, Show::Complexity::ExternalApi]) do
# Some JSON response
{ name: 'Mocky' }
end
#:mock-nested-subprocess-step end

#~method
assert_pass new_activity, default_params, {} do |(signal, (ctx, flow_options))|
assert_pass({}, {}, operation: new_activity) do |ctx|
assert_equal ctx[:seq], [:load_user, :some_complex_task]
end
#~method end
end

it "shows warning for deprecated `subprocess`" do
#~method
_, warning = capture_io do
new_activity = mock_step(Show, id: :make_call, subprocess: Show::Complexity, subprocess_path: [Show::Complexity::ExternalApi]) do
{ name: 'Mocky' }
end

assert_pass({}, {}, operation: new_activity) do |ctx|
assert_equal ctx[:seq], [:load_user, :some_complex_task]
end
end

assert_match ":subprocess is deprecated and will be removed in 1.0.0. Pass `subprocess_path: #{[Show::Complexity, Show::Complexity::ExternalApi]}` instead.", warning
#~method end
end
end
end

0 comments on commit 2236ffa

Please sign in to comment.