|
| 1 | +import { medusaIntegrationTestRunner } from '@medusajs/test-utils'; |
| 2 | +import { IProductModuleService, MedusaContainer } from '@medusajs/framework/types'; |
| 3 | +import { Modules, ProductStatus } from '@medusajs/framework/utils'; |
| 4 | +import { createProductsWorkflow } from '@medusajs/medusa/core-flows'; |
| 5 | + |
| 6 | +const SEARCH_PATH = '/store/search'; |
| 7 | + |
| 8 | +medusaIntegrationTestRunner({ |
| 9 | + inApp: true, |
| 10 | + testSuite: ({ api, getContainer }) => { |
| 11 | + describe('Store Search Price Range Filter', () => { |
| 12 | + let container: MedusaContainer; |
| 13 | + |
| 14 | + beforeAll(async () => { |
| 15 | + container = getContainer(); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + const productModuleService = container.resolve<IProductModuleService>(Modules.PRODUCT); |
| 20 | + |
| 21 | + // Czyścimy i tworzymy produkty testowe o różnych cenach |
| 22 | + await createProductsWorkflow(container).run({ |
| 23 | + input: { |
| 24 | + products: [ |
| 25 | + { |
| 26 | + title: 'Tani Produkt', |
| 27 | + status: ProductStatus.PUBLISHED, |
| 28 | + options: [{ title: 'Size', values: ['S'] }], |
| 29 | + variants: [ |
| 30 | + { |
| 31 | + title: 'S', |
| 32 | + sku: 'CHEAP-1', |
| 33 | + prices: [{ currency_code: 'eur', amount: 100 }] // 1.00 EUR |
| 34 | + } |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + title: 'Średni Produkt', |
| 39 | + status: ProductStatus.PUBLISHED, |
| 40 | + options: [{ title: 'Size', values: ['M'] }], |
| 41 | + variants: [ |
| 42 | + { |
| 43 | + title: 'M', |
| 44 | + sku: 'MEDIUM-1', |
| 45 | + prices: [{ currency_code: 'eur', amount: 500 }] // 5.00 EUR |
| 46 | + } |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + title: 'Drogi Produkt', |
| 51 | + status: ProductStatus.PUBLISHED, |
| 52 | + options: [{ title: 'Size', values: ['L'] }], |
| 53 | + variants: [ |
| 54 | + { |
| 55 | + title: 'L', |
| 56 | + sku: 'EXPENSIVE-1', |
| 57 | + prices: [{ currency_code: 'eur', amount: 1000 }] // 10.00 EUR |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | + ] |
| 62 | + } |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should return all products when no price filters are applied', async () => { |
| 67 | + const response = await api.get(SEARCH_PATH); |
| 68 | + expect(response.status).toEqual(200); |
| 69 | + expect(response.data.products.length).toBeGreaterThanOrEqual(3); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should filter products by min_price', async () => { |
| 73 | + // Produkty droższe lub równe 500 (Średni i Drogi) |
| 74 | + const response = await api.get(`${SEARCH_PATH}?min_price=500`); |
| 75 | + |
| 76 | + expect(response.status).toEqual(200); |
| 77 | + const titles = response.data.products.map((p) => p.title); |
| 78 | + expect(titles).toContain('Średni Produkt'); |
| 79 | + expect(titles).toContain('Drogi Produkt'); |
| 80 | + expect(titles).not.toContain('Tani Produkt'); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should filter products by max_price', async () => { |
| 84 | + // Produkty tańsze lub równe 500 (Tani i Średni) |
| 85 | + const response = await api.get(`${SEARCH_PATH}?max_price=500`); |
| 86 | + |
| 87 | + expect(response.status).toEqual(200); |
| 88 | + const titles = response.data.products.map((p) => p.title); |
| 89 | + expect(titles).toContain('Tani Produkt'); |
| 90 | + expect(titles).toContain('Średni Produkt'); |
| 91 | + expect(titles).not.toContain('Drogi Produkt'); |
| 92 | + }); |
| 93 | + |
| 94 | + test('should filter products by price range (min and max)', async () => { |
| 95 | + // Tylko Średni Produkt (cena 500) |
| 96 | + const response = await api.get(`${SEARCH_PATH}?min_price=400&max_price=600`); |
| 97 | + |
| 98 | + expect(response.status).toEqual(200); |
| 99 | + expect(response.data.products.length).toEqual(1); |
| 100 | + expect(response.data.products[0].title).toEqual('Średni Produkt'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('should return empty list when no products match price range', async () => { |
| 104 | + const response = await api.get(`${SEARCH_PATH}?min_price=2000`); |
| 105 | + |
| 106 | + expect(response.status).toEqual(200); |
| 107 | + expect(response.data.products.length).toEqual(0); |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +jest.setTimeout(120 * 1000); |
0 commit comments