Files
medusa-store/integration-tests/plugins/__tests__/stock-location/service.js
2023-02-06 09:02:43 -03:00

272 lines
6.8 KiB
JavaScript

const path = require("path")
const { bootstrapApp } = require("../../../helpers/bootstrap-app")
const { initDb, useDb } = require("../../../helpers/use-db")
jest.setTimeout(30000)
describe("Inventory Module", () => {
let appContainer
let dbConnection
let express
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
const { container, app, port } = await bootstrapApp({ cwd })
appContainer = container
express = app.listen(port, (err) => {
process.send(port)
})
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
express.close()
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
describe("Stock Location Module Interface", () => {
it("create", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
expect(
await stockLocationService.create({
name: "first location",
})
).toEqual(
expect.objectContaining({
id: expect.any(String),
name: "first location",
deleted_at: null,
address_id: null,
metadata: null,
created_at: expect.any(Date),
updated_at: expect.any(Date),
})
)
expect(
await stockLocationService.create({
name: "second location",
metadata: {
extra: "abc",
},
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
).toEqual(
expect.objectContaining({
id: expect.any(String),
name: "second location",
metadata: {
extra: "abc",
},
address_id: expect.any(String),
})
)
})
it("update", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
const loc = await stockLocationService.create({
name: "location",
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
const addressId = loc.address_id
expect(
await stockLocationService.retrieve(loc.id, {
relations: ["address"],
})
).toEqual(
expect.objectContaining({
id: loc.id,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
name: "location",
address_id: addressId,
metadata: null,
address: expect.objectContaining({
id: addressId,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
address_1: "addr_1",
address_2: "line 2",
company: null,
city: "city",
country_code: "DK",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: { abc: 123 },
}),
})
)
expect(
await stockLocationService.update(loc.id, {
name: "location name",
address_id: addressId,
address: {
address_1: "addr_1 updated",
country_code: "US",
},
})
).toEqual(
expect.objectContaining({
id: loc.id,
name: "location name",
address_id: addressId,
})
)
expect(
await stockLocationService.retrieve(loc.id, {
relations: ["address"],
})
).toEqual(
expect.objectContaining({
id: loc.id,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
name: "location name",
address_id: addressId,
metadata: null,
address: expect.objectContaining({
id: addressId,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
address_1: "addr_1 updated",
address_2: "line 2",
company: null,
city: "city",
country_code: "US",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: { abc: 123 },
}),
})
)
})
it("updateAddress", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
const loc = await stockLocationService.create({
name: "location",
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
const addressId = loc.address_id
expect(
await stockLocationService.updateAddress(addressId, {
address_1: "addr_1 updated",
country_code: "US",
})
).toEqual(
expect.objectContaining({
id: addressId,
address_1: "addr_1 updated",
address_2: "line 2",
country_code: "US",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
})
)
expect(
await stockLocationService.retrieve(loc.id, {
relations: ["address"],
})
).toEqual(
expect.objectContaining({
id: loc.id,
address_id: addressId,
address: expect.objectContaining({
id: addressId,
address_1: "addr_1 updated",
}),
})
)
})
it("delete", async () => {
const stockLocationService = appContainer.resolve("stockLocationService")
const loc = await stockLocationService.create({
name: "location",
address: {
address_1: "addr_1",
address_2: "line 2",
country_code: "DK",
city: "city",
phone: "111222333",
province: "province",
postal_code: "555-714",
metadata: {
abc: 123,
},
},
})
await stockLocationService.delete(loc.id)
const deletedItem = stockLocationService.retrieve(loc.id)
await expect(deletedItem).rejects.toThrow(
`StockLocation with id ${loc.id} was not found`
)
})
})
})