1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 const WIDGET = "src/admin/widgets/my-widget.tsx"
7
8 ruleTester.run("widget-must-export-config", rule, {
9 valid: [
10 // Standard widget: default export + named config via defineWidgetConfig.
11 {
12 code: `import { defineWidgetConfig } from "@medusajs/admin-sdk"
13 const MyWidget = () => null
14 export const config = defineWidgetConfig({ zone: "product.details.before" })
15 export default MyWidget`,
16 filename: WIDGET,
17 },
18 // Aliased defineWidgetConfig import.
19 {
20 code: `import { defineWidgetConfig as dwc } from "@medusajs/admin-sdk"
21 const MyWidget = () => null
22 export const config = dwc({ zone: "product.details.before" })
23 export default MyWidget`,
24 filename: WIDGET,
25 },
26 // Re-export of config from another module — accepted (initializer unreachable).
27 {
28 code: `export { config } from "./widget-config"
29 export default () => null`,
30 filename: WIDGET,
31 },
32 // Renamed export `foo as config`.
33 {
34 code: `import { defineWidgetConfig } from "@medusajs/admin-sdk"
35 const widgetConfig = defineWidgetConfig({ zone: "product.details.before" })
36 export { widgetConfig as config }
37 export default () => null`,
38 filename: WIDGET,
39 },
40 ],
41 invalid: [
42 // No config export at all.
43 {
44 code: `const MyWidget = () => null
45 export default MyWidget`,
46 filename: WIDGET,
47 errors: [{ messageId: "missingConfigExport" }],
48 },
49 // Only a default export, no named config.
50 {
51 code: `export default () => null`,
52 filename: WIDGET,
53 errors: [{ messageId: "missingConfigExport" }],
54 },
55 // `export const config = {...}` — not a defineWidgetConfig call.
56 {
57 code: `export const config = { zone: "product.details.before" }
58 export default () => null`,
59 filename: WIDGET,
60 errors: [{ messageId: "configNotDefineWidgetConfig" }],
61 },
62 // `export const config = somethingElse(...)` — wrong function.
63 {
64 code: `import { defineWidgetConfig } from "@medusajs/admin-sdk"
65 function buildConfig() { return { zone: "product.details.before" } }
66 export const config = buildConfig()
67 export default () => null`,
68 filename: WIDGET,
69 errors: [{ messageId: "configNotDefineWidgetConfig" }],
70 },
71 // defineWidgetConfig imported from a non-admin-sdk source — local name not tracked.
72 {
73 code: `import { defineWidgetConfig } from "./fake-sdk"
74 export const config = defineWidgetConfig({ zone: "product.details.before" })
75 export default () => null`,
76 filename: WIDGET,
77 errors: [{ messageId: "configNotDefineWidgetConfig" }],
78 },
79 ],
80 })