diff --git a/integration-tests/api/__tests__/admin/__snapshots__/product.js.snap b/integration-tests/api/__tests__/admin/__snapshots__/product.js.snap new file mode 100644 index 0000000000..830f17fc33 --- /dev/null +++ b/integration-tests/api/__tests__/admin/__snapshots__/product.js.snap @@ -0,0 +1,387 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`/admin/products GET /admin/products returns a list of products with child entities 1`] = ` +Array [ + Object { + "collection": Object { + "created_at": Any, + "deleted_at": null, + "handle": "test-collection", + "id": StringMatching /\\^test-\\*/, + "metadata": null, + "title": "Test collection", + "updated_at": Any, + }, + "collection_id": "test-collection", + "created_at": Any, + "deleted_at": null, + "description": "test-product-description", + "discountable": true, + "handle": "test-product", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-\\*/, + "images": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-\\*/, + "metadata": null, + "updated_at": Any, + "url": "test-image.png", + }, + ], + "is_giftcard": false, + "length": null, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-\\*/, + "metadata": null, + "product_id": StringMatching /\\^test-\\*/, + "title": "test-option", + "updated_at": Any, + }, + ], + "origin_country": null, + "profile_id": StringMatching /\\^sp_\\*/, + "subtitle": null, + "tags": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^tag\\*/, + "metadata": null, + "updated_at": Any, + "value": "123", + }, + ], + "thumbnail": null, + "title": "Test product", + "type": Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-\\*/, + "metadata": null, + "updated_at": Any, + "value": "test-type", + }, + "type_id": "test-type", + "updated_at": Any, + "variants": Array [ + Object { + "allow_backorder": false, + "barcode": "test-barcode", + "created_at": Any, + "deleted_at": null, + "ean": "test-ean", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-variant\\*/, + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-variant-option\\*/, + "metadata": null, + "option_id": StringMatching /\\^test-opt\\*/, + "updated_at": Any, + "value": "Default variant", + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": StringMatching /\\^test-price\\*/, + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "product_id": StringMatching /\\^test-\\*/, + "sku": "test-sku", + "title": "Test variant", + "upc": "test-upc", + "updated_at": Any, + "variant_rank": 0, + "weight": null, + "width": null, + }, + Object { + "allow_backorder": false, + "barcode": null, + "created_at": Any, + "deleted_at": null, + "ean": "test-ean2", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-variant\\*/, + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-variant-option\\*/, + "metadata": null, + "option_id": StringMatching /\\^test-opt\\*/, + "updated_at": Any, + "value": "Default variant 2", + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": StringMatching /\\^test-price\\*/, + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "product_id": StringMatching /\\^test-\\*/, + "sku": "test-sku2", + "title": "Test variant rank (2)", + "upc": "test-upc2", + "updated_at": Any, + "variant_rank": 1, + "weight": null, + "width": null, + }, + Object { + "allow_backorder": false, + "barcode": "test-barcode 1", + "created_at": Any, + "deleted_at": null, + "ean": "test-ean1", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-variant\\*/, + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-variant-option\\*/, + "metadata": null, + "option_id": StringMatching /\\^test-opt\\*/, + "updated_at": Any, + "value": "Default variant 1", + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": StringMatching /\\^test-price\\*/, + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "product_id": StringMatching /\\^test-\\*/, + "sku": "test-sku1", + "title": "Test variant rank (1)", + "upc": "test-upc1", + "updated_at": Any, + "variant_rank": 2, + "weight": null, + "width": null, + }, + ], + "weight": null, + "width": null, + }, + Object { + "collection": Object { + "created_at": Any, + "deleted_at": null, + "handle": "test-collection", + "id": StringMatching /\\^test-\\*/, + "metadata": null, + "title": "Test collection", + "updated_at": Any, + }, + "collection_id": "test-collection", + "created_at": Any, + "deleted_at": null, + "description": "test-product-description1", + "discountable": true, + "handle": "test-product1", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-\\*/, + "images": Array [], + "is_giftcard": false, + "length": null, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [], + "origin_country": null, + "profile_id": StringMatching /\\^sp_\\*/, + "subtitle": null, + "tags": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^tag\\*/, + "metadata": null, + "updated_at": Any, + "value": "123", + }, + ], + "thumbnail": null, + "title": "Test product1", + "type": Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-\\*/, + "metadata": null, + "updated_at": Any, + "value": "test-type", + }, + "type_id": "test-type", + "updated_at": Any, + "variants": Array [ + Object { + "allow_backorder": false, + "barcode": null, + "created_at": Any, + "deleted_at": null, + "ean": "test-ean4", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-variant\\*/, + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-variant-option\\*/, + "metadata": null, + "option_id": StringMatching /\\^test-opt\\*/, + "updated_at": Any, + "value": "Default variant 3", + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": StringMatching /\\^test-price\\*/, + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "product_id": StringMatching /\\^test-\\*/, + "sku": "test-sku4", + "title": "Test variant rank (2)", + "upc": "test-upc4", + "updated_at": Any, + "variant_rank": 0, + "weight": null, + "width": null, + }, + Object { + "allow_backorder": false, + "barcode": null, + "created_at": Any, + "deleted_at": null, + "ean": "test-ean3", + "height": null, + "hs_code": null, + "id": StringMatching /\\^test-variant\\*/, + "inventory_quantity": 10, + "length": null, + "manage_inventory": true, + "material": null, + "metadata": null, + "mid_code": null, + "options": Array [ + Object { + "created_at": Any, + "deleted_at": null, + "id": StringMatching /\\^test-variant-option\\*/, + "metadata": null, + "option_id": StringMatching /\\^test-opt\\*/, + "updated_at": Any, + "value": "Default variant 3", + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "origin_country": null, + "prices": Array [ + Object { + "amount": 100, + "created_at": Any, + "currency_code": "usd", + "deleted_at": null, + "id": StringMatching /\\^test-price\\*/, + "region_id": null, + "sale_amount": null, + "updated_at": Any, + "variant_id": StringMatching /\\^test-variant\\*/, + }, + ], + "product_id": StringMatching /\\^test-\\*/, + "sku": "test-sku3", + "title": "Test variant rank (2)", + "upc": "test-upc3", + "updated_at": Any, + "variant_rank": 1, + "weight": null, + "width": null, + }, + ], + "weight": null, + "width": null, + }, +] +`; diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 6d49e86fed..a63f821bd2 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -16,7 +16,7 @@ describe("/admin/products", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - medusaProcess = await setupServer({ cwd }) + medusaProcess = await setupServer({ cwd, verbose: true }) }) afterAll(async () => { @@ -26,6 +26,237 @@ describe("/admin/products", () => { medusaProcess.kill() }) + describe("GET /admin/products", () => { + beforeEach(async () => { + try { + await productSeeder(dbConnection) + await adminSeeder(dbConnection) + } catch (err) { + console.log(err) + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it.only("returns a list of products with child entities", async () => { + const api = useApi() + + const response = await api + .get("/admin/products", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + const testProduct = response.data.products[0] + + console.log(JSON.stringify(testProduct, null, 2)) + + const testProduct1 = response.data.products[1] + + console.log(JSON.stringify(testProduct1, null, 2)) + // console.log(testProduct.variants) + + expect(response.data.products).toMatchSnapshot([ + { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + options: [ + { + id: expect.stringMatching(/^test-*/), + product_id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + images: [ + { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + variants: [ + { + id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + variant_rank: 0, + product_id: expect.stringMatching(/^test-*/), + prices: [ + { + id: expect.stringMatching(/^test-price*/), + variant_id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + options: [ + { + id: expect.stringMatching(/^test-variant-option*/), + variant_id: expect.stringMatching(/^test-variant*/), + option_id: expect.stringMatching(/^test-opt*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + }, + { + id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + variant_rank: 1, + product_id: expect.stringMatching(/^test-*/), + prices: [ + { + id: expect.stringMatching(/^test-price*/), + variant_id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + options: [ + { + id: expect.stringMatching(/^test-variant-option*/), + variant_id: expect.stringMatching(/^test-variant*/), + option_id: expect.stringMatching(/^test-opt*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + }, + { + id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + variant_rank: 2, + product_id: expect.stringMatching(/^test-*/), + prices: [ + { + id: expect.stringMatching(/^test-price*/), + variant_id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + options: [ + { + id: expect.stringMatching(/^test-variant-option*/), + variant_id: expect.stringMatching(/^test-variant*/), + option_id: expect.stringMatching(/^test-opt*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + }, + ], + tags: [ + { + id: expect.stringMatching(/^tag*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + type: { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + collection: { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + profile_id: expect.stringMatching(/^sp_*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + options: [], + variants: [ + { + id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + variant_rank: 0, + product_id: expect.stringMatching(/^test-*/), + prices: [ + { + id: expect.stringMatching(/^test-price*/), + variant_id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + options: [ + { + id: expect.stringMatching(/^test-variant-option*/), + variant_id: expect.stringMatching(/^test-variant*/), + option_id: expect.stringMatching(/^test-opt*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + }, + { + id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + variant_rank: 1, + product_id: expect.stringMatching(/^test-*/), + prices: [ + { + id: expect.stringMatching(/^test-price*/), + variant_id: expect.stringMatching(/^test-variant*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + options: [ + { + id: expect.stringMatching(/^test-variant-option*/), + variant_id: expect.stringMatching(/^test-variant*/), + option_id: expect.stringMatching(/^test-opt*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + }, + ], + tags: [ + { + id: expect.stringMatching(/^tag*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ], + type: { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + collection: { + id: expect.stringMatching(/^test-*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + profile_id: expect.stringMatching(/^sp_*/), + created_at: expect.any(String), + updated_at: expect.any(String), + }, + ]) + }) + }) + describe("POST /admin/products", () => { beforeEach(async () => { try { diff --git a/integration-tests/api/helpers/product-seeder.js b/integration-tests/api/helpers/product-seeder.js index 60626ce68b..cc69255038 100644 --- a/integration-tests/api/helpers/product-seeder.js +++ b/integration-tests/api/helpers/product-seeder.js @@ -141,4 +141,62 @@ module.exports = async (connection, data = {}) => { }) await manager.save(variant3) + + const p1 = manager.create(Product, { + id: "test-product1", + handle: "test-product1", + title: "Test product1", + profile_id: defaultProfile.id, + description: "test-product-description1", + collection_id: "test-collection", + type: { id: "test-type", value: "test-type" }, + tags: [ + { id: "tag1", value: "123" }, + { tag: "tag2", value: "456" }, + ], + }) + + await manager.save(p1) + + const variant4 = await manager.create(ProductVariant, { + id: "test-variant_3", + inventory_quantity: 10, + title: "Test variant rank (2)", + variant_rank: 1, + sku: "test-sku3", + ean: "test-ean3", + upc: "test-upc3", + product_id: "test-product1", + prices: [{ id: "test-price3", currency_code: "usd", amount: 100 }], + options: [ + { + id: "test-variant-option-3", + value: "Default variant 3", + option_id: "test-option", + }, + ], + }) + + await manager.save(variant4) + + const variant5 = await manager.create(ProductVariant, { + id: "test-variant_4", + inventory_quantity: 10, + title: "Test variant rank (2)", + variant_rank: 0, + sku: "test-sku4", + ean: "test-ean4", + upc: "test-upc4", + product_id: "test-product1", + prices: [{ id: "test-price4", currency_code: "usd", amount: 100 }], + options: [ + { + id: "test-variant-option-4", + value: "Default variant 3", + option_id: "test-option", + }, + ], + }) + + await manager.save(variant5) }