|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { RemoteQueryFunction, SearchTypes } from "@medusajs/types" |
|
3
|
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils" |
|
4
|
import { |
|
5
|
adminHeaders, |
|
6
|
createAdminUser, |
|
7
|
} from "../../../helpers/create-admin-user" |
|
8
|
|
|
9
|
jest.setTimeout(120000) |
|
10
|
|
|
11
|
/** |
|
12
|
* The product index holds `id`, `title`, `handle` and `status` and nothing |
|
13
|
* else, so anything else a caller asks for has to come from `query.graph` and |
|
14
|
* be merged onto the engine's documents. |
|
15
|
*/ |
|
16
|
medusaIntegrationTestRunner({ |
|
17
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
18
|
let query: Omit<RemoteQueryFunction, symbol> |
|
19
|
let searchModule: SearchTypes.ISearchModuleService |
|
20
|
|
|
21
|
beforeAll(async () => { |
|
22
|
const container = getContainer() |
|
23
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
24
|
|
|
25
|
query = container.resolve(ContainerRegistrationKeys.QUERY) |
|
26
|
searchModule = container.resolve(Modules.SEARCH) |
|
27
|
|
|
28
|
await api.post( |
|
29
|
"/admin/products", |
|
30
|
{ |
|
31
|
title: "Zephyr Shirt", |
|
32
|
handle: "zephyr-shirt", |
|
33
|
description: "A shirt for windy days", |
|
34
|
status: "published", |
|
35
|
options: [{ title: "Size", values: ["S", "M"] }], |
|
36
|
variants: [ |
|
37
|
{ |
|
38
|
title: "Small", |
|
39
|
sku: "ZEPHYR-S", |
|
40
|
options: { Size: "S" }, |
|
41
|
prices: [{ amount: 10, currency_code: "usd" }], |
|
42
|
}, |
|
43
|
{ |
|
44
|
title: "Medium", |
|
45
|
sku: "ZEPHYR-M", |
|
46
|
options: { Size: "M" }, |
|
47
|
prices: [{ amount: 20, currency_code: "usd" }], |
|
48
|
}, |
|
49
|
], |
|
50
|
}, |
|
51
|
adminHeaders |
|
52
|
) |
|
53
|
|
|
54
|
await api.post( |
|
55
|
"/admin/products", |
|
56
|
{ |
|
57
|
title: "Aurora Shoe", |
|
58
|
handle: "aurora-shoe", |
|
59
|
description: "A shoe for long walks", |
|
60
|
status: "published", |
|
61
|
options: [{ title: "Size", values: ["42"] }], |
|
62
|
variants: [ |
|
63
|
{ |
|
64
|
title: "42", |
|
65
|
sku: "AURORA-42", |
|
66
|
options: { Size: "42" }, |
|
67
|
prices: [{ amount: 30, currency_code: "usd" }], |
|
68
|
}, |
|
69
|
], |
|
70
|
}, |
|
71
|
adminHeaders |
|
72
|
) |
|
73
|
|
|
74
|
await searchModule.reindex() |
|
75
|
}) |
|
76
|
|
|
77
|
describe("query.search", () => { |
|
78
|
it("returns fields and relations the index does not hold", async () => { |
|
79
|
const { data } = await query.search({ |
|
80
|
entity: "product", |
|
81
|
fields: ["id", "title", "handle", "description", "variants.*"], |
|
82
|
filters: { q: "zephyr", status: "published" }, |
|
83
|
}) |
|
84
|
|
|
85
|
expect(data).toHaveLength(1) |
|
86
|
expect(data[0]).toEqual( |
|
87
|
expect.objectContaining({ |
|
88
|
title: "Zephyr Shirt", |
|
89
|
handle: "zephyr-shirt", |
|
90
|
// Neither of these is on the index. |
|
91
|
description: "A shirt for windy days", |
|
92
|
variants: expect.arrayContaining([ |
|
93
|
expect.objectContaining({ sku: "ZEPHYR-S" }), |
|
94
|
expect.objectContaining({ sku: "ZEPHYR-M" }), |
|
95
|
]), |
|
96
|
}) |
|
97
|
) |
|
98
|
}) |
|
99
|
|
|
100
|
it("keeps the engine's ordering while hydrating", async () => { |
|
101
|
const { data } = await query.search({ |
|
102
|
entity: "product", |
|
103
|
fields: ["id", "title", "description"], |
|
104
|
filters: { status: "published" }, |
|
105
|
pagination: { order: { title: "DESC" } }, |
|
106
|
}) |
|
107
|
|
|
108
|
expect(data.map((product) => product.title)).toEqual([ |
|
109
|
"Zephyr Shirt", |
|
110
|
"Aurora Shoe", |
|
111
|
]) |
|
112
|
expect(data.map((product) => product.description)).toEqual([ |
|
113
|
"A shirt for windy days", |
|
114
|
"A shoe for long walks", |
|
115
|
]) |
|
116
|
}) |
|
117
|
|
|
118
|
it("returns the engine's documents untouched when nothing needs hydrating", async () => { |
|
119
|
const { data } = await query.search({ |
|
120
|
entity: "product", |
|
121
|
fields: ["id", "title", "handle"], |
|
122
|
filters: { q: "aurora" }, |
|
123
|
}) |
|
124
|
|
|
125
|
expect(data).toEqual([ |
|
126
|
expect.objectContaining({ |
|
127
|
title: "Aurora Shoe", |
|
128
|
handle: "aurora-shoe", |
|
129
|
}), |
|
130
|
]) |
|
131
|
expect(data[0]).not.toHaveProperty("description") |
|
132
|
}) |
|
133
|
}) |
|
134
|
}, |
|
135
|
}) |