1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { IPricingModuleService, IProductModuleService } from "@medusajs/types"
3 import {
4 ContainerRegistrationKeys,
5 Modules,
6 remoteQueryObjectFromString,
7 } from "@medusajs/utils"
8
9 jest.setTimeout(50000)
10
11 const env = {}
12
13 medusaIntegrationTestRunner({
14 env,
15 testSuite: ({ getContainer }) => {
16 describe("ProductVariant Price Sets", () => {
17 let appContainer
18 let productModule: IProductModuleService
19 let pricingModule: IPricingModuleService
20 let remoteQuery
21 let remoteLink
22
23 beforeAll(async () => {
24 appContainer = getContainer()
25 productModule = appContainer.resolve(Modules.PRODUCT)
26 pricingModule = appContainer.resolve(Modules.PRICING)
27 remoteQuery = appContainer.resolve(
28 ContainerRegistrationKeys.REMOTE_QUERY
29 )
30 remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
31 })
32
33 it("should query product variants and price set link with remote query", async () => {
34 const [product] = await productModule.createProducts([
35 {
36 title: "Test product",
37 variants: [
38 {
39 title: "Test variant",
40 },
41 {
42 title: "Variant number 2",
43 },
44 ],
45 },
46 ])
47
48 const [priceSet1, priceSet2] = await pricingModule.createPriceSets([
49 {
50 prices: [
51 {
52 amount: 3000,
53 currency_code: "usd",
54 },
55 {
56 amount: 5000,
57 currency_code: "eur",
58 rules: {
59 "customer.groups.id": "vip",
60 },
61 },
62 ],
63 },
64 {
65 prices: [
66 {
67 amount: 400,
68 currency_code: "eur",
69 },
70 {
71 amount: 500,
72 currency_code: "usd",
73 },
74 {
75 amount: 100,
76 currency_code: "usd",
77 rules: {
78 "customer.groups.id": "vip",
79 },
80 },
81 ],
82 },
83 ])
84
85 const variant1 = product.variants.find(
86 (v) => v.title === "Test variant"
87 )
88 const variant2 = product.variants.find(
89 (v) => v.title === "Variant number 2"
90 )
91
92 await remoteLink.create([
93 {
94 [Modules.PRODUCT]: {
95 variant_id: variant1!.id,
96 },
97 [Modules.PRICING]: {
98 price_set_id: priceSet1!.id,
99 },
100 },
101 {
102 [Modules.PRODUCT]: {
103 variant_id: variant2!.id,
104 },
105 [Modules.PRICING]: {
106 price_set_id: priceSet2!.id,
107 },
108 },
109 ])
110
111 const query = remoteQueryObjectFromString({
112 entryPoint: "product",
113 variables: {
114 "variants.calculated_price": {
115 context: {
116 currency_code: "usd",
117 customer: {
118 groups: [{ id: "vip" }],
119 },
120 },
121 },
122 },
123 fields: [
124 "id",
125 "title",
126 "variants.title",
127 "variants.prices.amount",
128 "variants.prices.currency_code",
129 "variants.calculated_price.calculated_amount",
130 "variants.calculated_price.currency_code",
131 ],
132 })
133
134 const link = await remoteQuery(query)
135
136 expect(link).toHaveLength(1)
137 expect(link).toEqual(
138 expect.arrayContaining([
139 expect.objectContaining({
140 title: "Test product",
141 variants: expect.arrayContaining([
142 expect.objectContaining({
143 title: "Test variant",
144 prices: expect.arrayContaining([
145 expect.objectContaining({
146 amount: 5000,
147 currency_code: "eur",
148 }),
149 expect.objectContaining({
150 amount: 3000,
151 currency_code: "usd",
152 }),
153 ]),
154 calculated_price: {
155 calculated_amount: 3000,
156 currency_code: "usd",
157 },
158 }),
159 expect.objectContaining({
160 title: "Variant number 2",
161 prices: expect.arrayContaining([
162 expect.objectContaining({
163 amount: 400,
164 currency_code: "eur",
165 }),
166 expect.objectContaining({
167 amount: 500,
168 currency_code: "usd",
169 }),
170 expect.objectContaining({
171 amount: 100,
172 currency_code: "usd",
173 }),
174 ]),
175 calculated_price: {
176 calculated_amount: 100,
177 currency_code: "usd",
178 },
179 }),
180 ]),
181 }),
182 ])
183 )
184 })
185 })
186 },
187 })