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 setupServer = require("../../../helpers/setup-server");
|
||||||
const { useApi } = require("../../../helpers/use-api");
|
const { useApi } = require("../../../helpers/use-api");
|
||||||
const { initDb } = require("../../../helpers/use-db");
|
const { initDb } = require("../../../helpers/use-db");
|
||||||
|
const { expectRelations } = require("../../helpers/expect-relations");
|
||||||
|
|
||||||
const cartSeeder = require("../../helpers/cart-seeder");
|
const cartSeeder = require("../../helpers/cart-seeder");
|
||||||
|
const productSeeder = require("../../helpers/product-seeder");
|
||||||
|
|
||||||
jest.setTimeout(30000);
|
jest.setTimeout(30000);
|
||||||
|
|
||||||
@@ -330,4 +332,56 @@ describe("/store/carts", () => {
|
|||||||
expect(response.status).toEqual(200);
|
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)
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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,
|
Product,
|
||||||
ShippingProfile,
|
ShippingProfile,
|
||||||
ProductVariant,
|
ProductVariant,
|
||||||
|
MoneyAmount,
|
||||||
Image,
|
Image,
|
||||||
} = require("@medusajs/medusa");
|
} = require("@medusajs/medusa");
|
||||||
|
|
||||||
@@ -74,7 +75,14 @@ module.exports = async (connection, data = {}) => {
|
|||||||
inventory_quantity: 10,
|
inventory_quantity: 10,
|
||||||
title: "Test variant",
|
title: "Test variant",
|
||||||
product_id: "test-product",
|
product_id: "test-product",
|
||||||
prices: [{ id: "test-price", currency_code: "usd", amount: 100 }],
|
prices: [],
|
||||||
options: [{ id: "test-variant-option", value: "Default variant" }],
|
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";
|
const databaseName = "medusa-integration";
|
||||||
|
|
||||||
await createDatabase({ databaseName });
|
await createDatabase({ databaseName });
|
||||||
|
|
||||||
|
|
||||||
const connection = await createConnection({
|
const connection = await createConnection({
|
||||||
type: "postgres",
|
type: "postgres",
|
||||||
url: "postgres://localhost/medusa-integration",
|
url: "postgres://localhost/medusa-integration",
|
||||||
@@ -66,6 +68,7 @@ module.exports = {
|
|||||||
|
|
||||||
instance.setDb(dbConnection);
|
instance.setDb(dbConnection);
|
||||||
return dbConnection;
|
return dbConnection;
|
||||||
|
|
||||||
},
|
},
|
||||||
useDb: function () {
|
useDb: function () {
|
||||||
return instance;
|
return instance;
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ export const defaultRelations = ["order", "cart"]
|
|||||||
export const defaultCartRelations = [
|
export const defaultCartRelations = [
|
||||||
"region",
|
"region",
|
||||||
"items",
|
"items",
|
||||||
|
"items.variant",
|
||||||
|
"items.variant.prices",
|
||||||
"payment",
|
"payment",
|
||||||
"shipping_address",
|
"shipping_address",
|
||||||
"billing_address",
|
"billing_address",
|
||||||
|
|||||||
@@ -101,13 +101,15 @@ export const defaultRelations = [
|
|||||||
"gift_cards",
|
"gift_cards",
|
||||||
"region",
|
"region",
|
||||||
"items",
|
"items",
|
||||||
|
"items.variant",
|
||||||
|
"items.variant.prices",
|
||||||
"payment",
|
"payment",
|
||||||
"shipping_address",
|
"shipping_address",
|
||||||
"billing_address",
|
"billing_address",
|
||||||
"region.countries",
|
"region.countries",
|
||||||
"region.payment_providers",
|
"region.payment_providers",
|
||||||
"shipping_methods",
|
|
||||||
"payment_sessions",
|
"payment_sessions",
|
||||||
|
"shipping_methods",
|
||||||
"shipping_methods.shipping_option",
|
"shipping_methods.shipping_option",
|
||||||
"discounts",
|
"discounts",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export const defaultRelations = [
|
|||||||
"items",
|
"items",
|
||||||
"items.variant",
|
"items.variant",
|
||||||
"items.variant.product",
|
"items.variant.product",
|
||||||
|
"items.variant.prices",
|
||||||
"shipping_methods",
|
"shipping_methods",
|
||||||
"discounts",
|
"discounts",
|
||||||
"customer",
|
"customer",
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ export class LineItem {
|
|||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
variant_id: string
|
variant_id: string
|
||||||
|
|
||||||
@ManyToOne(() => ProductVariant, { eager: true })
|
@ManyToOne(() => ProductVariant)
|
||||||
@JoinColumn({ name: "variant_id" })
|
@JoinColumn({ name: "variant_id" })
|
||||||
variant: ProductVariant
|
variant: ProductVariant
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ import { Product } from "./product"
|
|||||||
import { MoneyAmount } from "./money-amount"
|
import { MoneyAmount } from "./money-amount"
|
||||||
import { ProductOptionValue } from "./product-option-value"
|
import { ProductOptionValue } from "./product-option-value"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class ProductVariant {
|
export class ProductVariant {
|
||||||
@PrimaryColumn()
|
@PrimaryColumn()
|
||||||
|
|||||||
Reference in New Issue
Block a user