|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
ruleTester.run("read-only-link-requires-field", rule, { |
|
7
|
valid: [ |
|
8
|
// Canonical read-only link with `field`. |
|
9
|
{ |
|
10
|
code: ` |
|
11
|
import { defineLink } from "@medusajs/framework/utils" |
|
12
|
import BlogModule from "../modules/blog" |
|
13
|
import ProductModule from "@medusajs/medusa/product" |
|
14
|
export default defineLink( |
|
15
|
{ linkable: BlogModule.linkable.post, field: "product_id" }, |
|
16
|
ProductModule.linkable.product, |
|
17
|
{ readOnly: true } |
|
18
|
) |
|
19
|
`, |
|
20
|
}, |
|
21
|
// Inverse read-only link with `field` + `primaryKey`. |
|
22
|
{ |
|
23
|
code: ` |
|
24
|
import { defineLink } from "@medusajs/framework/utils" |
|
25
|
import BlogModule from "../modules/blog" |
|
26
|
import ProductModule from "@medusajs/medusa/product" |
|
27
|
export default defineLink( |
|
28
|
{ linkable: ProductModule.linkable.product, field: "id" }, |
|
29
|
{ ...BlogModule.linkable.post.id, primaryKey: "product_id" }, |
|
30
|
{ readOnly: true } |
|
31
|
) |
|
32
|
`, |
|
33
|
}, |
|
34
|
// Inverse read-only link with `field` + `alias` (e.g. the only way to link |
|
35
|
// to draft orders, via the `order` linkable re-aliased to `draft_order`). |
|
36
|
{ |
|
37
|
code: ` |
|
38
|
import { defineLink } from "@medusajs/framework/utils" |
|
39
|
import QuoteModule from "../modules/quote" |
|
40
|
import OrderModule from "@medusajs/medusa/order" |
|
41
|
export default defineLink( |
|
42
|
{ linkable: QuoteModule.linkable.quote, field: "draft_order_id" }, |
|
43
|
{ ...OrderModule.linkable.order.id, alias: "draft_order" }, |
|
44
|
{ readOnly: true } |
|
45
|
) |
|
46
|
`, |
|
47
|
}, |
|
48
|
// Read-only link with inline serviceName-based linkable descriptor + primaryKey on inverse not needed (no spread). |
|
49
|
{ |
|
50
|
code: ` |
|
51
|
import { defineLink } from "@medusajs/framework/utils" |
|
52
|
import ProductModule from "@medusajs/medusa/product" |
|
53
|
import { CMS_MODULE } from "../modules/cms" |
|
54
|
export default defineLink( |
|
55
|
{ linkable: ProductModule.linkable.product, field: "id" }, |
|
56
|
{ |
|
57
|
linkable: { |
|
58
|
serviceName: CMS_MODULE, |
|
59
|
alias: "cms_post", |
|
60
|
primaryKey: "product_id", |
|
61
|
}, |
|
62
|
}, |
|
63
|
{ readOnly: true } |
|
64
|
) |
|
65
|
`, |
|
66
|
}, |
|
67
|
// Not a read-only link — no `readOnly` key. |
|
68
|
{ |
|
69
|
code: ` |
|
70
|
import { defineLink } from "@medusajs/framework/utils" |
|
71
|
import ProductModule from "@medusajs/medusa/product" |
|
72
|
import BlogModule from "../modules/blog" |
|
73
|
export default defineLink( |
|
74
|
ProductModule.linkable.product, |
|
75
|
BlogModule.linkable.post |
|
76
|
) |
|
77
|
`, |
|
78
|
}, |
|
79
|
// `readOnly: false` — does not require field. |
|
80
|
{ |
|
81
|
code: ` |
|
82
|
import { defineLink } from "@medusajs/framework/utils" |
|
83
|
import ProductModule from "@medusajs/medusa/product" |
|
84
|
import BlogModule from "../modules/blog" |
|
85
|
export default defineLink( |
|
86
|
ProductModule.linkable.product, |
|
87
|
BlogModule.linkable.post, |
|
88
|
{ readOnly: false } |
|
89
|
) |
|
90
|
`, |
|
91
|
}, |
|
92
|
// defineLink from a non-framework source — not tracked. |
|
93
|
{ |
|
94
|
code: ` |
|
95
|
import { defineLink } from "some-other-lib" |
|
96
|
export default defineLink({ foo: "bar" }, "x", { readOnly: true }) |
|
97
|
`, |
|
98
|
}, |
|
99
|
// Aliased defineLink import with valid shape. |
|
100
|
{ |
|
101
|
code: ` |
|
102
|
import { defineLink as dl } from "@medusajs/framework/utils" |
|
103
|
import BlogModule from "../modules/blog" |
|
104
|
import ProductModule from "@medusajs/medusa/product" |
|
105
|
export default dl( |
|
106
|
{ linkable: BlogModule.linkable.post, field: "product_id" }, |
|
107
|
ProductModule.linkable.product, |
|
108
|
{ readOnly: true } |
|
109
|
) |
|
110
|
`, |
|
111
|
}, |
|
112
|
// `readOnly` is not a literal boolean — out of scope (statically unknown). |
|
113
|
{ |
|
114
|
code: ` |
|
115
|
import { defineLink } from "@medusajs/framework/utils" |
|
116
|
import ProductModule from "@medusajs/medusa/product" |
|
117
|
import BlogModule from "../modules/blog" |
|
118
|
const flag = true |
|
119
|
export default defineLink( |
|
120
|
ProductModule.linkable.product, |
|
121
|
BlogModule.linkable.post, |
|
122
|
{ readOnly: flag } |
|
123
|
) |
|
124
|
`, |
|
125
|
}, |
|
126
|
], |
|
127
|
invalid: [ |
|
128
|
// Read-only link, first arg is a bare linkable member chain (no `field`). |
|
129
|
{ |
|
130
|
code: ` |
|
131
|
import { defineLink } from "@medusajs/framework/utils" |
|
132
|
import BlogModule from "../modules/blog" |
|
133
|
import ProductModule from "@medusajs/medusa/product" |
|
134
|
export default defineLink( |
|
135
|
BlogModule.linkable.post, |
|
136
|
ProductModule.linkable.product, |
|
137
|
{ readOnly: true } |
|
138
|
) |
|
139
|
`, |
|
140
|
errors: [{ messageId: "missingField" }], |
|
141
|
}, |
|
142
|
// Read-only link, first arg is an object but without `field`. |
|
143
|
{ |
|
144
|
code: ` |
|
145
|
import { defineLink } from "@medusajs/framework/utils" |
|
146
|
import BlogModule from "../modules/blog" |
|
147
|
import ProductModule from "@medusajs/medusa/product" |
|
148
|
export default defineLink( |
|
149
|
{ linkable: BlogModule.linkable.post }, |
|
150
|
ProductModule.linkable.product, |
|
151
|
{ readOnly: true } |
|
152
|
) |
|
153
|
`, |
|
154
|
errors: [{ messageId: "missingField" }], |
|
155
|
}, |
|
156
|
// Inverse read-only: second arg spreads `.linkable.<y>.id` but no `primaryKey`. |
|
157
|
{ |
|
158
|
code: ` |
|
159
|
import { defineLink } from "@medusajs/framework/utils" |
|
160
|
import BlogModule from "../modules/blog" |
|
161
|
import ProductModule from "@medusajs/medusa/product" |
|
162
|
export default defineLink( |
|
163
|
{ linkable: ProductModule.linkable.product, field: "id" }, |
|
164
|
{ ...BlogModule.linkable.post.id }, |
|
165
|
{ readOnly: true } |
|
166
|
) |
|
167
|
`, |
|
168
|
errors: [{ messageId: "missingPrimaryKey" }], |
|
169
|
}, |
|
170
|
// Inverse read-only: first arg also missing field → two errors. |
|
171
|
{ |
|
172
|
code: ` |
|
173
|
import { defineLink } from "@medusajs/framework/utils" |
|
174
|
import BlogModule from "../modules/blog" |
|
175
|
import ProductModule from "@medusajs/medusa/product" |
|
176
|
export default defineLink( |
|
177
|
{ linkable: ProductModule.linkable.product }, |
|
178
|
{ ...BlogModule.linkable.post.id }, |
|
179
|
{ readOnly: true } |
|
180
|
) |
|
181
|
`, |
|
182
|
errors: [ |
|
183
|
{ messageId: "missingField" }, |
|
184
|
{ messageId: "missingPrimaryKey" }, |
|
185
|
], |
|
186
|
}, |
|
187
|
// Aliased import + missing field. |
|
188
|
{ |
|
189
|
code: ` |
|
190
|
import { defineLink as dl } from "@medusajs/framework/utils" |
|
191
|
import BlogModule from "../modules/blog" |
|
192
|
import ProductModule from "@medusajs/medusa/product" |
|
193
|
export default dl( |
|
194
|
BlogModule.linkable.post, |
|
195
|
ProductModule.linkable.product, |
|
196
|
{ readOnly: true } |
|
197
|
) |
|
198
|
`, |
|
199
|
errors: [{ messageId: "missingField" }], |
|
200
|
}, |
|
201
|
// First arg is a non-object (e.g. identifier) — still flagged as missing field. |
|
202
|
{ |
|
203
|
code: ` |
|
204
|
import { defineLink } from "@medusajs/framework/utils" |
|
205
|
import BlogModule from "../modules/blog" |
|
206
|
import ProductModule from "@medusajs/medusa/product" |
|
207
|
const first = BlogModule.linkable.post |
|
208
|
export default defineLink( |
|
209
|
first, |
|
210
|
ProductModule.linkable.product, |
|
211
|
{ readOnly: true } |
|
212
|
) |
|
213
|
`, |
|
214
|
errors: [{ messageId: "missingField" }], |
|
215
|
}, |
|
216
|
], |
|
217
|
}) |