|
1
|
import { medusaIntegrationTestRunner, TestEventUtils } from "@medusajs/test-utils" |
|
2
|
import { SearchTypes } from "@medusajs/types" |
|
3
|
import { Modules, ProductEvents } from "@medusajs/utils" |
|
4
|
import { |
|
5
|
adminHeaders, |
|
6
|
createAdminUser, |
|
7
|
} from "../../../helpers/create-admin-user" |
|
8
|
|
|
9
|
jest.setTimeout(120000) |
|
10
|
|
|
11
|
medusaIntegrationTestRunner({ |
|
12
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
13
|
let searchModule: SearchTypes.ISearchModuleService |
|
14
|
let eventBus |
|
15
|
|
|
16
|
const titles = async (q?: string) => { |
|
17
|
const result = await searchModule.search({ |
|
18
|
entity: "product", |
|
19
|
fields: ["id", "title", "handle", "status"], |
|
20
|
filters: q ? { q } : undefined, |
|
21
|
}) |
|
22
|
|
|
23
|
return result.hits.map((hit) => hit.document.title) |
|
24
|
} |
|
25
|
|
|
26
|
// Ingestion is only observable once the event has been delivered. Set up |
|
27
|
// before the request that emits it. |
|
28
|
const delivered = (event: string) => |
|
29
|
TestEventUtils.waitSubscribersExecution(event, eventBus) |
|
30
|
|
|
31
|
beforeAll(async () => { |
|
32
|
const container = getContainer() |
|
33
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
34
|
|
|
35
|
searchModule = container.resolve(Modules.SEARCH) |
|
36
|
eventBus = container.resolve(Modules.EVENT_BUS) |
|
37
|
}) |
|
38
|
|
|
39
|
describe("built-in search ingestion subscriber", () => { |
|
40
|
it("indexes a product created over the API, without a reindex", async () => { |
|
41
|
const subscribed = delivered(ProductEvents.PRODUCT_CREATED) |
|
42
|
|
|
43
|
const created = ( |
|
44
|
await api.post( |
|
45
|
"/admin/products", |
|
46
|
{ |
|
47
|
title: "Zephyr Shirt", |
|
48
|
handle: "zephyr-shirt", |
|
49
|
status: "published", |
|
50
|
options: [{ title: "Size", values: ["M"] }], |
|
51
|
}, |
|
52
|
adminHeaders |
|
53
|
) |
|
54
|
).data.product |
|
55
|
|
|
56
|
await subscribed |
|
57
|
|
|
58
|
expect(await titles("zephyr")).toEqual(["Zephyr Shirt"]) |
|
59
|
|
|
60
|
const result = await searchModule.search({ |
|
61
|
entity: "product", |
|
62
|
fields: ["id", "title", "handle", "status"], |
|
63
|
filters: { q: "zephyr" }, |
|
64
|
}) |
|
65
|
|
|
66
|
// Built by `consume` through `query.graph` — the event only carries an id. |
|
67
|
expect(result.hits[0]).toEqual( |
|
68
|
expect.objectContaining({ |
|
69
|
id: created.id, |
|
70
|
document: expect.objectContaining({ |
|
71
|
id: created.id, |
|
72
|
title: "Zephyr Shirt", |
|
73
|
handle: "zephyr-shirt", |
|
74
|
status: "published", |
|
75
|
}), |
|
76
|
}) |
|
77
|
) |
|
78
|
}) |
|
79
|
|
|
80
|
it("reindexes a product updated over the API", async () => { |
|
81
|
const created = ( |
|
82
|
await api.post( |
|
83
|
"/admin/products", |
|
84
|
{ |
|
85
|
title: "Aurora Shoe", |
|
86
|
handle: "aurora-shoe", |
|
87
|
status: "published", |
|
88
|
options: [{ title: "Size", values: ["42"] }], |
|
89
|
}, |
|
90
|
adminHeaders |
|
91
|
) |
|
92
|
).data.product |
|
93
|
|
|
94
|
const subscribed = delivered(ProductEvents.PRODUCT_UPDATED) |
|
95
|
|
|
96
|
await api.post( |
|
97
|
`/admin/products/${created.id}`, |
|
98
|
{ title: "Aurora Trail Shoe" }, |
|
99
|
adminHeaders |
|
100
|
) |
|
101
|
|
|
102
|
await subscribed |
|
103
|
|
|
104
|
expect(await titles("aurora")).toEqual(["Aurora Trail Shoe"]) |
|
105
|
}) |
|
106
|
|
|
107
|
it("removes a product deleted over the API", async () => { |
|
108
|
const created = ( |
|
109
|
await api.post( |
|
110
|
"/admin/products", |
|
111
|
{ |
|
112
|
title: "Ephemeral Cap", |
|
113
|
handle: "ephemeral-cap", |
|
114
|
status: "published", |
|
115
|
options: [{ title: "Size", values: ["S"] }], |
|
116
|
}, |
|
117
|
adminHeaders |
|
118
|
) |
|
119
|
).data.product |
|
120
|
|
|
121
|
const subscribed = delivered(ProductEvents.PRODUCT_DELETED) |
|
122
|
|
|
123
|
await api.delete(`/admin/products/${created.id}`, adminHeaders) |
|
124
|
|
|
125
|
await subscribed |
|
126
|
|
|
127
|
expect(await titles("ephemeral")).toEqual([]) |
|
128
|
}) |
|
129
|
}) |
|
130
|
}, |
|
131
|
}) |