diff --git a/lib/trailblazer/test/operation/helper.rb b/lib/trailblazer/test/operation/helper.rb index 0fbbd2d..b372778 100644 --- a/lib/trailblazer/test/operation/helper.rb +++ b/lib/trailblazer/test/operation/helper.rb @@ -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 diff --git a/test/docs/helpers/mocking_test.rb b/test/docs/helpers/mocking_test.rb index ad55cf0..f416d6b 100644 --- a/test/docs/helpers/mocking_test.rb +++ b/test/docs/helpers/mocking_test.rb @@ -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) @@ -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 @@ -56,7 +61,7 @@ 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 @@ -64,14 +69,14 @@ class ExternalApi < Trailblazer::Activity::FastTrack 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 @@ -79,16 +84,33 @@ class ExternalApi < Trailblazer::Activity::FastTrack 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