Class StepMockAbstract
Hierarchy (View Summary)
- Step
- StepMock
Index
Properties
Readonlyinputs
The inputs it was built with, in the order its constructor requests
them: each input's value, or an Option of it for an optional input.
Methods
Protectedfail
-
The outcome of an attempt that could not do its work, for a reason, or only its code. The reason's details are captured now, so changing them afterwards changes nothing; details that are not a value abort the journey with
not a valueonce the attempt ends.import assert from "node:assert/strict";
import { input, type Outcome, Step, step } from "itinera/step";
@step()
class Charge extends Step {
constructor(@input("amount") private readonly amount: number) {
super();
}
override run(): Outcome {
if (this.amount === 0) {
return this.skip();
}
if (this.amount < 0) {
return this.skipBecause("refunds are not charged");
}
if (this.amount > 100) {
return this.fail({
code: "declined",
message: "over the limit",
details: { limit: 100 },
});
}
return this.succeed();
}
}
assert.deepEqual(new Charge(40).run(), { kind: "success" });
assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
assert.deepEqual(new Charge(-5).run(), {
kind: "skipped",
reason: { code: "refunds are not charged" },
});
assert.deepEqual(new Charge(140).run(), {
kind: "failure",
reason: {
code: "declined",
message: "over the limit",
details: { limit: 100 },
},
});Type Parameters
- T
Parameters
- reason: GivenReason<T>
Returns Fail
run
Protectedskip
-
The outcome of a step that decided not to do its work: it does not run again in the journey, and what it contributed is discarded.
import assert from "node:assert/strict";
import { input, type Outcome, Step, step } from "itinera/step";
@step()
class Charge extends Step {
constructor(@input("amount") private readonly amount: number) {
super();
}
override run(): Outcome {
if (this.amount === 0) {
return this.skip();
}
if (this.amount < 0) {
return this.skipBecause("refunds are not charged");
}
if (this.amount > 100) {
return this.fail({
code: "declined",
message: "over the limit",
details: { limit: 100 },
});
}
return this.succeed();
}
}
assert.deepEqual(new Charge(40).run(), { kind: "success" });
assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
assert.deepEqual(new Charge(-5).run(), {
kind: "skipped",
reason: { code: "refunds are not charged" },
});
assert.deepEqual(new Charge(140).run(), {
kind: "failure",
reason: {
code: "declined",
message: "over the limit",
details: { limit: 100 },
},
});Returns Skip
Protectedskip Because
-
The outcome of a step that decided not to do its work, saying why, with a reason or only its code, captured as Step.fail captures it.
import assert from "node:assert/strict";
import { input, type Outcome, Step, step } from "itinera/step";
@step()
class Charge extends Step {
constructor(@input("amount") private readonly amount: number) {
super();
}
override run(): Outcome {
if (this.amount === 0) {
return this.skip();
}
if (this.amount < 0) {
return this.skipBecause("refunds are not charged");
}
if (this.amount > 100) {
return this.fail({
code: "declined",
message: "over the limit",
details: { limit: 100 },
});
}
return this.succeed();
}
}
assert.deepEqual(new Charge(40).run(), { kind: "success" });
assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
assert.deepEqual(new Charge(-5).run(), {
kind: "skipped",
reason: { code: "refunds are not charged" },
});
assert.deepEqual(new Charge(140).run(), {
kind: "failure",
reason: {
code: "declined",
message: "over the limit",
details: { limit: 100 },
},
});Type Parameters
- T
Parameters
- reason: GivenReason<T>
Returns Skip
Protectedsucceed
-
The outcome of an attempt that did its work.
Returns Outcome
Staticbuilder
-
builder<C extends StepClass>(
this: WithoutStaticData<C, typeof Step>,
name: string,
): StepBuilder<C>Starts putting a step of this class together for a workflow's builder, named
name, asCharge.builder("charge").import assert from "node:assert/strict";
import type { Event } from "itinera/event";
import { type DataBag, dataBag } from "itinera/journey";
import type { Reporter } from "itinera/report";
import { type Outcome, Step } from "itinera/step";
import { Workflow } from "itinera/workflow";
import { integer } from "itinera/value";
class Charge extends Step {
override run(): Outcome {
return this.succeed();
}
}
class Ship extends Step {
override run(): Outcome {
return this.succeed();
}
}
class Audit implements Reporter {
readonly kinds: string[] = [];
report(event: Event): void {
this.kinds.push(event.kind);
}
}
class Orders extends Workflow {
constructor(readonly shop: string) {
super();
}
orderId(data: DataBag): string {
const number = data.read("number", integer).option();
return `${this.shop}-${number.map(String).unwrapOr("unnumbered")}`;
}
}
const orders = Orders.builder()
.name("orders")
.step(Charge.builder("charge").retryBudget(1).noRetryOnAbnormalTermination())
.step(Ship.builder("ship"))
.reporter(Audit)
.idGenerator("orderId", dataBag())
.constructWith("corner")
.build();
const instance = orders.instance({ number: 7 });
assert.equal(instance.descriptor.name, "orders");
assert.equal(instance.journeyId, "corner-7");
assert.deepEqual(
instance.descriptor.steps().map((described) => described.name),
["charge", "ship"],
);
assert.equal(instance.descriptor.steps()[0]?.retryBudget, 1);
assert.equal(instance.descriptor.steps()[0]?.retryOnAbnormalTermination, false);A step keeps no static data, refused as WithoutStaticData says: at compile time, or else when the builder starts.
Type Parameters
- C extends StepClass
Parameters
- this: WithoutStaticData<C, typeof Step>
- name: string
Returns StepBuilder<C>
The base class of the step mocks stepMock makes: a step that plays its script, ending each attempt through an Ending, so that it ends exactly as a real step would, and keeps the inputs it was built with.