feat: CartRegion link, definition + workflow (#6392)

This commit is contained in:
Oli Juhl
2024-02-16 14:06:26 +00:00
committed by GitHub
parent 7df4947ecf
commit 24fb102a56
17 changed files with 495 additions and 218 deletions
@@ -0,0 +1,166 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICartModuleService, IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("POST /store/carts", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService
let regionModuleService: IRegionModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
regionModuleService = appContainer.resolve(ModuleRegistrationName.REGION)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
// @ts-ignore
await regionModuleService.createDefaultCountriesAndCurrencies()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should create a cart", async () => {
const region = await regionModuleService.create({
name: "US",
currency_code: "usd",
})
const api = useApi() as any
const response = await api.post(`/store/carts`, {
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
})
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: response.data.cart.id,
currency_code: "usd",
email: "tony@stark.com",
region: expect.objectContaining({
id: region.id,
currency_code: "usd",
}),
})
)
})
it("should use any region", async () => {
await regionModuleService.create({
name: "US",
currency_code: "usd",
})
const api = useApi() as any
const response = await api.post(`/store/carts`, {
email: "tony@stark.com",
currency_code: "usd",
})
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: response.data.cart.id,
currency_code: "usd",
email: "tony@stark.com",
region: expect.objectContaining({
id: expect.any(String),
}),
})
)
})
it("should use region currency code", async () => {
await regionModuleService.create({
name: "US",
currency_code: "usd",
})
const api = useApi() as any
const response = await api.post(`/store/carts`, {
email: "tony@stark.com",
})
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: response.data.cart.id,
currency_code: "usd",
email: "tony@stark.com",
region: expect.objectContaining({
id: expect.any(String),
}),
})
)
})
it("should throw when no regions exist", async () => {
const api = useApi() as any
await expect(
api.post(`/store/carts`, {
email: "tony@stark.com",
currency_code: "usd",
})
).rejects.toThrow()
})
it("should create a cart", async () => {
const region = await regionModuleService.create({
name: "US",
currency_code: "usd",
})
await regionModuleService.create({
name: "Europe",
currency_code: "eur",
})
const api = useApi() as any
const response = await api.post(`/store/carts`, {
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
})
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: response.data.cart.id,
currency_code: "usd",
email: "tony@stark.com",
region: expect.objectContaining({
id: region.id,
currency_code: "usd",
}),
})
)
})
})
@@ -1,59 +0,0 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICartModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("POST /store/carts", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should create a cart", async () => {
const api = useApi() as any
const response = await api.post(`/store/carts`, {
email: "tony@stark.com",
currency_code: "usd",
})
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: response.data.cart.id,
currency_code: "usd",
email: "tony@stark.com",
})
)
})
})