From d6372246ee47e01d16d3858824986d970e035e2c Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Tue, 31 Oct 2023 16:20:55 +0100 Subject: [PATCH] fix(integration): Product ordered by variants prices (#5507) * fix(integration): Product ordered by variants prices * set cache to 0 to prevent issues between tests --- .../api/__tests__/store/products.js | 69 +++++++++++++++---- integration-tests/api/medusa-config.js | 2 +- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/integration-tests/api/__tests__/store/products.js b/integration-tests/api/__tests__/store/products.js index bb52f40a39..045cb3fcc3 100644 --- a/integration-tests/api/__tests__/store/products.js +++ b/integration-tests/api/__tests__/store/products.js @@ -15,6 +15,12 @@ const { defaultStoreProductsRelations, } = require("@medusajs/medusa/dist") +const adminHeaders = { + headers: { + "x-medusa-access-token": "test_token", + }, +} + jest.setTimeout(30000) describe("/store/products", () => { @@ -30,7 +36,12 @@ describe("/store/products", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) - dbConnection = await initDb({ cwd }) + dbConnection = await initDb({ + cwd, + env: { + CACHE_TTL: 0, + }, + }) medusaProcess = await setupServer({ cwd }) }) @@ -128,7 +139,7 @@ describe("/store/products", () => { const api = useApi() await simpleProductFactory(dbConnection, { - id: "test-product2", + id: testProductId2, status: "published", variants: [ { @@ -143,10 +154,29 @@ describe("/store/products", () => { ], }) - const response = await api.get( + let response = await api.get( "/store/products?order=-variants.prices.amount" ) + // Update amount to unsure order, same amount will add randomness in the result with the same amounts + const productToUpdate = response.data.products.find( + (p) => p.id === testProductId + ) + const priceToUpdate = productToUpdate.variants[0].prices[0] + const priceData = { + id: priceToUpdate.id, + currency_code: priceToUpdate.currency_code, + amount: 120, + } + + await api.post( + `/admin/products/${testProductId}/variants/${productToUpdate.variants[0].id}`, + { prices: [priceData] }, + adminHeaders + ) + + response = await api.get("/store/products?order=-variants.prices.amount") + expect(response.status).toEqual(200) expect(response.data.products).toHaveLength(6) @@ -161,7 +191,7 @@ describe("/store/products", () => { ) expect(testProduct2Index).toBe(3) // 200 - expect(testProductIndex).toBe(4) // 100 + expect(testProductIndex).toBe(4) // 120 expect(testProduct1Index).toBe(5) // 100 }) @@ -169,7 +199,7 @@ describe("/store/products", () => { const api = useApi() await simpleProductFactory(dbConnection, { - id: "test-product2", + id: testProductId2, status: "published", variants: [ { @@ -184,10 +214,29 @@ describe("/store/products", () => { ], }) - const response = await api.get( + let response = await api.get( "/store/products?order=variants.prices.amount" ) + // Update amount to unsure order, same amount will add randomness in the result with the same amounts + const productToUpdate = response.data.products.find( + (p) => p.id === testProductId1 + ) + const priceToUpdate = productToUpdate.variants[0].prices[0] + const priceData = { + id: priceToUpdate.id, + currency_code: priceToUpdate.currency_code, + amount: 120, + } + + await api.post( + `/admin/products/${testProductId1}/variants/${productToUpdate.variants[0].id}`, + { prices: [priceData] }, + adminHeaders + ) + + response = await api.get("/store/products?order=variants.prices.amount") + expect(response.status).toEqual(200) expect(response.data.products).toHaveLength(6) @@ -202,7 +251,7 @@ describe("/store/products", () => { ) expect(testProductIndex).toBe(0) // 100 - expect(testProduct1Index).toBe(1) // 100 + expect(testProduct1Index).toBe(1) // 120 expect(testProduct2Index).toBe(2) // 200 }) @@ -971,11 +1020,7 @@ describe("/store/products", () => { { status: "published", }, - { - headers: { - "x-medusa-access-token": "test_token", - }, - } + adminHeaders ) .catch((err) => { console.log(err) diff --git a/integration-tests/api/medusa-config.js b/integration-tests/api/medusa-config.js index 527900350a..3438fc82cc 100644 --- a/integration-tests/api/medusa-config.js +++ b/integration-tests/api/medusa-config.js @@ -4,7 +4,7 @@ const DB_PASSWORD = process.env.DB_PASSWORD const DB_NAME = process.env.DB_TEMP_NAME const redisUrl = process.env.REDIS_URL || "redis://localhost:6379" -const cacheTTL = process.env.CACHE_TTL || 15 +const cacheTTL = process.env.CACHE_TTL ?? 15 const enableResponseCompression = process.env.ENABLE_RESPONSE_COMPRESSION || true