1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 const PAGE = "src/admin/routes/custom/page.tsx"
7 const DYNAMIC_PAGE = "src/admin/routes/products/[id]/page.tsx"
8
9 ruleTester.run("ui-route-config-via-define-route-config", rule, {
10 valid: [
11 // config initialized via defineRouteConfig.
12 {
13 code: `import { defineRouteConfig } from "@medusajs/admin-sdk"
14 const CustomPage = () => null
15 export const config = defineRouteConfig({ label: "Custom" })
16 export default CustomPage`,
17 filename: PAGE,
18 },
19 // Aliased defineRouteConfig import.
20 {
21 code: `import { defineRouteConfig as drc } from "@medusajs/admin-sdk"
22 const CustomPage = () => null
23 export const config = drc({ label: "Custom" })
24 export default CustomPage`,
25 filename: PAGE,
26 },
27 // No config export at all — nothing to enforce.
28 {
29 code: `const CustomPage = () => null
30 export default CustomPage`,
31 filename: PAGE,
32 },
33 // A non-`config` export initialized via something else is irrelevant.
34 {
35 code: `const CustomPage = () => null
36 export const label = "Custom"
37 export default CustomPage`,
38 filename: PAGE,
39 },
40 // config typed but still initialized via defineRouteConfig.
41 {
42 code: `import { defineRouteConfig } from "@medusajs/admin-sdk"
43 import type { RouteConfig } from "@medusajs/admin-sdk"
44 const CustomPage = () => null
45 export const config: RouteConfig = defineRouteConfig({ label: "Custom" })
46 export default CustomPage`,
47 filename: PAGE,
48 },
49 // Dynamic route — `config` doesn't apply, so a bad initializer is ignored.
50 {
51 code: `const CustomPage = () => null
52 export const config = { label: "Custom" }
53 export default CustomPage`,
54 filename: DYNAMIC_PAGE,
55 },
56 ],
57 invalid: [
58 // config is a plain object literal — reported on the `config` variable.
59 {
60 code: `const CustomPage = () => null
61 export const config = { label: "Custom" }
62 export default CustomPage`,
63 filename: PAGE,
64 errors: [
65 { messageId: "configNotDefineRouteConfig", line: 2, column: 14 },
66 ],
67 },
68 // config is some other call expression.
69 {
70 code: `const buildConfig = () => ({ label: "Custom" })
71 const CustomPage = () => null
72 export const config = buildConfig()
73 export default CustomPage`,
74 filename: PAGE,
75 errors: [
76 { messageId: "configNotDefineRouteConfig", line: 3, column: 14 },
77 ],
78 },
79 // config references an identifier defined elsewhere.
80 {
81 code: `const routeConfig = { label: "Custom" }
82 const CustomPage = () => null
83 export const config = routeConfig
84 export default CustomPage`,
85 filename: PAGE,
86 errors: [{ messageId: "configNotDefineRouteConfig" }],
87 },
88 // `defineRouteConfig` not imported from the admin SDK — local shadow.
89 {
90 code: `const defineRouteConfig = (x) => x
91 const CustomPage = () => null
92 export const config = defineRouteConfig({ label: "Custom" })
93 export default CustomPage`,
94 filename: PAGE,
95 errors: [{ messageId: "configNotDefineRouteConfig" }],
96 },
97 ],
98 })