|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
ruleTester.run("link-uses-linkable-properties", rule, { |
|
7
|
valid: [ |
|
8
|
// Canonical: both args are direct linkable member chains. |
|
9
|
{ |
|
10
|
code: ` |
|
11
|
import { defineLink } from "@medusajs/framework/utils" |
|
12
|
import ProductModule from "@medusajs/medusa/product" |
|
13
|
import BlogModule from "../modules/blog" |
|
14
|
export default defineLink( |
|
15
|
ProductModule.linkable.product, |
|
16
|
BlogModule.linkable.post |
|
17
|
) |
|
18
|
`, |
|
19
|
}, |
|
20
|
// Object form with linkable property (isList). |
|
21
|
{ |
|
22
|
code: ` |
|
23
|
import { defineLink } from "@medusajs/framework/utils" |
|
24
|
import ProductModule from "@medusajs/medusa/product" |
|
25
|
import BlogModule from "../modules/blog" |
|
26
|
export default defineLink( |
|
27
|
ProductModule.linkable.product, |
|
28
|
{ linkable: BlogModule.linkable.post, isList: true } |
|
29
|
) |
|
30
|
`, |
|
31
|
}, |
|
32
|
// Both args in object form (many-to-many). |
|
33
|
{ |
|
34
|
code: ` |
|
35
|
import { defineLink } from "@medusajs/framework/utils" |
|
36
|
import ProductModule from "@medusajs/medusa/product" |
|
37
|
import BlogModule from "../modules/blog" |
|
38
|
export default defineLink( |
|
39
|
{ linkable: ProductModule.linkable.product, isList: true }, |
|
40
|
{ linkable: BlogModule.linkable.post, isList: true } |
|
41
|
) |
|
42
|
`, |
|
43
|
}, |
|
44
|
// Third options arg is ignored (database renaming). |
|
45
|
{ |
|
46
|
code: ` |
|
47
|
import { defineLink } from "@medusajs/framework/utils" |
|
48
|
import ProductModule from "@medusajs/medusa/product" |
|
49
|
import ArticleModule from "../modules/article" |
|
50
|
export default defineLink( |
|
51
|
ProductModule.linkable.product, |
|
52
|
{ linkable: ArticleModule.linkable.post, isList: true }, |
|
53
|
{ database: { table: "product_product_blog_post" } } |
|
54
|
) |
|
55
|
`, |
|
56
|
}, |
|
57
|
// Aliased defineLink import. |
|
58
|
{ |
|
59
|
code: ` |
|
60
|
import { defineLink as dl } from "@medusajs/framework/utils" |
|
61
|
import ProductModule from "@medusajs/medusa/product" |
|
62
|
import BlogModule from "../modules/blog" |
|
63
|
export default dl(ProductModule.linkable.product, BlogModule.linkable.post) |
|
64
|
`, |
|
65
|
}, |
|
66
|
// defineLink from a non-framework source — should not trigger. |
|
67
|
{ |
|
68
|
code: ` |
|
69
|
import { defineLink } from "some-other-lib" |
|
70
|
export default defineLink({ foo: "bar" }, "something") |
|
71
|
`, |
|
72
|
}, |
|
73
|
// Inverse read-only link form: second arg spreads `<X>.linkable.<y>.id`. |
|
74
|
{ |
|
75
|
code: ` |
|
76
|
import { defineLink } from "@medusajs/framework/utils" |
|
77
|
import ProductModule from "@medusajs/medusa/product" |
|
78
|
import BlogModule from "../modules/blog" |
|
79
|
export default defineLink( |
|
80
|
{ linkable: ProductModule.linkable.product, field: "id" }, |
|
81
|
{ ...BlogModule.linkable.post.id, primaryKey: "product_id" }, |
|
82
|
{ readOnly: true } |
|
83
|
) |
|
84
|
`, |
|
85
|
}, |
|
86
|
// Read-only link with inline serviceName-based linkable descriptor. |
|
87
|
{ |
|
88
|
code: ` |
|
89
|
import { defineLink } from "@medusajs/framework/utils" |
|
90
|
import ProductModule from "@medusajs/medusa/product" |
|
91
|
import { CMS_MODULE } from "../modules/cms" |
|
92
|
export default defineLink( |
|
93
|
{ linkable: ProductModule.linkable.product, field: "id" }, |
|
94
|
{ |
|
95
|
linkable: { |
|
96
|
serviceName: CMS_MODULE, |
|
97
|
alias: "cms_post", |
|
98
|
primaryKey: "product_id", |
|
99
|
}, |
|
100
|
}, |
|
101
|
{ readOnly: true } |
|
102
|
) |
|
103
|
`, |
|
104
|
}, |
|
105
|
// Object form whose `linkable` value references a data model by id |
|
106
|
// (e.g. the only way to link to draft orders, via `order.id`). |
|
107
|
{ |
|
108
|
code: ` |
|
109
|
import { defineLink } from "@medusajs/framework/utils" |
|
110
|
import MarketplaceModule from "../modules/marketplace" |
|
111
|
import OrderModule from "@medusajs/medusa/order" |
|
112
|
export default defineLink( |
|
113
|
MarketplaceModule.linkable.vendor, |
|
114
|
{ linkable: OrderModule.linkable.order.id, isList: true } |
|
115
|
) |
|
116
|
`, |
|
117
|
}, |
|
118
|
// Deeper module chain (e.g. namespace import). |
|
119
|
{ |
|
120
|
code: ` |
|
121
|
import { defineLink } from "@medusajs/framework/utils" |
|
122
|
import * as Modules from "../modules" |
|
123
|
export default defineLink( |
|
124
|
Modules.Product.linkable.product, |
|
125
|
Modules.Blog.linkable.post |
|
126
|
) |
|
127
|
`, |
|
128
|
}, |
|
129
|
], |
|
130
|
invalid: [ |
|
131
|
// Bare object literal without linkable property. |
|
132
|
{ |
|
133
|
code: ` |
|
134
|
import { defineLink } from "@medusajs/framework/utils" |
|
135
|
import ProductModule from "@medusajs/medusa/product" |
|
136
|
export default defineLink( |
|
137
|
ProductModule.linkable.product, |
|
138
|
{ foo: "bar" } |
|
139
|
) |
|
140
|
`, |
|
141
|
errors: [{ messageId: "notLinkableProperty" }], |
|
142
|
}, |
|
143
|
// String literal participant. |
|
144
|
{ |
|
145
|
code: ` |
|
146
|
import { defineLink } from "@medusajs/framework/utils" |
|
147
|
import BlogModule from "../modules/blog" |
|
148
|
export default defineLink("product", BlogModule.linkable.post) |
|
149
|
`, |
|
150
|
errors: [{ messageId: "notLinkableProperty" }], |
|
151
|
}, |
|
152
|
// Direct module reference without .linkable. |
|
153
|
{ |
|
154
|
code: ` |
|
155
|
import { defineLink } from "@medusajs/framework/utils" |
|
156
|
import ProductModule from "@medusajs/medusa/product" |
|
157
|
import BlogModule from "../modules/blog" |
|
158
|
export default defineLink(ProductModule, BlogModule.linkable.post) |
|
159
|
`, |
|
160
|
errors: [{ messageId: "notLinkableProperty" }], |
|
161
|
}, |
|
162
|
// Object form whose `linkable` value is not a linkable member chain. |
|
163
|
{ |
|
164
|
code: ` |
|
165
|
import { defineLink } from "@medusajs/framework/utils" |
|
166
|
import ProductModule from "@medusajs/medusa/product" |
|
167
|
export default defineLink( |
|
168
|
ProductModule.linkable.product, |
|
169
|
{ linkable: "post", isList: true } |
|
170
|
) |
|
171
|
`, |
|
172
|
errors: [{ messageId: "notLinkableProperty" }], |
|
173
|
}, |
|
174
|
// Both args invalid — two errors. |
|
175
|
{ |
|
176
|
code: ` |
|
177
|
import { defineLink } from "@medusajs/framework/utils" |
|
178
|
export default defineLink("product", "post") |
|
179
|
`, |
|
180
|
errors: [ |
|
181
|
{ messageId: "notLinkableProperty" }, |
|
182
|
{ messageId: "notLinkableProperty" }, |
|
183
|
], |
|
184
|
}, |
|
185
|
// Aliased import + invalid arg. |
|
186
|
{ |
|
187
|
code: ` |
|
188
|
import { defineLink as dl } from "@medusajs/framework/utils" |
|
189
|
import BlogModule from "../modules/blog" |
|
190
|
export default dl({ foo: "bar" }, BlogModule.linkable.post) |
|
191
|
`, |
|
192
|
errors: [{ messageId: "notLinkableProperty" }], |
|
193
|
}, |
|
194
|
// Direct module's .linkable property without sub-model (`ProductModule.linkable`). |
|
195
|
{ |
|
196
|
code: ` |
|
197
|
import { defineLink } from "@medusajs/framework/utils" |
|
198
|
import ProductModule from "@medusajs/medusa/product" |
|
199
|
import BlogModule from "../modules/blog" |
|
200
|
export default defineLink( |
|
201
|
ProductModule.linkable, |
|
202
|
BlogModule.linkable.post |
|
203
|
) |
|
204
|
`, |
|
205
|
errors: [{ messageId: "notLinkableProperty" }], |
|
206
|
}, |
|
207
|
], |
|
208
|
}) |