|
1
|
import { vi } from "vitest" |
|
2
|
|
|
3
|
import fs from "fs/promises" |
|
4
|
import * as utils from "../../utils" |
|
5
|
import { generateWidgets } from "../generate-widgets" |
|
6
|
|
|
7
|
vi.mock("../../utils", async () => { |
|
8
|
const actual = await vi.importActual("../../utils") |
|
9
|
return { |
|
10
|
...actual, |
|
11
|
crawl: vi.fn(), |
|
12
|
} |
|
13
|
}) |
|
14
|
|
|
15
|
vi.mock("fs/promises", () => ({ |
|
16
|
default: { |
|
17
|
readFile: vi.fn(), |
|
18
|
}, |
|
19
|
})) |
|
20
|
|
|
21
|
const mockFileContents = [ |
|
22
|
` |
|
23
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
24
|
|
|
25
|
const Widget = () => { |
|
26
|
return <div>Widget 1</div> |
|
27
|
} |
|
28
|
|
|
29
|
export const config = defineWidgetConfig({ |
|
30
|
zone: "product.details.after", |
|
31
|
}) |
|
32
|
|
|
33
|
export default Widget |
|
34
|
`, |
|
35
|
] |
|
36
|
|
|
37
|
// `<component-name>-<short hash>`. The hash is of the admin-relative path |
|
38
|
// "widgets/widget.tsx" — independent of the absolute/OS path, so both the Unix |
|
39
|
// and Windows cases share this id. |
|
40
|
const expectedWidgets = ` |
|
41
|
widgets: [ |
|
42
|
{ |
|
43
|
Component: WidgetComponent0, |
|
44
|
zone: ["product.details.after"], |
|
45
|
widgetId: "Widget-e4f7" |
|
46
|
} |
|
47
|
] |
|
48
|
` |
|
49
|
|
|
50
|
describe("generateWidgets", () => { |
|
51
|
it("should generate widgets", async () => { |
|
52
|
const mockFiles = ["Users/user/medusa/src/admin/widgets/widget.tsx"] |
|
53
|
vi.mocked(utils.crawl).mockResolvedValue(mockFiles) |
|
54
|
|
|
55
|
vi.mocked(fs.readFile).mockImplementation(async (file) => |
|
56
|
Promise.resolve(mockFileContents[mockFiles.indexOf(file as string)]) |
|
57
|
) |
|
58
|
|
|
59
|
const result = await generateWidgets( |
|
60
|
new Set(["Users/user/medusa/src/admin"]) |
|
61
|
) |
|
62
|
|
|
63
|
expect(result.imports).toEqual([ |
|
64
|
`import WidgetComponent0, { config as WidgetConfig0 } from "Users/user/medusa/src/admin/widgets/widget.tsx"`, |
|
65
|
]) |
|
66
|
expect(utils.normalizeString(result.code)).toEqual( |
|
67
|
utils.normalizeString(expectedWidgets) |
|
68
|
) |
|
69
|
}) |
|
70
|
it("should handle windows paths", async () => { |
|
71
|
const mockFiles = ["C:\\medusa\\src\\admin\\widgets\\widget.tsx"] |
|
72
|
vi.mocked(utils.crawl).mockResolvedValue(mockFiles) |
|
73
|
|
|
74
|
vi.mocked(fs.readFile).mockImplementation(async (file) => |
|
75
|
Promise.resolve(mockFileContents[mockFiles.indexOf(file as string)]) |
|
76
|
) |
|
77
|
|
|
78
|
const result = await generateWidgets(new Set(["C:\\medusa\\src\\admin"])) |
|
79
|
|
|
80
|
expect(result.imports).toEqual([ |
|
81
|
`import WidgetComponent0, { config as WidgetConfig0 } from "C:/medusa/src/admin/widgets/widget.tsx"`, |
|
82
|
]) |
|
83
|
expect(utils.normalizeString(result.code)).toEqual( |
|
84
|
utils.normalizeString(expectedWidgets) |
|
85
|
) |
|
86
|
}) |
|
87
|
it("should use an explicit id from the widget config", async () => { |
|
88
|
const mockFiles = ["Users/user/medusa/src/admin/widgets/widget.tsx"] |
|
89
|
vi.mocked(utils.crawl).mockResolvedValue(mockFiles) |
|
90
|
|
|
91
|
vi.mocked(fs.readFile).mockImplementation(async () => |
|
92
|
Promise.resolve(` |
|
93
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
94
|
|
|
95
|
const Widget = () => <div>Widget</div> |
|
96
|
|
|
97
|
export const config = defineWidgetConfig({ |
|
98
|
zone: "product.details.after", |
|
99
|
id: "my-plugin:product-summary", |
|
100
|
}) |
|
101
|
|
|
102
|
export default Widget |
|
103
|
`) |
|
104
|
) |
|
105
|
|
|
106
|
const result = await generateWidgets( |
|
107
|
new Set(["Users/user/medusa/src/admin"]) |
|
108
|
) |
|
109
|
|
|
110
|
expect(utils.normalizeString(result.code)).toEqual( |
|
111
|
utils.normalizeString(` |
|
112
|
widgets: [ |
|
113
|
{ |
|
114
|
Component: WidgetComponent0, |
|
115
|
zone: ["product.details.after"], |
|
116
|
widgetId: "my-plugin:product-summary" |
|
117
|
} |
|
118
|
] |
|
119
|
`) |
|
120
|
) |
|
121
|
}) |
|
122
|
}) |