serenity-process 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/lib/Step.d.ts +31 -1
- package/lib/Step.js +27 -2
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/package.json +18 -4
package/README.md
CHANGED
|
@@ -26,6 +26,29 @@ Report output reads: `Chidi, as customer, in: Chidi raises an urgent ticket that
|
|
|
26
26
|
- `.with()` returns `() => Promise<void>`, so it can be passed straight to `Given`, `When` or `Then`.
|
|
27
27
|
- `process.roles()` lists the roles in order of first appearance.
|
|
28
28
|
|
|
29
|
+
## Referring to other actors
|
|
30
|
+
|
|
31
|
+
`referringTo(...roles, activity)` builds an activity that refers to other actors. The names of the
|
|
32
|
+
actors playing those roles are passed to `activity` in the same order, so the parameters can be
|
|
33
|
+
named freely:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { Process, referringTo, step } from 'serenity-process';
|
|
37
|
+
|
|
38
|
+
const contest = Process.where('#jury judges #singer and #dancer',
|
|
39
|
+
step('singer', sing()),
|
|
40
|
+
step('dancer', dance()),
|
|
41
|
+
step('jury', referringTo('singer', 'dancer', (s, d) => Compare(s).with(d))),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
await When(contest.with({ singer: piet, dancer: klaas, jury: han })); // Compare('Piet').with('Klaas')
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- Referred roles are roles of the process: TypeScript and `.with()` require an actor for them.
|
|
48
|
+
- TypeScript checks that the function takes as many names as there are roles.
|
|
49
|
+
- It can be mixed with plain activities in one step, and runs with the actors bound at run time.
|
|
50
|
+
- In a nested process, referred roles are mapped by `subprocess` like any other role.
|
|
51
|
+
|
|
29
52
|
## Nested processes
|
|
30
53
|
|
|
31
54
|
A step can itself be a process. `subprocess(child, mapping)` binds the child's roles to roles of the
|
package/lib/Step.d.ts
CHANGED
|
@@ -20,10 +20,40 @@ export interface Step<Role extends string> {
|
|
|
20
20
|
}
|
|
21
21
|
/** The roles of a step type. */
|
|
22
22
|
export type RolesOf<S> = S extends Step<infer Role> ? Role : never;
|
|
23
|
+
/**
|
|
24
|
+
* An activity that refers to other actors in the process, built by {@link referringTo}.
|
|
25
|
+
* Its roles become roles of the step, and of the process.
|
|
26
|
+
*/
|
|
27
|
+
export interface Referring<Role extends string> {
|
|
28
|
+
/** The roles referred to, in the order their names are passed on. */
|
|
29
|
+
readonly roles: readonly Role[];
|
|
30
|
+
/** Builds the activity from the actors playing the roles. */
|
|
31
|
+
activityFor(bindings: RoleBindings<Role>): Activity;
|
|
32
|
+
}
|
|
33
|
+
/** What a {@link step} performs: an activity, or one that refers to other actors. */
|
|
34
|
+
export type StepActivity = Activity | Referring<string>;
|
|
35
|
+
/** The roles referred to by the activities of a step. */
|
|
36
|
+
export type ReferredRolesOf<A> = A extends Referring<infer Role> ? Role : never;
|
|
37
|
+
/**
|
|
38
|
+
* An activity that refers to other actors in the process. The names of the actors
|
|
39
|
+
* playing `roles` are passed to `activity` in the same order, so its parameters
|
|
40
|
+
* can be named freely. The roles become part of the process: `.with()` requires
|
|
41
|
+
* an actor for them, and a nested process maps them like any other role.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* step('judge', referringTo('performer', 'singer', (p, s) => Compare(p).with(s)))
|
|
45
|
+
*/
|
|
46
|
+
export declare function referringTo<const Roles extends [string, ...string[]]>(...args: [...roles: Roles, activity: (...names: {
|
|
47
|
+
[K in keyof Roles]: string;
|
|
48
|
+
}) => Activity]): Referring<Roles[number]>;
|
|
23
49
|
/**
|
|
24
50
|
* A step of a {@link Process}: the role that performs it, and its activities.
|
|
51
|
+
* Use {@link referringTo} for an activity that refers to other actors.
|
|
25
52
|
*
|
|
26
53
|
* @example
|
|
27
54
|
* step('customer', raiseTicket())
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* step('judge', referringTo('performer', performer => JudgeTheWorkOf(performer)))
|
|
28
58
|
*/
|
|
29
|
-
export declare const step: <Role extends string>(role: Role, ...activities:
|
|
59
|
+
export declare const step: <Role extends string, const Activities extends StepActivity[]>(role: Role, ...activities: Activities) => Step<Role | ReferredRolesOf<Activities[number]>>;
|
package/lib/Step.js
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.step = void 0;
|
|
4
|
+
exports.referringTo = referringTo;
|
|
4
5
|
const core_1 = require("@serenity-js/core");
|
|
6
|
+
/**
|
|
7
|
+
* An activity that refers to other actors in the process. The names of the actors
|
|
8
|
+
* playing `roles` are passed to `activity` in the same order, so its parameters
|
|
9
|
+
* can be named freely. The roles become part of the process: `.with()` requires
|
|
10
|
+
* an actor for them, and a nested process maps them like any other role.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* step('judge', referringTo('performer', 'singer', (p, s) => Compare(p).with(s)))
|
|
14
|
+
*/
|
|
15
|
+
function referringTo(...args) {
|
|
16
|
+
const roles = args.slice(0, -1);
|
|
17
|
+
const activity = args[args.length - 1];
|
|
18
|
+
return {
|
|
19
|
+
roles,
|
|
20
|
+
activityFor: bindings => activity(...roles.map(role => bindings[role].name)),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
5
23
|
/**
|
|
6
24
|
* A step of a {@link Process}: the role that performs it, and its activities.
|
|
25
|
+
* Use {@link referringTo} for an activity that refers to other actors.
|
|
7
26
|
*
|
|
8
27
|
* @example
|
|
9
28
|
* step('customer', raiseTicket())
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* step('judge', referringTo('performer', performer => JudgeTheWorkOf(performer)))
|
|
10
32
|
*/
|
|
11
33
|
const step = (role, ...activities) => ({
|
|
12
|
-
roles: [role],
|
|
34
|
+
roles: [...new Set([role, ...activities.flatMap(a => isReferring(a) ? a.roles : [])])],
|
|
13
35
|
async perform(bindings, context) {
|
|
14
|
-
await bindings[role].attemptsTo(core_1.Task.where(`#actor, as ${role}, in: ${context}`, ...activities));
|
|
36
|
+
await bindings[role].attemptsTo(core_1.Task.where(`#actor, as ${role}, in: ${context}`, ...activities.map(a => isReferring(a) ? a.activityFor(bindings) : a)));
|
|
15
37
|
},
|
|
16
38
|
});
|
|
17
39
|
exports.step = step;
|
|
40
|
+
function isReferring(activity) {
|
|
41
|
+
return typeof activity.activityFor === 'function';
|
|
42
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Process } from './Process';
|
|
2
|
-
export { step } from './Step';
|
|
3
|
-
export type { Step, RoleBindings, RolesOf } from './Step';
|
|
2
|
+
export { step, referringTo } from './Step';
|
|
3
|
+
export type { Step, RoleBindings, RolesOf, Referring, ReferredRolesOf, StepActivity } from './Step';
|
|
4
4
|
export { subprocess } from './Subprocess';
|
|
5
5
|
export type { RoleMapping, MappedRoles } from './Subprocess';
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.subprocess = exports.step = exports.Process = void 0;
|
|
3
|
+
exports.subprocess = exports.referringTo = exports.step = exports.Process = void 0;
|
|
4
4
|
var Process_1 = require("./Process");
|
|
5
5
|
Object.defineProperty(exports, "Process", { enumerable: true, get: function () { return Process_1.Process; } });
|
|
6
6
|
var Step_1 = require("./Step");
|
|
7
7
|
Object.defineProperty(exports, "step", { enumerable: true, get: function () { return Step_1.step; } });
|
|
8
|
+
Object.defineProperty(exports, "referringTo", { enumerable: true, get: function () { return Step_1.referringTo; } });
|
|
8
9
|
var Subprocess_1 = require("./Subprocess");
|
|
9
10
|
Object.defineProperty(exports, "subprocess", { enumerable: true, get: function () { return Subprocess_1.subprocess; } });
|
package/package.json
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serenity-process",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Process: like a Serenity/JS Task, but performed by several actors, each playing a role",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"serenity-js",
|
|
7
|
+
"serenity",
|
|
8
|
+
"testing",
|
|
9
|
+
"bdd",
|
|
10
|
+
"screenplay",
|
|
11
|
+
"multi-actor",
|
|
12
|
+
"process"
|
|
13
|
+
],
|
|
6
14
|
"license": "MIT",
|
|
7
15
|
"main": "./lib/index.js",
|
|
8
16
|
"types": "./lib/index.d.ts",
|
|
9
|
-
"files": [
|
|
10
|
-
|
|
17
|
+
"files": [
|
|
18
|
+
"lib",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
11
25
|
"scripts": {
|
|
12
26
|
"build": "tsc -p tsconfig.build.json",
|
|
13
27
|
"test": "vitest run",
|