1 import { getMigrationPlanner, initialize } from "@medusajs/link-modules"
2 import { MedusaModule } from "@medusajs/modules-sdk"
3 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
4 import { ModuleJoinerConfig } from "@medusajs/types"
5
6 jest.setTimeout(5000000)
7
8 medusaIntegrationTestRunner({
9 testSuite: ({ dbConfig: { clientUrl }, dbUtils }) => {
10 let DB_URL
11 let links
12
13 beforeAll(async () => {
14 DB_URL = clientUrl
15
16 const linkDefinition: ModuleJoinerConfig[] = [
17 {
18 serviceName: "linkServiceName",
19 isLink: true,
20 databaseConfig: {
21 tableName: "linkTableName",
22 idPrefix: "prefix",
23 extraFields: {
24 extra_field: {
25 type: "integer",
26 defaultValue: "-1",
27 },
28 another_field: {
29 type: "string",
30 nullable: true,
31 },
32 },
33 },
34 relationships: [
35 {
36 serviceName: "moduleA",
37 primaryKey: "id",
38 foreignKey: "product_id",
39 alias: "product",
40 },
41 {
42 serviceName: "moduleB",
43 primaryKey: "id",
44 foreignKey: "inventory_item_id",
45 alias: "inventory",
46 },
47 ],
48 },
49 ]
50 const dbConfig = {
51 database: {
52 clientUrl: DB_URL,
53 },
54 }
55
56 jest.spyOn(MedusaModule, "getLoadedModules").mockImplementation((() => {
57 return [
58 {
59 moduleA: {
60 __definition: {
61 key: "moduleA",
62 },
63 },
64 },
65 {
66 moduleB: {
67 __definition: {
68 key: "moduleB",
69 },
70 },
71 },
72 ]
73 }) as any)
74
75 const planner = getMigrationPlanner(dbConfig, linkDefinition)
76 await planner.executePlan(await planner.createPlan())
77
78 links = await initialize(dbConfig, linkDefinition)
79 await dbUtils.snapshot()
80 })
81
82 afterAll(async () => {
83 jest.clearAllMocks()
84 })
85
86 describe("Link Modules", () => {
87 it("Should insert values in a declared link", async function () {
88 // simple
89 await links.linkServiceName.create("modA_id", "modB_id")
90
91 // extra fields
92 await links.linkServiceName.create("123", "abc", {
93 extra_field: 333,
94 another_field: "value**",
95 })
96
97 // bulk
98 await links.linkServiceName.create([
99 ["111", "aaa", { another_field: "test" }],
100 ["222", "bbb"],
101 ["333", "ccc", { extra_field: 2 }],
102 ["444", "bbb"],
103 ])
104
105 const values = await links.linkServiceName.list()
106
107 expect(values).toHaveLength(6)
108 expect(values).toEqual(
109 expect.arrayContaining([
110 {
111 product_id: "modA_id",
112 inventory_item_id: "modB_id",
113 id: expect.stringMatching("prefix_.+"),
114 extra_field: -1,
115 another_field: null,
116 created_at: expect.any(Date),
117 updated_at: expect.any(Date),
118 deleted_at: null,
119 },
120 expect.objectContaining({
121 product_id: "123",
122 inventory_item_id: "abc",
123 id: expect.stringMatching("prefix_.+"),
124 extra_field: 333,
125 another_field: "value**",
126 }),
127 expect.objectContaining({
128 product_id: "111",
129 inventory_item_id: "aaa",
130 extra_field: -1,
131 another_field: "test",
132 }),
133 expect.objectContaining({
134 product_id: "222",
135 inventory_item_id: "bbb",
136 extra_field: -1,
137 another_field: null,
138 }),
139 expect.objectContaining({
140 product_id: "333",
141 inventory_item_id: "ccc",
142 id: expect.stringMatching("prefix_.+"),
143 extra_field: 2,
144 }),
145 expect.objectContaining({
146 product_id: "444",
147 inventory_item_id: "bbb",
148 }),
149 ])
150 )
151 })
152
153 it("Should dismiss the link of a given pair of keys", async function () {
154 // simple
155 await links.linkServiceName.create("modA_id", "modB_id")
156
157 // extra fields
158 await links.linkServiceName.create("123", "abc", {
159 extra_field: 333,
160 another_field: "value**",
161 })
162
163 // bulk
164 await links.linkServiceName.create([
165 ["111", "aaa", { another_field: "test" }],
166 ["222", "bbb"],
167 ["333", "ccc", { extra_field: 2 }],
168 ["444", "bbb"],
169 ])
170
171 // simple
172 const dismissSingle = await links.linkServiceName.dismiss(
173 "modA_id",
174 "modB_id"
175 )
176
177 // bulk
178 const dismissMulti = await links.linkServiceName.dismiss([
179 ["111", "aaa"],
180 ["333", "ccc"],
181 ])
182
183 expect(dismissSingle).toEqual([
184 expect.objectContaining({
185 product_id: "modA_id",
186 inventory_item_id: "modB_id",
187 deleted_at: expect.any(Date),
188 }),
189 ])
190
191 expect(dismissMulti).toEqual([
192 expect.objectContaining({
193 product_id: "111",
194 inventory_item_id: "aaa",
195 deleted_at: expect.any(Date),
196 }),
197 expect.objectContaining({
198 product_id: "333",
199 inventory_item_id: "ccc",
200 deleted_at: expect.any(Date),
201 }),
202 ])
203 })
204
205 it("Should delete all the links related to a given key", async function () {
206 // simple
207 await links.linkServiceName.create("modA_id", "modB_id")
208
209 // extra fields
210 await links.linkServiceName.create("123", "abc", {
211 extra_field: 333,
212 another_field: "value**",
213 })
214
215 // bulk
216 await links.linkServiceName.create([
217 ["111", "aaa", { another_field: "test" }],
218 ["222", "bbb"],
219 ["333", "ccc", { extra_field: 2 }],
220 ["444", "bbb"],
221 ])
222
223 await links.linkServiceName.softDelete({
224 inventory_item_id: "bbb",
225 })
226
227 const values = await links.linkServiceName.list(
228 { inventory_item_id: "bbb" },
229 { withDeleted: true }
230 )
231
232 expect(values).toEqual([
233 expect.objectContaining({
234 product_id: "222",
235 inventory_item_id: "bbb",
236 deleted_at: expect.any(Date),
237 }),
238 expect.objectContaining({
239 product_id: "444",
240 inventory_item_id: "bbb",
241 deleted_at: expect.any(Date),
242 }),
243 ])
244 })
245
246 it("Should dismiss the link of a given pair of keys and recreate it", async function () {
247 await links.linkServiceName.create("123", "abc", {
248 extra_field: 333,
249 another_field: "value**",
250 })
251
252 await links.linkServiceName.dismiss("123", "abc")
253
254 const values = await links.linkServiceName.list()
255
256 expect(values).toHaveLength(0)
257
258 await links.linkServiceName.create("123", "abc", {
259 another_field: "changed",
260 })
261
262 const values2 = await links.linkServiceName.list()
263
264 expect(values2).toEqual([
265 expect.objectContaining({
266 product_id: "123",
267 inventory_item_id: "abc",
268 extra_field: 333,
269 another_field: "changed",
270 deleted_at: null,
271 }),
272 ])
273 })
274 })
275 },
276 })