added expect-relations helper and used it to test get-cart
This commit is contained in:
@@ -5,8 +5,10 @@ const { Region } = require("@medusajs/medusa");
|
||||
const setupServer = require("../../../helpers/setup-server");
|
||||
const { useApi } = require("../../../helpers/use-api");
|
||||
const { initDb } = require("../../../helpers/use-db");
|
||||
const { expectRelations } = require("../../helpers/expect-relations");
|
||||
|
||||
const cartSeeder = require("../../helpers/cart-seeder");
|
||||
const productSeeder = require("../../helpers/product-seeder");
|
||||
|
||||
jest.setTimeout(30000);
|
||||
|
||||
@@ -330,4 +332,56 @@ describe("/store/carts", () => {
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("get-cart returns expected relations", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await productSeeder(dbConnection);
|
||||
await cartSeeder(dbConnection);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
const manager = dbConnection.manager;
|
||||
await doAfterEach(manager);
|
||||
});
|
||||
|
||||
|
||||
it("returns default relations", async () => {
|
||||
const api = useApi();
|
||||
|
||||
const expectedRelations = [
|
||||
"gift_cards",
|
||||
"region",
|
||||
"items.variant.prices",
|
||||
"payment",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"region.countries",
|
||||
"region.payment_providers",
|
||||
"payment_sessions",
|
||||
"shipping_methods.shipping_option",
|
||||
"discounts",
|
||||
]
|
||||
|
||||
await api.post("/store/carts/test-cart/line-items", {
|
||||
quantity: 1,
|
||||
variant_id: "test-variant",
|
||||
})
|
||||
|
||||
// await api.post("/store/carts/test-cart/shipping-methods", {
|
||||
// option_id: "test-option"
|
||||
// })
|
||||
|
||||
|
||||
const response = await api.get("/store/carts/test-cart")
|
||||
|
||||
console.log(response.data.cart)
|
||||
expectRelations(expectedRelations, response.data.cart)
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
20
integration-tests/api/helpers/expect-relations.js
Normal file
20
integration-tests/api/helpers/expect-relations.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const { expect } = require("@jest/globals")
|
||||
|
||||
export function expectRelations(expected, actual){
|
||||
expected.forEach(a => expectRelations_(a, actual))
|
||||
}
|
||||
|
||||
function expectRelations_(expected, actual){
|
||||
const items = expected.split(".");
|
||||
let data_ = actual;
|
||||
|
||||
for(const i in items){
|
||||
data_ = data_[items[i]];
|
||||
|
||||
if(data_ instanceof Array && data_.length > 0) {
|
||||
data_ = data_[0];
|
||||
}
|
||||
|
||||
expect(data_).toBeDefined();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ const {
|
||||
Product,
|
||||
ShippingProfile,
|
||||
ProductVariant,
|
||||
MoneyAmount,
|
||||
Image,
|
||||
} = require("@medusajs/medusa");
|
||||
|
||||
@@ -74,7 +75,14 @@ module.exports = async (connection, data = {}) => {
|
||||
inventory_quantity: 10,
|
||||
title: "Test variant",
|
||||
product_id: "test-product",
|
||||
prices: [{ id: "test-price", currency_code: "usd", amount: 100 }],
|
||||
prices: [],
|
||||
options: [{ id: "test-variant-option", value: "Default variant" }],
|
||||
});
|
||||
|
||||
await manager.insert(MoneyAmount, {
|
||||
variant_id: "test-variant",
|
||||
id: "test-price",
|
||||
currency_code: "usd",
|
||||
amount: 100,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -36,8 +36,10 @@ module.exports = {
|
||||
);
|
||||
|
||||
const databaseName = "medusa-integration";
|
||||
await createDatabase({ databaseName });
|
||||
|
||||
await createDatabase({ databaseName });
|
||||
|
||||
|
||||
const connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: "postgres://localhost/medusa-integration",
|
||||
@@ -66,6 +68,7 @@ module.exports = {
|
||||
|
||||
instance.setDb(dbConnection);
|
||||
return dbConnection;
|
||||
|
||||
},
|
||||
useDb: function () {
|
||||
return instance;
|
||||
|
||||
@@ -49,6 +49,8 @@ export const defaultRelations = ["order", "cart"]
|
||||
export const defaultCartRelations = [
|
||||
"region",
|
||||
"items",
|
||||
"items.variant",
|
||||
"items.variant.prices",
|
||||
"payment",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
|
||||
@@ -101,13 +101,15 @@ export const defaultRelations = [
|
||||
"gift_cards",
|
||||
"region",
|
||||
"items",
|
||||
"items.variant",
|
||||
"items.variant.prices",
|
||||
"payment",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"region.countries",
|
||||
"region.payment_providers",
|
||||
"shipping_methods",
|
||||
"payment_sessions",
|
||||
"shipping_methods",
|
||||
"shipping_methods.shipping_option",
|
||||
"discounts",
|
||||
]
|
||||
|
||||
@@ -33,6 +33,7 @@ export const defaultRelations = [
|
||||
"items",
|
||||
"items.variant",
|
||||
"items.variant.product",
|
||||
"items.variant.prices",
|
||||
"shipping_methods",
|
||||
"discounts",
|
||||
"customer",
|
||||
|
||||
@@ -99,7 +99,7 @@ export class LineItem {
|
||||
@Column({ nullable: true })
|
||||
variant_id: string
|
||||
|
||||
@ManyToOne(() => ProductVariant, { eager: true })
|
||||
@ManyToOne(() => ProductVariant)
|
||||
@JoinColumn({ name: "variant_id" })
|
||||
variant: ProductVariant
|
||||
|
||||
|
||||
@@ -19,6 +19,9 @@ import { Product } from "./product"
|
||||
import { MoneyAmount } from "./money-amount"
|
||||
import { ProductOptionValue } from "./product-option-value"
|
||||
|
||||
|
||||
|
||||
|
||||
@Entity()
|
||||
export class ProductVariant {
|
||||
@PrimaryColumn()
|
||||
|
||||
Reference in New Issue
Block a user