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",
})
)
})
})
@@ -0,0 +1,96 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICartModuleService, IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../environment-helpers/use-db"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("Link: Cart Region", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService
let regionModule: IRegionModuleService
let remoteQuery
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)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
remoteQuery = appContainer.resolve("remoteQuery")
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
beforeEach(async () => {
// @ts-ignore
await regionModule.createDefaultCountriesAndCurrencies()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should query carts and regions with remote query", async () => {
const region = await regionModule.create({
name: "Region",
currency_code: "usd",
})
const cart = await cartModuleService.create({
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
})
const carts = await remoteQuery({
cart: {
fields: ["id"],
region: {
fields: ["id"],
},
},
})
const regions = await remoteQuery({
region: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: cart.id,
region: expect.objectContaining({ id: region.id }),
}),
])
)
expect(regions).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
})
})