chore: V2 core loader + modules integration-tests (#6544)

This commit is contained in:
Oli Juhl
2024-02-29 16:46:30 +01:00
committed by GitHub
parent dc025302a1
commit 296d7faad4
100 changed files with 318 additions and 78 deletions

View File

@@ -0,0 +1,164 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
ICartModuleService,
ICustomerModuleService,
IRegionModuleService,
ISalesChannelModuleService,
} 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("Cart links", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService
let regionModule: IRegionModuleService
let customerModule: ICustomerModuleService
let scModuleService: ISalesChannelModuleService
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)
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
scModuleService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL)
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.createDefaultCountries()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should query carts, sales channels, customers, regions with remote query", async () => {
const region = await regionModule.create({
name: "Region",
currency_code: "usd",
})
const customer = await customerModule.create({
email: "tony@stark.com",
})
const salesChannel = await scModuleService.create({
name: "Webshop",
})
const cart = await cartModuleService.create({
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
sales_channel_id: salesChannel.id,
customer_id: customer.id,
})
const carts = await remoteQuery({
cart: {
fields: ["id"],
region: {
fields: ["id"],
},
customer: {
fields: ["id"],
},
sales_channel: {
fields: ["id"],
},
},
})
const salesChannels = await remoteQuery({
sales_channel: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const customers = await remoteQuery({
customer: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const regions = await remoteQuery({
region: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: cart.id,
customer: expect.objectContaining({ id: customer.id }),
sales_channel: expect.objectContaining({ id: salesChannel.id }),
region: expect.objectContaining({ id: region.id }),
}),
])
)
expect(salesChannels).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: salesChannel.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(customers).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: customer.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(regions).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
})
})

View File

@@ -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.createDefaultCountries()
})
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 }),
]),
}),
])
)
})
})

View File

@@ -0,0 +1,188 @@
import { initialize, runMigrations } from "@medusajs/link-modules"
import { MedusaModule, ModuleJoinerConfig } from "@medusajs/modules-sdk"
jest.setTimeout(5000000)
const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_TEMP_NAME
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
describe("Link Modules", () => {
let links
const linkDefinition: ModuleJoinerConfig[] = [
{
serviceName: "linkServiceName",
isLink: true,
databaseConfig: {
tableName: "linkTableName",
idPrefix: "prefix",
extraFields: {
extra_field: {
type: "integer",
defaultValue: "-1",
},
another_field: {
type: "string",
nullable: true,
},
},
},
relationships: [
{
serviceName: "moduleA",
primaryKey: "id",
foreignKey: "product_id",
alias: "product",
},
{
serviceName: "moduleB",
primaryKey: "id",
foreignKey: "inventory_item_id",
alias: "inventory",
},
],
},
]
const dbConfig = {
database: {
clientUrl: DB_URL,
},
}
beforeAll(async () => {
jest.spyOn(MedusaModule, "getLoadedModules").mockImplementation((() => {
return [{ moduleA: [{}] }, { moduleB: [{}] }]
}) as any)
await runMigrations({ options: dbConfig }, linkDefinition)
links = await initialize(dbConfig, linkDefinition)
})
afterAll(async () => {
jest.clearAllMocks()
})
it("Should insert values in a declared link", async function () {
// simple
await links.linkServiceName.create("modA_id", "modB_id")
// extra fields
await links.linkServiceName.create("123", "abc", {
extra_field: 333,
another_field: "value**",
})
// bulk
await links.linkServiceName.create([
["111", "aaa", { another_field: "test" }],
["222", "bbb"],
["333", "ccc", { extra_field: 2 }],
["444", "bbb"],
])
const values = await links.linkServiceName.list()
expect(values).toEqual([
{
product_id: "modA_id",
inventory_item_id: "modB_id",
id: expect.stringMatching("prefix_.+"),
extra_field: -1,
another_field: null,
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
},
expect.objectContaining({
product_id: "123",
inventory_item_id: "abc",
id: expect.stringMatching("prefix_.+"),
extra_field: 333,
another_field: "value**",
}),
expect.objectContaining({
product_id: "111",
inventory_item_id: "aaa",
extra_field: -1,
another_field: "test",
}),
expect.objectContaining({
product_id: "222",
inventory_item_id: "bbb",
extra_field: -1,
another_field: null,
}),
expect.objectContaining({
product_id: "333",
inventory_item_id: "ccc",
id: expect.stringMatching("prefix_.+"),
extra_field: 2,
}),
expect.objectContaining({
product_id: "444",
inventory_item_id: "bbb",
}),
])
})
it("Should dismiss the link of a given pair of keys", async function () {
// simple
const dismissSingle = await links.linkServiceName.dismiss(
"modA_id",
"modB_id"
)
// bulk
const dismissMulti = await links.linkServiceName.dismiss([
["111", "aaa"],
["333", "ccc"],
])
expect(dismissSingle).toEqual([
expect.objectContaining({
product_id: "modA_id",
inventory_item_id: "modB_id",
deleted_at: expect.any(Date),
}),
])
expect(dismissMulti).toEqual([
expect.objectContaining({
product_id: "111",
inventory_item_id: "aaa",
deleted_at: expect.any(Date),
}),
expect.objectContaining({
product_id: "333",
inventory_item_id: "ccc",
deleted_at: expect.any(Date),
}),
])
})
it("Should delete all the links related to a given key", async function () {
await links.linkServiceName.softDelete({
inventory_item_id: "bbb",
})
const values = await links.linkServiceName.list(
{ inventory_item_id: "bbb" },
{ withDeleted: true }
)
expect(values).toEqual([
expect.objectContaining({
product_id: "222",
inventory_item_id: "bbb",
deleted_at: expect.any(Date),
}),
expect.objectContaining({
product_id: "444",
inventory_item_id: "bbb",
deleted_at: expect.any(Date),
}),
])
})
})