Skip to content

Commit 5a0ded5

Browse files
committed
Add doc test for Path()'s switch mimic
1 parent 0db36ff commit 5a0ded5

File tree

1 file changed

+65
-26
lines changed

1 file changed

+65
-26
lines changed

test/docs/path_test.rb

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,44 +152,83 @@ def decide_type(ctx, model:, **)
152152

153153
it "allows multiple Path()s per step" do
154154
module D
155+
#:path-switch
155156
class Charge < Trailblazer::Activity::Railway
156-
include T.def_steps(:validate, :decide_type, :direct_debit, :finalize, :authorize, :charge)
157+
UseStripe = Class.new(Trailblazer::Activity::Signal)
158+
UseBraintree = Class.new(Trailblazer::Activity::Signal)
159+
Unknown = Class.new(Trailblazer::Activity::Signal)
157160

158-
failure_path = ->(*) { step :go }
159-
success_path = ->(*) { step :surf }
161+
StripeTrack = lambda do
162+
step :stripe
163+
step :stripe_invoice
164+
end
160165

161-
step :validate
162-
step :decide_type,
163-
Output(:failure) => Path(connect_to: Id(:finalize), &failure_path),
164-
Output(:success) => Path(connect_to: Id(:finalize), &success_path)
165-
step :direct_debit
166-
step :finalize
166+
BraintreeTrack = lambda do
167+
step :braintree
168+
step :braintree_invoice
169+
end
170+
171+
UnknownTrack = lambda do
172+
step :log_error
173+
end
174+
175+
step :gateway_type,
176+
Output(UseStripe, :stripe) => Path(end_id: "End.stripe", end_task: End(:stripe), &StripeTrack),
177+
Output(UseBraintree, :braintree) => Path(end_id: "End.braintree", end_task: End(:braintree), &BraintreeTrack),
178+
Output(Unknown, :unknown) => Path(end_id: "End.invalid", end_task: End(:invalid), &UnknownTrack)
179+
180+
def gateway_type(ctx, params:, **)
181+
case params[:gateway_type]
182+
when :stripe then UseStripe
183+
when :braintree then UseBraintree
184+
else Unknown
185+
end
186+
end
187+
188+
#~meths
189+
include T.def_steps(:stripe, :stripe_invoice, :braintree, :braintree_invoice, :log_error)
190+
#~meths-end
167191
end
168-
#:path-railway end
192+
#:path-switch-end
169193
end
170194

171-
assert_process_for D::Charge, :success, :failure, %{
195+
assert_process_for D::Charge, :success, :invalid, :braintree, :stripe, :failure, %{
172196
#<Start/:default>
173-
{Trailblazer::Activity::Right} => <*validate>
174-
<*validate>
175-
{Trailblazer::Activity::Left} => #<End/:failure>
176-
{Trailblazer::Activity::Right} => <*decide_type>
177-
<*decide_type>
178-
{Trailblazer::Activity::Left} => <*go>
179-
{Trailblazer::Activity::Right} => <*surf>
180-
<*go>
181-
{Trailblazer::Activity::Right} => <*finalize>
182-
<*surf>
183-
{Trailblazer::Activity::Right} => <*finalize>
184-
<*direct_debit>
185-
{Trailblazer::Activity::Left} => #<End/:failure>
186-
{Trailblazer::Activity::Right} => <*finalize>
187-
<*finalize>
197+
{Trailblazer::Activity::Right} => <*gateway_type>
198+
<*gateway_type>
188199
{Trailblazer::Activity::Left} => #<End/:failure>
189200
{Trailblazer::Activity::Right} => #<End/:success>
201+
{DocsPathTest::D::Charge::UseStripe} => <*stripe>
202+
{DocsPathTest::D::Charge::UseBraintree} => <*braintree>
203+
{DocsPathTest::D::Charge::Unknown} => <*log_error>
204+
<*stripe>
205+
{Trailblazer::Activity::Right} => <*stripe_invoice>
206+
<*stripe_invoice>
207+
{Trailblazer::Activity::Right} => #<End/:stripe>
208+
<*braintree>
209+
{Trailblazer::Activity::Right} => <*braintree_invoice>
210+
<*braintree_invoice>
211+
{Trailblazer::Activity::Right} => #<End/:braintree>
212+
<*log_error>
213+
{Trailblazer::Activity::Right} => #<End/:invalid>
190214
#<End/:success>
191215
216+
#<End/:invalid>
217+
218+
#<End/:braintree>
219+
220+
#<End/:stripe>
221+
192222
#<End/:failure>
193223
}
224+
225+
signal, (ctx, flow_options) = D::Charge.([ { seq: [], params: { gateway_type: :stripe } }, {} ])
226+
_(ctx[:seq]).must_equal([:stripe, :stripe_invoice])
227+
228+
signal, (ctx, flow_options) = D::Charge.([ { seq: [], params: { gateway_type: :braintree } }, {} ])
229+
_(ctx[:seq]).must_equal([:braintree, :braintree_invoice])
230+
231+
signal, (ctx, flow_options) = D::Charge.([ { seq: [], params: { gateway_type: :dummy } }, {} ])
232+
_(ctx[:seq]).must_equal([:log_error])
194233
end
195234
end

0 commit comments

Comments
 (0)