1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { ModuleRegistrationName } from "@medusajs/utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7 import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
8 import { createOrderSeeder } from "../../fixtures/order"
9
10 jest.setTimeout(300000)
11
12 const env = { MEDUSA_FF_VIEW_CONFIGURATIONS: true }
13
14 medusaIntegrationTestRunner({
15 env,
16 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
17 beforeAll(async () => {
18 const container = getContainer()
19 await setupTaxStructure(container.resolve(ModuleRegistrationName.TAX))
20 await createAdminUser(dbConnection, adminHeaders, container)
21
22 await dbUtils.snapshot()
23 })
24
25 describe("GET /admin/views/:entity/columns", () => {
26 describe("orders entity", () => {
27 let order
28 let seeder
29
30 beforeEach(async () => {
31 // Create an order with all relationships
32 seeder = await createOrderSeeder({
33 api,
34 container: getContainer(),
35 })
36 order = seeder.order
37 })
38
39 it("should return all order columns including relationships", async () => {
40 const response = await api.get(
41 `/admin/views/orders/columns`,
42 adminHeaders
43 )
44
45 expect(response.status).toEqual(200)
46 expect(response.data.columns).toBeDefined()
47 expect(Array.isArray(response.data.columns)).toBe(true)
48
49 // Check for direct fields
50 const displayIdColumn = response.data.columns.find(
51 (c) => c.id === "display_id"
52 )
53 expect(displayIdColumn).toBeDefined()
54 expect(displayIdColumn).toMatchObject({
55 id: "display_id",
56 name: "Display Id",
57 field: "display_id",
58 data_type: "string",
59 semantic_type: "identifier",
60 context: "both",
61 })
62
63 // Check for missing relationships
64 const salesChannelColumns = response.data.columns.filter((c) =>
65 c.id.startsWith("sales_channel")
66 )
67
68 const orderExtraColumns = response.data.columns.filter((c) =>
69 c.id.startsWith("order_extra")
70 )
71
72 // Check for shipping address columns
73 const shippingAddressColumns = response.data.columns.filter((c) =>
74 c.id.startsWith("shipping_address")
75 )
76
77 // Check that we DON'T have the sales_channel relationship object
78 const salesChannelField = response.data.columns.find(
79 (c) => c.id === "sales_channel"
80 )
81 expect(salesChannelField).toBeUndefined()
82
83 // Check that sales_channel.name is marked as default visible
84 const salesChannelNameField = response.data.columns.find(
85 (c) => c.id === "sales_channel.name"
86 )
87 expect(salesChannelNameField).toBeDefined()
88 expect(salesChannelNameField).toMatchObject({
89 id: "sales_channel.name",
90 field: "sales_channel.name",
91 default_visible: true,
92 })
93
94 // Check that other sales_channel fields are NOT default visible
95 const salesChannelIdField = response.data.columns.find(
96 (c) => c.id === "sales_channel.id"
97 )
98 expect(salesChannelIdField).toBeDefined()
99 expect(salesChannelIdField.default_visible).toBe(false)
100
101 const salesChannelDescriptionField = response.data.columns.find(
102 (c) => c.id === "sales_channel.description"
103 )
104 if (salesChannelDescriptionField) {
105 expect(salesChannelDescriptionField.default_visible).toBe(false)
106 }
107
108 // Check that we have the customer_display computed column
109 const customerDisplayField = response.data.columns.find(
110 (c) => c.id === "customer_display"
111 )
112 expect(customerDisplayField).toBeDefined()
113 expect(customerDisplayField).toMatchObject({
114 id: "customer_display",
115 name: "Customer",
116 field: "customer_display",
117 data_type: "string",
118 semantic_type: "computed",
119 context: "display",
120 default_visible: true,
121 sortable: false,
122 computed: {
123 type: "name",
124 required_fields: [
125 "customer.first_name",
126 "customer.last_name",
127 "customer.email",
128 ],
129 optional_fields: ["customer.phone"],
130 },
131 default_order: 300,
132 category: "relationship",
133 })
134
135 // Check that we have the country computed column
136 const countryField = response.data.columns.find(
137 (c) => c.id === "order_shipping_country_display"
138 )
139 expect(countryField).toBeDefined()
140 expect(countryField).toMatchObject({
141 id: "order_shipping_country_display",
142 name: "Country",
143 field: "order_shipping_country_display",
144 data_type: "string",
145 semantic_type: "computed",
146 context: "display",
147 default_visible: true,
148 sortable: false,
149 computed: {
150 type: "country_code",
151 required_fields: ["shipping_address.country_code"],
152 optional_fields: [],
153 },
154 default_order: 800,
155 category: "metadata",
156 })
157
158 // Check that we DON'T have customer or shipping_address objects
159 const customerField = response.data.columns.find(
160 (c) => c.id === "customer"
161 )
162 expect(customerField).toBeUndefined()
163
164 const shippingAddressField = response.data.columns.find(
165 (c) => c.id === "shipping_address"
166 )
167 expect(shippingAddressField).toBeUndefined()
168
169 // Group columns by type
170 const directFields = response.data.columns.filter(
171 (c) => !c.id.includes(".")
172 )
173 const relationshipFields = response.data.columns.filter((c) =>
174 c.id.includes(".")
175 )
176
177 // Check that important fields have proper ordering
178 const displayIdField = response.data.columns.find(
179 (c) => c.id === "display_id"
180 )
181 expect(displayIdField?.default_order).toBe(100)
182 expect(displayIdField?.category).toBe("identifier")
183
184 const totalField = response.data.columns.find((c) => c.id === "total")
185 expect(totalField?.default_order).toBe(700)
186 expect(totalField?.category).toBe("metric")
187
188 const createdAtField = response.data.columns.find(
189 (c) => c.id === "created_at"
190 )
191 expect(createdAtField?.default_order).toBe(200)
192 expect(createdAtField?.category).toBe("timestamp")
193 })
194
195 it("should check filtering behavior", async () => {
196 const response = await api.get(
197 `/admin/views/orders/columns`,
198 adminHeaders
199 )
200
201 // Check which fields might be getting filtered
202 const allFieldIds = response.data.columns.map((c) => c.id)
203
204 // Check for fields that might be filtered by suffixes
205 const linkFields = allFieldIds.filter((id) => id.includes("_link"))
206 expect(linkFields).toHaveLength(0)
207 })
208 })
209
210 describe("products entity", () => {
211 it("should return product columns", async () => {
212 const response = await api.get(
213 `/admin/views/products/columns`,
214 adminHeaders
215 )
216
217 expect(response.status).toEqual(200)
218 expect(response.data.columns).toBeDefined()
219 expect(Array.isArray(response.data.columns)).toBe(true)
220
221 // Check for product-specific fields
222 const titleColumn = response.data.columns.find(
223 (c) => c.id === "title"
224 )
225 expect(titleColumn).toBeDefined()
226 expect(titleColumn).toMatchObject({
227 id: "title",
228 name: "Title",
229 field: "title",
230 default_visible: false,
231 })
232
233 const handleColumn = response.data.columns.find(
234 (c) => c.id === "handle"
235 )
236 expect(handleColumn).toBeDefined()
237 expect(handleColumn).toMatchObject({
238 id: "handle",
239 name: "Handle",
240 field: "handle",
241 default_visible: false,
242 })
243 })
244 })
245
246 describe("promotions entity", () => {
247 it("should include the campaign budget type in the status column", async () => {
248 const response = await api.get(
249 `/admin/views/promotions/columns`,
250 adminHeaders
251 )
252
253 const statusColumn = response.data.columns.find(
254 (c) => c.id === "status_display"
255 )
256
257 expect(statusColumn).toMatchObject({
258 computed: {
259 required_fields: [
260 "status",
261 "campaign.starts_at",
262 "campaign.ends_at",
263 "campaign.budget.type",
264 "campaign.budget.limit",
265 "campaign.budget.used",
266 ],
267 },
268 })
269 })
270 })
271
272 describe("unsupported entity", () => {
273 it("should return 400 for unsupported entity", async () => {
274 const response = await api
275 .get(`/admin/views/unsupported-entity/columns`, adminHeaders)
276 .catch((e) => e.response)
277
278 expect(response.status).toEqual(400)
279 expect(response.data.message).toContain("Unsupported entity")
280 })
281 })
282 })
283 },
284 })