Files
medusa-store/integration-tests/api/__tests__/admin/stock-location/index.spec.ts
Philip Korsholm 18438a695a feat(medusa, stock-location-next): add list-stock-locations endpoint to api-v2 (#6788)
* initial create

* add list for stock locations

* add changeset

* redo changes for stock locatino module'

* add changeset

* naming

* prep for pr

* move integration tests

* fix pr feedback

* add changeset

* update changeset

---------

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
2024-03-28 17:33:26 +01:00

284 lines
7.9 KiB
TypeScript

import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import {
adminHeaders,
createAdminUser,
} from "../../../../helpers/create-admin-user"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { IStockLocationServiceNext } from "@medusajs/types"
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
beforeEach(async () => {
appContainer = 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(
`/admin/stock-locations`,
{
name: "Test Location 1",
address: {
address_1: "Test Address",
country_code: "US",
},
},
adminHeaders
)
location1 = location1CreateResponse.data.stock_location
const location2CreateResponse = await api.post(
`/admin/stock-locations`,
{
name: "Test Location 2",
address: {
address_1: "Test Address",
country_code: "US",
},
},
adminHeaders
)
location2 = location2CreateResponse.data.stock_location
})
it("should list stock locations", async () => {
const listLocationsResponse = await api.get(
"/admin/stock-locations",
adminHeaders
)
expect(listLocationsResponse.status).toEqual(200)
expect(listLocationsResponse.data.stock_locations).toEqual([
expect.objectContaining(location1),
expect.objectContaining(location2),
])
})
it("should filter stock locations by name", async () => {
const listLocationsResponse = await api.get(
"/admin/stock-locations?name=Test%20Location%201",
adminHeaders
)
expect(listLocationsResponse.status).toEqual(200)
expect(listLocationsResponse.data.stock_locations).toEqual([
expect.objectContaining(location1),
])
})
it("should filter stock locations by partial name with q parameter", async () => {
const listLocationsResponse = await api.get(
"/admin/stock-locations?q=ation%201",
adminHeaders
)
expect(listLocationsResponse.status).toEqual(200)
expect(listLocationsResponse.data.stock_locations).toEqual([
expect.objectContaining(location1),
])
})
it("should filter stock locations on sales_channel_id", async () => {
const remoteLinkService = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
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",
adminHeaders
)
expect(listLocationsResponse.status).toEqual(200)
expect(listLocationsResponse.data.stock_locations).toEqual([
expect.objectContaining(location1),
])
})
})
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}`,
{
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}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.stock_location).toEqual(
expect.objectContaining({ id: locationId, ...location })
)
})
it("should get a stock location", async () => {
let error
await api
.get(`/admin/stock-locations/does-not-exist`, adminHeaders)
.catch((e) => (error = e))
expect(error.response.status).toEqual(404)
expect(error.response.data.message).toEqual(
`Stock location with id: does-not-exist was not found`
)
})
})
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}`,
adminHeaders
)
expect(stockLocationDeleteResponse.status).toEqual(200)
expect(stockLocationDeleteResponse.data).toEqual({
id: stockLocationId,
object: "stock_location",
deleted: true,
})
})
it("should successfully delete stock location associations", async () => {
const remoteLink = appContainer.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
await remoteLink.create([
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "default",
},
[Modules.STOCK_LOCATION]: {
stock_location_id: stockLocationId,
},
},
])
await api.delete(
`/admin/stock-locations/${stockLocationId}`,
adminHeaders
)
const linkService = remoteLink.getLinkModule(
Modules.SALES_CHANNEL,
"sales_channel_id",
Modules.STOCK_LOCATION,
"stock_location_id"
)
const stockLocationLinks = await linkService.list()
expect(stockLocationLinks).toHaveLength(0)
})
})
},
})