Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor mock_step and deprecate subprocess kwarg #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we name it :path instead of :subprocess_path (feels clumsy)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah, but I thought path resonates more with Activity::Path and we allow Railway and FastTrack too 😄

Having subprocess in it makes intent clear, how about just subprocess or subprocesses ?

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
50 changes: 36 additions & 14 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')
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|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the {}, {} arguments here, I wonder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I want to compare ctx[:user] object here in the 2nd {} of output here, but I guess it's not possible to compare it's not primitive type!

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
Loading