From b2f2c366ecf53ea32857af1298a7d9442e913929 Mon Sep 17 00:00:00 2001 From: Stevche Radevski Date: Wed, 5 Jun 2024 12:37:14 +0200 Subject: [PATCH] chore: Move stock location tests to HTTP folder (#7615) --- .../inventory/admin/inventory.spec.ts | 2 + .../admin/stock-location.spec.ts} | 327 ++++++------------ 2 files changed, 104 insertions(+), 225 deletions(-) rename integration-tests/{api/__tests__/admin/stock-location/index.spec.ts => http/__tests__/stock-location/admin/stock-location.spec.ts} (53%) diff --git a/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts b/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts index 3106334e3f..2f5ea6de88 100644 --- a/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts +++ b/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts @@ -5,6 +5,8 @@ import { import { medusaIntegrationTestRunner } from "medusa-test-utils" import { getProductFixture } from "../../../../helpers/fixtures" +jest.setTimeout(30000) + medusaIntegrationTestRunner({ testSuite: ({ dbConnection, getContainer, api }) => { let inventoryItem1 diff --git a/integration-tests/api/__tests__/admin/stock-location/index.spec.ts b/integration-tests/http/__tests__/stock-location/admin/stock-location.spec.ts similarity index 53% rename from integration-tests/api/__tests__/admin/stock-location/index.spec.ts rename to integration-tests/http/__tests__/stock-location/admin/stock-location.spec.ts index b54275a10a..7be10f0788 100644 --- a/integration-tests/api/__tests__/admin/stock-location/index.spec.ts +++ b/integration-tests/http/__tests__/stock-location/admin/stock-location.spec.ts @@ -1,66 +1,25 @@ -import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk" +import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { adminHeaders, createAdminUser, } from "../../../../helpers/create-admin-user" -import { IStockLocationServiceNext } from "@medusajs/types" -import { ContainerRegistrationKeys } from "@medusajs/utils" - const { medusaIntegrationTestRunner } = require("medusa-test-utils") jest.setTimeout(30000) medusaIntegrationTestRunner({ - env: { - MEDUSA_FF_MEDUSA_V2: true, - }, testSuite: ({ dbConnection, getContainer, api }) => { - let appContainer - let service: IStockLocationServiceNext + let location1 + let location2 + let salesChannel1 + let salesChannel2 beforeEach(async () => { - appContainer = getContainer() + await createAdminUser(dbConnection, adminHeaders, getContainer()) - await createAdminUser(dbConnection, adminHeaders, appContainer) - - service = appContainer.resolve(ModuleRegistrationName.STOCK_LOCATION) - }) - - describe("create stock location", () => { - it("should create a stock location with a name and address", async () => { - const address = { - address_1: "Test Address", - country_code: "US", - } - const location = { - name: "Test Location", - } - - const response = await api.post( - "/admin/stock-locations", - { - ...location, - address, - }, - adminHeaders - ) - - expect(response.status).toEqual(200) - expect(response.data.stock_location).toEqual( - expect.objectContaining({ - ...location, - address: expect.objectContaining(address), - }) - ) - }) - }) - - describe("list stock locations", () => { - let location1 - let location2 - beforeEach(async () => { - const location1CreateResponse = await api.post( + location1 = ( + await api.post( `/admin/stock-locations`, { name: "Test Location 1", @@ -71,8 +30,10 @@ medusaIntegrationTestRunner({ }, adminHeaders ) - location1 = location1CreateResponse.data.stock_location - const location2CreateResponse = await api.post( + ).data.stock_location + + location2 = ( + await api.post( `/admin/stock-locations`, { name: "Test Location 2", @@ -83,9 +44,57 @@ medusaIntegrationTestRunner({ }, adminHeaders ) - location2 = location2CreateResponse.data.stock_location - }) + ).data.stock_location + salesChannel1 = ( + await api.post( + `/admin/sales-channels`, + { + name: "Test SC", + }, + adminHeaders + ) + ).data.sales_channel + + salesChannel2 = ( + await api.post( + `/admin/sales-channels`, + { + name: "Test SC 2", + }, + adminHeaders + ) + ).data.sales_channel + }) + + describe("create stock location", () => { + it("should create a stock location with a name and address", async () => { + const response = await api.post( + "/admin/stock-locations", + { + name: "Test Location", + address: { + address_1: "Test Address", + country_code: "US", + }, + }, + adminHeaders + ) + + expect(response.status).toEqual(200) + expect(response.data.stock_location).toEqual( + expect.objectContaining({ + name: "Test Location", + address: expect.objectContaining({ + address_1: "Test Address", + country_code: "US", + }), + }) + ) + }) + }) + + describe("list stock locations", () => { it("should list stock locations", async () => { const listLocationsResponse = await api.get( "/admin/stock-locations", @@ -124,23 +133,16 @@ medusaIntegrationTestRunner({ }) it("should filter stock locations on sales_channel_id", async () => { - const remoteLinkService = appContainer.resolve( - ContainerRegistrationKeys.REMOTE_LINK + await api.post( + `/admin/stock-locations/${location1.id}/sales-channels`, + { + add: [salesChannel1.id], + }, + adminHeaders ) - await remoteLinkService.create([ - { - [Modules.SALES_CHANNEL]: { - sales_channel_id: "default", - }, - [Modules.STOCK_LOCATION]: { - stock_location_id: location1.id, - }, - }, - ]) - const listLocationsResponse = await api.get( - "/admin/stock-locations?sales_channel_id=default", + `/admin/stock-locations?sales_channel_id=${salesChannel1.id}`, adminHeaders ) @@ -152,63 +154,33 @@ medusaIntegrationTestRunner({ }) describe("Update stock locations", () => { - let stockLocationId - - beforeEach(async () => { - const createResponse = await api.post( - `/admin/stock-locations`, - { - name: "test location", - }, - adminHeaders - ) - - stockLocationId = createResponse.data.stock_location.id - }) - it("should update stock location name", async () => { const response = await api.post( - `/admin/stock-locations/${stockLocationId}`, + `/admin/stock-locations/${location1.id}`, { name: "new name", }, adminHeaders ) expect(response.status).toEqual(200) - expect(response.data.stock_location.name).toEqual("new name") }) }) describe("Get stock location", () => { - let locationId - const location = { - name: "Test Location", - } - beforeEach(async () => { - const createLocationRespones = await api.post( - "/admin/stock-locations", - { - ...location, - }, - adminHeaders - ) - locationId = createLocationRespones.data.stock_location.id - }) - it("should get a stock location", async () => { const response = await api.get( - `/admin/stock-locations/${locationId}`, + `/admin/stock-locations/${location1.id}`, adminHeaders ) expect(response.status).toEqual(200) expect(response.data.stock_location).toEqual( - expect.objectContaining({ id: locationId, ...location }) + expect.objectContaining({ id: location1.id, name: "Test Location 1" }) ) }) - it("should get a stock location", async () => { + it("should throw an error when a stock location does not exist", async () => { let error await api .get(`/admin/stock-locations/does-not-exist`, adminHeaders) @@ -222,26 +194,15 @@ medusaIntegrationTestRunner({ }) describe("Delete stock location", () => { - let stockLocationId - beforeEach(async () => { - const stockLocationCreateResponse = await api.post( - `/admin/stock-locations`, - { name: "test location" }, - adminHeaders - ) - - stockLocationId = stockLocationCreateResponse.data.stock_location.id - }) - it("should successfully delete stock location", async () => { const stockLocationDeleteResponse = await api.delete( - `/admin/stock-locations/${stockLocationId}`, + `/admin/stock-locations/${location1.id}`, adminHeaders ) expect(stockLocationDeleteResponse.status).toEqual(200) expect(stockLocationDeleteResponse.data).toEqual({ - id: stockLocationId, + id: location1.id, object: "stock_location", deleted: true, }) @@ -252,72 +213,38 @@ medusaIntegrationTestRunner({ ) expect(stockLocations.status).toEqual(200) - expect(stockLocations.data.stock_locations).toEqual([]) + expect(stockLocations.data.stock_locations).toEqual([ + expect.objectContaining(location2), + ]) }) it("should successfully delete stock location associations", async () => { - const remoteLink = appContainer.resolve( - ContainerRegistrationKeys.REMOTE_LINK - ) - - await remoteLink.create([ + await api.post( + `/admin/stock-locations/${location1.id}/sales-channels`, { - [Modules.SALES_CHANNEL]: { - sales_channel_id: "default", - }, - [Modules.STOCK_LOCATION]: { - stock_location_id: stockLocationId, - }, + add: [salesChannel1.id], }, - ]) - - await api.delete( - `/admin/stock-locations/${stockLocationId}`, adminHeaders ) - const linkService = remoteLink.getLinkModule( - Modules.SALES_CHANNEL, - "sales_channel_id", - Modules.STOCK_LOCATION, - "stock_location_id" - ) + await api.delete(`/admin/stock-locations/${location1.id}`, adminHeaders) - const stockLocationLinks = await linkService.list() - expect(stockLocationLinks).toHaveLength(0) + const scWithAssociation = ( + await api.get( + `/admin/sales-channels/${salesChannel1.id}?fields=*stock_locations`, + adminHeaders + ) + ).data.sales_channel + + expect(scWithAssociation.stock_locations).toEqual([]) }) }) describe("Add sales channels", () => { - let salesChannel - let location - - beforeEach(async () => { - const salesChannelResponse = await api.post( - "/admin/sales-channels", - { - name: "test name", - description: "test description", - }, - adminHeaders - ) - salesChannel = salesChannelResponse.data.sales_channel - - const locationResponse = await api.post( - "/admin/stock-locations", - { - name: "test location", - }, - adminHeaders - ) - - location = locationResponse.data.stock_location - }) - it("should add sales channels to a location", async () => { const salesChannelResponse = await api.post( - `/admin/stock-locations/${location.id}/sales-channels?fields=*sales_channels`, - { add: [salesChannel.id] }, + `/admin/stock-locations/${location1.id}/sales-channels?fields=*sales_channels`, + { add: [salesChannel1.id] }, adminHeaders ) @@ -328,43 +255,9 @@ medusaIntegrationTestRunner({ }) describe("Remove sales channels", () => { - let salesChannel1 - let salesChannel2 - let location - beforeEach(async () => { - const salesChannelResponse1 = await api.post( - "/admin/sales-channels", - { - name: "test name", - description: "test description", - }, - adminHeaders - ) - salesChannel1 = salesChannelResponse1.data.sales_channel - - const salesChannelResponse2 = await api.post( - "/admin/sales-channels", - { - name: "test name", - description: "test description", - }, - adminHeaders - ) - salesChannel2 = salesChannelResponse2.data.sales_channel - - const locationResponse = await api.post( - "/admin/stock-locations", - { - name: "test location", - }, - adminHeaders - ) - - location = locationResponse.data.stock_location - await api.post( - `/admin/stock-locations/${location.id}/sales-channels?fields=*sales_channels`, + `/admin/stock-locations/${location1.id}/sales-channels?fields=*sales_channels`, { add: [salesChannel1.id, salesChannel2.id] }, adminHeaders ) @@ -372,7 +265,7 @@ medusaIntegrationTestRunner({ it("should remove sales channels from a location", async () => { const salesChannelResponse = await api.post( - `/admin/stock-locations/${location.id}/sales-channels?fields=*sales_channels`, + `/admin/stock-locations/${location1.id}/sales-channels?fields=*sales_channels`, { remove: [salesChannel1.id] }, adminHeaders ) @@ -384,23 +277,9 @@ medusaIntegrationTestRunner({ }) describe("Location fulfillment sets", () => { - let stockLocationId - - beforeEach(async () => { - const createResponse = await api.post( - `/admin/stock-locations`, - { - name: "test location", - }, - adminHeaders - ) - - stockLocationId = createResponse.data.stock_location.id - }) - it("should create a fulfillment set for the location and then delete it and its associations", async () => { const response = await api.post( - `/admin/stock-locations/${stockLocationId}/fulfillment-sets?fields=id,*fulfillment_sets`, + `/admin/stock-locations/${location1.id}/fulfillment-sets?fields=id,*fulfillment_sets`, { name: "Fulfillment Set", type: "shipping", @@ -416,17 +295,13 @@ medusaIntegrationTestRunner({ }), ]) - await api.delete( - `/admin/stock-locations/${stockLocationId}`, - adminHeaders - ) + await api.delete(`/admin/stock-locations/${location1.id}`, adminHeaders) - const fulfillmentModule = appContainer.resolve( + // TODO: Ideally we use HTTP here, maybe we should have a get endpoint for fulfillment sets? + const fulfillmentModule = getContainer().resolve( ModuleRegistrationName.FULFILLMENT ) - const sets = await fulfillmentModule.list() - expect(sets).toHaveLength(0) }) @@ -434,7 +309,7 @@ medusaIntegrationTestRunner({ it("should throw a validation error on wrong input", async () => { const errorResponse = await api .post( - `/admin/stock-locations/${stockLocationId}/fulfillment-sets?fields=id,*fulfillment_sets`, + `/admin/stock-locations/${location1.id}/fulfillment-sets?fields=id,*fulfillment_sets`, { name: "Fulfillment Set", type: "shipping", @@ -446,7 +321,9 @@ medusaIntegrationTestRunner({ expect(errorResponse.status).toEqual(400) - expect(errorResponse.data.message).toContain("Invalid request body: ") + expect(errorResponse.data.message).toEqual( + "Invalid request: Unrecognized fields: 'foo'" + ) }) }) },