|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
ruleTester.run("no-nested-when-in-workflow", rule, { |
|
7
|
valid: [ |
|
8
|
// A single top-level `when().then()` in the workflow constructor is fine. |
|
9
|
{ |
|
10
|
code: ` |
|
11
|
import { createWorkflow, when } from "@medusajs/framework/workflows-sdk" |
|
12
|
createWorkflow("my-workflow", (input) => { |
|
13
|
return when({ input }, (data) => data.input.foo).then(() => { |
|
14
|
return input |
|
15
|
}) |
|
16
|
}) |
|
17
|
`, |
|
18
|
}, |
|
19
|
// Sibling (non-nested) `when().then()` calls in the constructor are fine. |
|
20
|
{ |
|
21
|
code: ` |
|
22
|
import { createWorkflow, when, createStep } from "@medusajs/framework/workflows-sdk" |
|
23
|
const stepA = createStep("a", () => {}) |
|
24
|
const stepB = createStep("b", () => {}) |
|
25
|
createWorkflow("my-workflow", (input) => { |
|
26
|
when({ input }, (data) => data.input.foo).then(() => stepA()) |
|
27
|
when({ input }, (data) => data.input.bar).then(() => stepB()) |
|
28
|
return input |
|
29
|
}) |
|
30
|
`, |
|
31
|
}, |
|
32
|
// Not the workflows-sdk import — should not flag. |
|
33
|
{ |
|
34
|
code: ` |
|
35
|
import { when } from "some-other-lib" |
|
36
|
function build(input) { |
|
37
|
when(input, () => true).then(() => { |
|
38
|
when(input, () => false).then(() => {}) |
|
39
|
}) |
|
40
|
} |
|
41
|
`, |
|
42
|
}, |
|
43
|
// `when(...)` outside any when-then callback (e.g. in a plain helper). |
|
44
|
{ |
|
45
|
code: ` |
|
46
|
import { createWorkflow, when } from "@medusajs/framework/workflows-sdk" |
|
47
|
function helper(input) { |
|
48
|
return when(input, () => true) |
|
49
|
} |
|
50
|
createWorkflow("my-workflow", (input) => helper(input)) |
|
51
|
`, |
|
52
|
}, |
|
53
|
], |
|
54
|
invalid: [ |
|
55
|
// an inner `when().then()` nested |
|
56
|
// directly inside an outer `when().then()` callback. |
|
57
|
{ |
|
58
|
code: ` |
|
59
|
import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk" |
|
60
|
createWorkflow("demo", function (input) { |
|
61
|
const outer = when({ input }, (d) => d.input.items.length > 0).then(() => { |
|
62
|
const inner = when({ input }, (d) => d.input.flag).then(() => someStep(input)) |
|
63
|
return inner |
|
64
|
}) |
|
65
|
return new WorkflowResponse(outer) |
|
66
|
}) |
|
67
|
`, |
|
68
|
errors: [{ messageId: "nestedWhenInWorkflow" }], |
|
69
|
}, |
|
70
|
// Sibling nested `when()` calls inside the same outer `.then()` callback |
|
71
|
// are each flagged — the bug hits as soon as any inner `when().then()` |
|
72
|
// resolves before the outer one does. |
|
73
|
{ |
|
74
|
code: ` |
|
75
|
import { createWorkflow, when } from "@medusajs/framework/workflows-sdk" |
|
76
|
createWorkflow("my-workflow", (input) => { |
|
77
|
when({ input }, (d) => d.input.foo).then(() => { |
|
78
|
when({ input }, (d) => d.input.bar).then(() => stepA()) |
|
79
|
when({ input }, (d) => d.input.baz).then(() => stepB()) |
|
80
|
}) |
|
81
|
}) |
|
82
|
`, |
|
83
|
errors: [ |
|
84
|
{ messageId: "nestedWhenInWorkflow" }, |
|
85
|
{ messageId: "nestedWhenInWorkflow" }, |
|
86
|
], |
|
87
|
}, |
|
88
|
// Nested `when()` guarded by an intervening `if` still counts — no |
|
89
|
// function boundary sits between the inner call and the outer callback. |
|
90
|
{ |
|
91
|
code: ` |
|
92
|
import { createWorkflow, when } from "@medusajs/framework/workflows-sdk" |
|
93
|
createWorkflow("my-workflow", (input) => { |
|
94
|
when({ input }, (d) => d.input.foo).then(() => { |
|
95
|
if (input.flag) { |
|
96
|
when({ input }, (d) => d.input.bar).then(() => stepA()) |
|
97
|
} |
|
98
|
}) |
|
99
|
}) |
|
100
|
`, |
|
101
|
errors: [{ messageId: "nestedWhenInWorkflow" }], |
|
102
|
}, |
|
103
|
// Aliased `when` import is tracked too. |
|
104
|
{ |
|
105
|
code: ` |
|
106
|
import { createWorkflow, when as w } from "@medusajs/framework/workflows-sdk" |
|
107
|
createWorkflow("my-workflow", (input) => { |
|
108
|
w({ input }, (d) => d.input.foo).then(() => { |
|
109
|
w({ input }, (d) => d.input.bar).then(() => stepA()) |
|
110
|
}) |
|
111
|
}) |
|
112
|
`, |
|
113
|
errors: [{ messageId: "nestedWhenInWorkflow" }], |
|
114
|
}, |
|
115
|
// Three levels deep — the innermost and middle `when()` calls are each |
|
116
|
// nested inside a `when().then()` callback. |
|
117
|
{ |
|
118
|
code: ` |
|
119
|
import { createWorkflow, when } from "@medusajs/framework/workflows-sdk" |
|
120
|
createWorkflow("my-workflow", (input) => { |
|
121
|
when({ input }, (d) => d.input.a).then(() => { |
|
122
|
when({ input }, (d) => d.input.b).then(() => { |
|
123
|
when({ input }, (d) => d.input.c).then(() => stepA()) |
|
124
|
}) |
|
125
|
}) |
|
126
|
}) |
|
127
|
`, |
|
128
|
errors: [ |
|
129
|
{ messageId: "nestedWhenInWorkflow" }, |
|
130
|
{ messageId: "nestedWhenInWorkflow" }, |
|
131
|
], |
|
132
|
}, |
|
133
|
], |
|
134
|
}) |