feat: Allow backorder on swaps (#404)
This commit is contained in:
committed by
GitHub
parent
75b608330b
commit
00ab03f3a2
@@ -0,0 +1,382 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`/admin/products GET /admin/products returns a list of products with child entities 1`] = `
|
||||
Array [
|
||||
Object {
|
||||
"collection": Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"handle": "test-collection",
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"metadata": null,
|
||||
"title": "Test collection",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
"collection_id": "test-collection",
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"description": "test-product-description",
|
||||
"discountable": true,
|
||||
"handle": "test-product",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"images": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"metadata": null,
|
||||
"updated_at": Any<String>,
|
||||
"url": "test-image.png",
|
||||
},
|
||||
],
|
||||
"is_giftcard": false,
|
||||
"length": null,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"metadata": null,
|
||||
"product_id": StringMatching /\\^test-\\*/,
|
||||
"title": "test-option",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
],
|
||||
"origin_country": null,
|
||||
"profile_id": StringMatching /\\^sp_\\*/,
|
||||
"subtitle": null,
|
||||
"tags": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^tag\\*/,
|
||||
"metadata": null,
|
||||
"updated_at": Any<String>,
|
||||
"value": "123",
|
||||
},
|
||||
],
|
||||
"thumbnail": null,
|
||||
"title": "Test product",
|
||||
"type": Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"metadata": null,
|
||||
"updated_at": Any<String>,
|
||||
"value": "test-type",
|
||||
},
|
||||
"type_id": "test-type",
|
||||
"updated_at": Any<String>,
|
||||
"variants": Array [
|
||||
Object {
|
||||
"allow_backorder": false,
|
||||
"barcode": "test-barcode",
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"ean": "test-ean",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-variant",
|
||||
"inventory_quantity": 10,
|
||||
"length": null,
|
||||
"manage_inventory": true,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-variant-option\\*/,
|
||||
"metadata": null,
|
||||
"option_id": StringMatching /\\^test-opt\\*/,
|
||||
"updated_at": Any<String>,
|
||||
"value": "Default variant",
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"origin_country": null,
|
||||
"prices": Array [
|
||||
Object {
|
||||
"amount": 100,
|
||||
"created_at": Any<String>,
|
||||
"currency_code": "usd",
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-price\\*/,
|
||||
"region_id": null,
|
||||
"sale_amount": null,
|
||||
"updated_at": Any<String>,
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"product_id": StringMatching /\\^test-\\*/,
|
||||
"sku": "test-sku",
|
||||
"title": "Test variant",
|
||||
"upc": "test-upc",
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
Object {
|
||||
"allow_backorder": false,
|
||||
"barcode": null,
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"ean": "test-ean2",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-variant_2",
|
||||
"inventory_quantity": 10,
|
||||
"length": null,
|
||||
"manage_inventory": true,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-variant-option\\*/,
|
||||
"metadata": null,
|
||||
"option_id": StringMatching /\\^test-opt\\*/,
|
||||
"updated_at": Any<String>,
|
||||
"value": "Default variant 2",
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"origin_country": null,
|
||||
"prices": Array [
|
||||
Object {
|
||||
"amount": 100,
|
||||
"created_at": Any<String>,
|
||||
"currency_code": "usd",
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-price\\*/,
|
||||
"region_id": null,
|
||||
"sale_amount": null,
|
||||
"updated_at": Any<String>,
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"product_id": StringMatching /\\^test-\\*/,
|
||||
"sku": "test-sku2",
|
||||
"title": "Test variant rank (2)",
|
||||
"upc": "test-upc2",
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
Object {
|
||||
"allow_backorder": false,
|
||||
"barcode": "test-barcode 1",
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"ean": "test-ean1",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-variant_1",
|
||||
"inventory_quantity": 10,
|
||||
"length": null,
|
||||
"manage_inventory": true,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-variant-option\\*/,
|
||||
"metadata": null,
|
||||
"option_id": StringMatching /\\^test-opt\\*/,
|
||||
"updated_at": Any<String>,
|
||||
"value": "Default variant 1",
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"origin_country": null,
|
||||
"prices": Array [
|
||||
Object {
|
||||
"amount": 100,
|
||||
"created_at": Any<String>,
|
||||
"currency_code": "usd",
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-price\\*/,
|
||||
"region_id": null,
|
||||
"sale_amount": null,
|
||||
"updated_at": Any<String>,
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"product_id": StringMatching /\\^test-\\*/,
|
||||
"sku": "test-sku1",
|
||||
"title": "Test variant rank (1)",
|
||||
"upc": "test-upc1",
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
],
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
Object {
|
||||
"collection": Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"handle": "test-collection",
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"metadata": null,
|
||||
"title": "Test collection",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
"collection_id": "test-collection",
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"description": "test-product-description1",
|
||||
"discountable": true,
|
||||
"handle": "test-product1",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"images": Array [],
|
||||
"is_giftcard": false,
|
||||
"length": null,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [],
|
||||
"origin_country": null,
|
||||
"profile_id": StringMatching /\\^sp_\\*/,
|
||||
"subtitle": null,
|
||||
"tags": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^tag\\*/,
|
||||
"metadata": null,
|
||||
"updated_at": Any<String>,
|
||||
"value": "123",
|
||||
},
|
||||
],
|
||||
"thumbnail": null,
|
||||
"title": "Test product1",
|
||||
"type": Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-\\*/,
|
||||
"metadata": null,
|
||||
"updated_at": Any<String>,
|
||||
"value": "test-type",
|
||||
},
|
||||
"type_id": "test-type",
|
||||
"updated_at": Any<String>,
|
||||
"variants": Array [
|
||||
Object {
|
||||
"allow_backorder": false,
|
||||
"barcode": null,
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"ean": "test-ean4",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-variant_4",
|
||||
"inventory_quantity": 10,
|
||||
"length": null,
|
||||
"manage_inventory": true,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-variant-option\\*/,
|
||||
"metadata": null,
|
||||
"option_id": StringMatching /\\^test-opt\\*/,
|
||||
"updated_at": Any<String>,
|
||||
"value": "Default variant 4",
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"origin_country": null,
|
||||
"prices": Array [
|
||||
Object {
|
||||
"amount": 100,
|
||||
"created_at": Any<String>,
|
||||
"currency_code": "usd",
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-price\\*/,
|
||||
"region_id": null,
|
||||
"sale_amount": null,
|
||||
"updated_at": Any<String>,
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"product_id": StringMatching /\\^test-\\*/,
|
||||
"sku": "test-sku4",
|
||||
"title": "Test variant rank (2)",
|
||||
"upc": "test-upc4",
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
Object {
|
||||
"allow_backorder": false,
|
||||
"barcode": null,
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"ean": "test-ean3",
|
||||
"height": null,
|
||||
"hs_code": null,
|
||||
"id": "test-variant_3",
|
||||
"inventory_quantity": 10,
|
||||
"length": null,
|
||||
"manage_inventory": true,
|
||||
"material": null,
|
||||
"metadata": null,
|
||||
"mid_code": null,
|
||||
"options": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-variant-option\\*/,
|
||||
"metadata": null,
|
||||
"option_id": StringMatching /\\^test-opt\\*/,
|
||||
"updated_at": Any<String>,
|
||||
"value": "Default variant 3",
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"origin_country": null,
|
||||
"prices": Array [
|
||||
Object {
|
||||
"amount": 100,
|
||||
"created_at": Any<String>,
|
||||
"currency_code": "usd",
|
||||
"deleted_at": null,
|
||||
"id": StringMatching /\\^test-price\\*/,
|
||||
"region_id": null,
|
||||
"sale_amount": null,
|
||||
"updated_at": Any<String>,
|
||||
"variant_id": StringMatching /\\^test-variant\\*/,
|
||||
},
|
||||
],
|
||||
"product_id": StringMatching /\\^test-\\*/,
|
||||
"sku": "test-sku3",
|
||||
"title": "Test variant rank (2)",
|
||||
"upc": "test-upc3",
|
||||
"updated_at": Any<String>,
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
],
|
||||
"weight": null,
|
||||
"width": null,
|
||||
},
|
||||
]
|
||||
`;
|
||||
@@ -1,49 +1,49 @@
|
||||
const path = require("path");
|
||||
const path = require("path")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server");
|
||||
const { useApi } = require("../../../helpers/use-api");
|
||||
const { initDb, useDb } = require("../../../helpers/use-db");
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const orderSeeder = require("../../helpers/order-seeder");
|
||||
const swapSeeder = require("../../helpers/swap-seeder");
|
||||
const adminSeeder = require("../../helpers/admin-seeder");
|
||||
const orderSeeder = require("../../helpers/order-seeder")
|
||||
const swapSeeder = require("../../helpers/swap-seeder")
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
jest.setTimeout(30000);
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/admin/swaps", () => {
|
||||
let medusaProcess;
|
||||
let dbConnection;
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."));
|
||||
dbConnection = await initDb({ cwd });
|
||||
medusaProcess = await setupServer({ cwd });
|
||||
});
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb();
|
||||
await db.shutdown();
|
||||
medusaProcess.kill();
|
||||
});
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("GET /admin/swaps/:id", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection);
|
||||
await orderSeeder(dbConnection);
|
||||
await swapSeeder(dbConnection);
|
||||
await adminSeeder(dbConnection)
|
||||
await orderSeeder(dbConnection)
|
||||
await swapSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
throw err;
|
||||
throw err
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb();
|
||||
await db.teardown();
|
||||
});
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("gets a swap with cart and totals", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/swaps/test-swap", {
|
||||
@@ -52,46 +52,46 @@ describe("/admin/swaps", () => {
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
expect(response.status).toEqual(200);
|
||||
console.log(err)
|
||||
})
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.swap).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-swap",
|
||||
})
|
||||
);
|
||||
)
|
||||
|
||||
expect(response.data.swap.cart).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-cart",
|
||||
id: "test-cart-w-swap",
|
||||
shipping_total: 1000,
|
||||
subtotal: 1000,
|
||||
total: 2000,
|
||||
})
|
||||
);
|
||||
expect(response.data.swap.cart).toHaveProperty("discount_total");
|
||||
expect(response.data.swap.cart).toHaveProperty("gift_card_total");
|
||||
});
|
||||
});
|
||||
)
|
||||
expect(response.data.swap.cart).toHaveProperty("discount_total")
|
||||
expect(response.data.swap.cart).toHaveProperty("gift_card_total")
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/swaps/", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection);
|
||||
await orderSeeder(dbConnection);
|
||||
await swapSeeder(dbConnection);
|
||||
await adminSeeder(dbConnection)
|
||||
await orderSeeder(dbConnection)
|
||||
await swapSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
throw err;
|
||||
throw err
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb();
|
||||
await db.teardown();
|
||||
});
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("lists all swaps", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/swaps/", {
|
||||
@@ -100,18 +100,18 @@ describe("/admin/swaps", () => {
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.data).toHaveProperty("count");
|
||||
expect(response.data.offset).toBe(0);
|
||||
expect(response.data.limit).toBe(50);
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data).toHaveProperty("count")
|
||||
expect(response.data.offset).toBe(0)
|
||||
expect(response.data.limit).toBe(50)
|
||||
expect(response.data.swaps).toContainEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-swap",
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,6 +8,14 @@ Object {
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/store/carts POST /store/carts/:id fails to complete swap cart with items inventory not/partially covered 1`] = `
|
||||
Object {
|
||||
"code": "insufficient_inventory",
|
||||
"message": "Variant with id: test-variant-2 does not have the required inventory",
|
||||
"type": "not_allowed",
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/store/carts POST /store/carts/:id returns early, if cart is already completed 1`] = `
|
||||
Object {
|
||||
"code": "cart_incompatible_state",
|
||||
|
||||
@@ -91,6 +91,7 @@ Object {
|
||||
"parent_order_id": "test-order",
|
||||
"swap_id": StringMatching /\\^swap_\\*/,
|
||||
},
|
||||
"payment_authorized_at": null,
|
||||
"payment_id": null,
|
||||
"region_id": "test-region",
|
||||
"shipping_address_id": "test-shipping-address",
|
||||
@@ -260,6 +261,7 @@ Object {
|
||||
"parent_order_id": "test-order",
|
||||
"swap_id": StringMatching /\\^swap_\\*/,
|
||||
},
|
||||
"payment_authorized_at": null,
|
||||
"payment_id": null,
|
||||
"region_id": "test-region",
|
||||
"shipping_address_id": "test-shipping-address",
|
||||
|
||||
@@ -1,155 +1,161 @@
|
||||
const path = require("path");
|
||||
const { Region, LineItem, GiftCard } = require("@medusajs/medusa");
|
||||
const path = require("path")
|
||||
const { Region, LineItem, GiftCard } = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server");
|
||||
const { useApi } = require("../../../helpers/use-api");
|
||||
const { initDb, useDb } = require("../../../helpers/use-db");
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const cartSeeder = require("../../helpers/cart-seeder");
|
||||
const cartSeeder = require("../../helpers/cart-seeder")
|
||||
const swapSeeder = require("../../helpers/swap-seeder")
|
||||
|
||||
jest.setTimeout(30000);
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/store/carts", () => {
|
||||
let medusaProcess;
|
||||
let dbConnection;
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
const doAfterEach = async () => {
|
||||
const db = useDb();
|
||||
return await db.teardown();
|
||||
};
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."));
|
||||
dbConnection = await initDb({ cwd });
|
||||
medusaProcess = await setupServer({ cwd });
|
||||
});
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
try {
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb();
|
||||
await db.shutdown();
|
||||
medusaProcess.kill();
|
||||
});
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("POST /store/carts", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager;
|
||||
const manager = dbConnection.manager
|
||||
await manager.insert(Region, {
|
||||
id: "region",
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
});
|
||||
})
|
||||
await manager.query(
|
||||
`UPDATE "country" SET region_id='region' WHERE iso_2 = 'us'`
|
||||
);
|
||||
});
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach();
|
||||
});
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("creates a cart", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post("/store/carts");
|
||||
expect(response.status).toEqual(200);
|
||||
const response = await api.post("/store/carts")
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const getRes = await api.post(`/store/carts/${response.data.cart.id}`);
|
||||
expect(getRes.status).toEqual(200);
|
||||
});
|
||||
const getRes = await api.post(`/store/carts/${response.data.cart.id}`)
|
||||
expect(getRes.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("creates a cart with country", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post("/store/carts", {
|
||||
country_code: "us",
|
||||
});
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.data.cart.shipping_address.country_code).toEqual("us");
|
||||
})
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.cart.shipping_address.country_code).toEqual("us")
|
||||
|
||||
const getRes = await api.post(`/store/carts/${response.data.cart.id}`);
|
||||
expect(getRes.status).toEqual(200);
|
||||
});
|
||||
const getRes = await api.post(`/store/carts/${response.data.cart.id}`)
|
||||
expect(getRes.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("creates a cart with context", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
const response = await api.post("/store/carts", {
|
||||
context: {
|
||||
test_id: "test",
|
||||
},
|
||||
});
|
||||
expect(response.status).toEqual(200);
|
||||
})
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
const getRes = await api.post(`/store/carts/${response.data.cart.id}`);
|
||||
expect(getRes.status).toEqual(200);
|
||||
const getRes = await api.post(`/store/carts/${response.data.cart.id}`)
|
||||
expect(getRes.status).toEqual(200)
|
||||
|
||||
const cart = getRes.data.cart;
|
||||
const cart = getRes.data.cart
|
||||
expect(cart.context).toEqual({
|
||||
ip: "::ffff:127.0.0.1",
|
||||
user_agent: "axios/0.21.1",
|
||||
test_id: "test",
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /store/carts/:id", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await cartSeeder(dbConnection);
|
||||
await cartSeeder(dbConnection)
|
||||
await swapSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach();
|
||||
});
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("fails on apply discount if limit has been reached", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
try {
|
||||
await api.post("/store/carts/test-cart", {
|
||||
discounts: [{ code: "CREATED" }],
|
||||
});
|
||||
})
|
||||
} catch (error) {
|
||||
expect(error.response.status).toEqual(400);
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.message).toEqual(
|
||||
"Discount has been used maximum allowed times"
|
||||
);
|
||||
)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
it("updates cart customer id", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post("/store/carts/test-cart", {
|
||||
customer_id: "test-customer-2",
|
||||
});
|
||||
})
|
||||
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("updates address using string id", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post("/store/carts/test-cart", {
|
||||
billing_address: "test-general-address",
|
||||
shipping_address: "test-general-address",
|
||||
});
|
||||
})
|
||||
|
||||
expect(response.data.cart.shipping_address_id).toEqual(
|
||||
"test-general-address"
|
||||
);
|
||||
)
|
||||
expect(response.data.cart.billing_address_id).toEqual(
|
||||
"test-general-address"
|
||||
);
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
)
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("updates address", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.post("/store/carts/test-cart", {
|
||||
shipping_address: {
|
||||
@@ -160,14 +166,14 @@ describe("/store/carts", () => {
|
||||
country_code: "us",
|
||||
postal_code: "something",
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
expect(response.data.cart.shipping_address.first_name).toEqual("clark");
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
expect(response.data.cart.shipping_address.first_name).toEqual("clark")
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("adds free shipping to cart then removes it again", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
let cart = await api.post(
|
||||
"/store/carts/test-cart",
|
||||
@@ -175,10 +181,10 @@ describe("/store/carts", () => {
|
||||
discounts: [{ code: "FREE_SHIPPING" }, { code: "CREATED" }],
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
expect(cart.data.cart.shipping_total).toBe(0);
|
||||
expect(cart.status).toEqual(200);
|
||||
expect(cart.data.cart.shipping_total).toBe(0)
|
||||
expect(cart.status).toEqual(200)
|
||||
|
||||
cart = await api.post(
|
||||
"/store/carts/test-cart",
|
||||
@@ -186,68 +192,68 @@ describe("/store/carts", () => {
|
||||
discounts: [{ code: "CREATED" }],
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
expect(cart.data.cart.shipping_total).toBe(1000);
|
||||
expect(cart.status).toEqual(200);
|
||||
});
|
||||
expect(cart.data.cart.shipping_total).toBe(1000)
|
||||
expect(cart.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("complete cart with giftcard total 0", async () => {
|
||||
const manager = dbConnection.manager;
|
||||
const manager = dbConnection.manager
|
||||
await manager.insert(GiftCard, {
|
||||
id: "gift_test",
|
||||
code: "GC_TEST",
|
||||
value: 20000,
|
||||
balance: 20000,
|
||||
region_id: "test-region",
|
||||
});
|
||||
})
|
||||
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
await api.post(`/store/carts/test-cart-3`, {
|
||||
gift_cards: [{ code: "GC_TEST" }],
|
||||
});
|
||||
})
|
||||
|
||||
const getRes = await api
|
||||
.post(`/store/carts/test-cart-3/complete`)
|
||||
.catch((err) => {
|
||||
console.log(err.response.data);
|
||||
});
|
||||
console.log(err.response.data)
|
||||
})
|
||||
|
||||
expect(getRes.status).toEqual(200);
|
||||
expect(getRes.data.type).toEqual("order");
|
||||
});
|
||||
expect(getRes.status).toEqual(200)
|
||||
expect(getRes.data.type).toEqual("order")
|
||||
})
|
||||
|
||||
it("complete cart with items inventory covered", async () => {
|
||||
const api = useApi();
|
||||
const getRes = await api.post(`/store/carts/test-cart-2/complete-cart`);
|
||||
const api = useApi()
|
||||
const getRes = await api.post(`/store/carts/test-cart-2/complete-cart`)
|
||||
|
||||
expect(getRes.status).toEqual(200);
|
||||
expect(getRes.status).toEqual(200)
|
||||
|
||||
const variantRes = await api.get("/store/variants/test-variant");
|
||||
expect(variantRes.data.variant.inventory_quantity).toEqual(0);
|
||||
});
|
||||
const variantRes = await api.get("/store/variants/test-variant")
|
||||
expect(variantRes.data.variant.inventory_quantity).toEqual(0)
|
||||
})
|
||||
|
||||
it("returns early, if cart is already completed", async () => {
|
||||
const manager = dbConnection.manager;
|
||||
const api = useApi();
|
||||
const manager = dbConnection.manager
|
||||
const api = useApi()
|
||||
await manager.query(
|
||||
`UPDATE "cart" SET completed_at=current_timestamp WHERE id = 'test-cart-2'`
|
||||
);
|
||||
)
|
||||
try {
|
||||
await api.post(`/store/carts/test-cart-2/complete-cart`);
|
||||
await api.post(`/store/carts/test-cart-2/complete-cart`)
|
||||
} catch (error) {
|
||||
expect(error.response.data).toMatchSnapshot({
|
||||
code: "not_allowed",
|
||||
message: "Cart has already been completed",
|
||||
code: "cart_incompatible_state",
|
||||
});
|
||||
expect(error.response.status).toEqual(409);
|
||||
})
|
||||
expect(error.response.status).toEqual(409)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
it("fails to complete cart with items inventory not/partially covered", async () => {
|
||||
const manager = dbConnection.manager;
|
||||
const manager = dbConnection.manager
|
||||
|
||||
const li = manager.create(LineItem, {
|
||||
id: "test-item",
|
||||
@@ -258,37 +264,110 @@ describe("/store/carts", () => {
|
||||
quantity: 99,
|
||||
variant_id: "test-variant-2",
|
||||
cart_id: "test-cart-2",
|
||||
});
|
||||
await manager.save(li);
|
||||
})
|
||||
await manager.save(li)
|
||||
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
try {
|
||||
await api.post(`/store/carts/test-cart-2/complete-cart`);
|
||||
await api.post(`/store/carts/test-cart-2/complete-cart`)
|
||||
} catch (e) {
|
||||
expect(e.response.data).toMatchSnapshot({
|
||||
code: "insufficient_inventory",
|
||||
});
|
||||
expect(e.response.status).toBe(409);
|
||||
})
|
||||
expect(e.response.status).toBe(409)
|
||||
}
|
||||
|
||||
//check to see if payment has been cancelled
|
||||
const res = await api.get(`/store/carts/test-cart-2`);
|
||||
expect(res.data.cart.payment.canceled_at).not.toBe(null);
|
||||
});
|
||||
});
|
||||
//check to see if payment has been cancelled and cart is not completed
|
||||
const res = await api.get(`/store/carts/test-cart-2`)
|
||||
expect(res.data.cart.payment.canceled_at).not.toBe(null)
|
||||
expect(res.data.cart.completed_at).toBe(null)
|
||||
})
|
||||
|
||||
it("fails to complete swap cart with items inventory not/partially covered", async () => {
|
||||
const manager = dbConnection.manager
|
||||
|
||||
const li = manager.create(LineItem, {
|
||||
id: "test-item",
|
||||
title: "Line Item",
|
||||
description: "Line Item Desc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 8000,
|
||||
quantity: 99,
|
||||
variant_id: "test-variant-2",
|
||||
cart_id: "swap-cart",
|
||||
})
|
||||
await manager.save(li)
|
||||
|
||||
await manager.query(
|
||||
"UPDATE swap SET cart_id='swap-cart' where id='test-swap'"
|
||||
)
|
||||
|
||||
const api = useApi()
|
||||
|
||||
try {
|
||||
await api.post(`/store/carts/swap-cart/complete-cart`)
|
||||
} catch (e) {
|
||||
expect(e.response.data).toMatchSnapshot({
|
||||
code: "insufficient_inventory",
|
||||
})
|
||||
expect(e.response.status).toBe(409)
|
||||
}
|
||||
|
||||
//check to see if payment has been cancelled and cart is not completed
|
||||
const res = await api.get(`/store/carts/swap-cart`)
|
||||
expect(res.data.cart.payment_authorized_at).toBe(null)
|
||||
expect(res.data.cart.payment.canceled_at).not.toBe(null)
|
||||
})
|
||||
|
||||
it("successfully completes swap cart with items inventory not/partially covered due to backorder flag", async () => {
|
||||
const manager = dbConnection.manager
|
||||
|
||||
const li = manager.create(LineItem, {
|
||||
id: "test-item",
|
||||
title: "Line Item",
|
||||
description: "Line Item Desc",
|
||||
thumbnail: "https://test.js/1234",
|
||||
unit_price: 8000,
|
||||
quantity: 99,
|
||||
variant_id: "test-variant-2",
|
||||
cart_id: "swap-cart",
|
||||
})
|
||||
await manager.save(li)
|
||||
await manager.query(
|
||||
"UPDATE swap SET cart_id='swap-cart' where id='test-swap'"
|
||||
)
|
||||
await manager.query(
|
||||
"UPDATE swap SET allow_backorder=true where id='test-swap'"
|
||||
)
|
||||
await manager.query("DELETE FROM payment where swap_id='test-swap'")
|
||||
|
||||
const api = useApi()
|
||||
|
||||
try {
|
||||
await api.post(`/store/carts/swap-cart/complete-cart`)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
//check to see if payment is authorized and cart is completed
|
||||
const res = await api.get(`/store/carts/swap-cart`)
|
||||
expect(res.data.cart.payment_authorized_at).not.toBe(null)
|
||||
expect(res.data.cart.completed_at).not.toBe(null)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /store/carts/:id/shipping-methods", () => {
|
||||
beforeEach(async () => {
|
||||
await cartSeeder(dbConnection);
|
||||
});
|
||||
await cartSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach();
|
||||
});
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("adds a shipping method to cart", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const cartWithShippingMethod = await api.post(
|
||||
"/store/carts/test-cart/shipping-methods",
|
||||
@@ -296,16 +375,16 @@ describe("/store/carts", () => {
|
||||
option_id: "test-option",
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
expect(cartWithShippingMethod.data.cart.shipping_methods).toContainEqual(
|
||||
expect.objectContaining({ shipping_option_id: "test-option" })
|
||||
);
|
||||
expect(cartWithShippingMethod.status).toEqual(200);
|
||||
});
|
||||
)
|
||||
expect(cartWithShippingMethod.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("adds a giftcard to cart, but ensures discount only applied to discountable items", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
// Add standard line item to cart
|
||||
await api.post(
|
||||
@@ -315,7 +394,7 @@ describe("/store/carts", () => {
|
||||
quantity: 1,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
// Add gift card to cart
|
||||
await api.post(
|
||||
@@ -325,7 +404,7 @@ describe("/store/carts", () => {
|
||||
quantity: 1,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
// Add a 10% discount to the cart
|
||||
const cartWithGiftcard = await api.post(
|
||||
@@ -334,16 +413,16 @@ describe("/store/carts", () => {
|
||||
discounts: [{ code: "10PERCENT" }],
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
// Ensure that the discount is only applied to the standard item
|
||||
expect(cartWithGiftcard.data.cart.total).toBe(1900); // 1000 (giftcard) + 900 (standard item with 10% discount)
|
||||
expect(cartWithGiftcard.data.cart.discount_total).toBe(100);
|
||||
expect(cartWithGiftcard.status).toEqual(200);
|
||||
});
|
||||
expect(cartWithGiftcard.data.cart.total).toBe(1900) // 1000 (giftcard) + 900 (standard item with 10% discount)
|
||||
expect(cartWithGiftcard.data.cart.discount_total).toBe(100)
|
||||
expect(cartWithGiftcard.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("adds no more than 1 shipping method per shipping profile", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
const addShippingMethod = async (option_id) => {
|
||||
return await api.post(
|
||||
"/store/carts/test-cart/shipping-methods",
|
||||
@@ -351,17 +430,17 @@ describe("/store/carts", () => {
|
||||
option_id,
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
};
|
||||
)
|
||||
}
|
||||
|
||||
await addShippingMethod("test-option");
|
||||
await addShippingMethod("test-option")
|
||||
const cartWithAnotherShippingMethod = await addShippingMethod(
|
||||
"test-option-2"
|
||||
);
|
||||
)
|
||||
|
||||
expect(
|
||||
cartWithAnotherShippingMethod.data.cart.shipping_methods.length
|
||||
).toEqual(1);
|
||||
).toEqual(1)
|
||||
expect(
|
||||
cartWithAnotherShippingMethod.data.cart.shipping_methods
|
||||
).toContainEqual(
|
||||
@@ -369,30 +448,30 @@ describe("/store/carts", () => {
|
||||
shipping_option_id: "test-option-2",
|
||||
price: 500,
|
||||
})
|
||||
);
|
||||
expect(cartWithAnotherShippingMethod.status).toEqual(200);
|
||||
});
|
||||
});
|
||||
)
|
||||
expect(cartWithAnotherShippingMethod.status).toEqual(200)
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /store/carts/:id/discounts/:code", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await cartSeeder(dbConnection);
|
||||
await cartSeeder(dbConnection)
|
||||
await dbConnection.manager.query(
|
||||
`INSERT INTO "cart_discounts" (cart_id, discount_id) VALUES ('test-cart', 'free-shipping')`
|
||||
);
|
||||
)
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach();
|
||||
});
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("removes free shipping and updates shipping total", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
const cartWithFreeShipping = await api.post(
|
||||
"/store/carts/test-cart",
|
||||
@@ -400,36 +479,36 @@ describe("/store/carts", () => {
|
||||
discounts: [{ code: "FREE_SHIPPING" }],
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
expect(cartWithFreeShipping.data.cart.shipping_total).toBe(0);
|
||||
expect(cartWithFreeShipping.status).toEqual(200);
|
||||
expect(cartWithFreeShipping.data.cart.shipping_total).toBe(0)
|
||||
expect(cartWithFreeShipping.status).toEqual(200)
|
||||
|
||||
const response = await api.delete(
|
||||
"/store/carts/test-cart/discounts/FREE_SHIPPING"
|
||||
);
|
||||
)
|
||||
|
||||
expect(response.data.cart.shipping_total).toBe(1000);
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
});
|
||||
expect(response.data.cart.shipping_total).toBe(1000)
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
})
|
||||
|
||||
describe("get-cart with session customer", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await cartSeeder(dbConnection);
|
||||
await cartSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await doAfterEach();
|
||||
});
|
||||
await doAfterEach()
|
||||
})
|
||||
|
||||
it("updates empty cart.customer_id on cart retrieval", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
let customer = await api.post(
|
||||
"/store/customers",
|
||||
@@ -440,29 +519,25 @@ describe("/store/carts", () => {
|
||||
last_name: "oli",
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
const cookie = customer.headers["set-cookie"][0];
|
||||
const cookie = customer.headers["set-cookie"][0]
|
||||
|
||||
const cart = await api.post(
|
||||
"/store/carts",
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
const cart = await api.post("/store/carts", {}, { withCredentials: true })
|
||||
|
||||
const response = await api.get(`/store/carts/${cart.data.cart.id}`, {
|
||||
headers: {
|
||||
cookie,
|
||||
},
|
||||
withCredentials: true,
|
||||
});
|
||||
})
|
||||
|
||||
expect(response.data.cart.customer_id).toEqual(customer.data.customer.id);
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
expect(response.data.cart.customer_id).toEqual(customer.data.customer.id)
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("updates cart.customer_id on cart retrieval if cart.customer_id differ from session customer", async () => {
|
||||
const api = useApi();
|
||||
const api = useApi()
|
||||
|
||||
let customer = await api.post(
|
||||
"/store/customers",
|
||||
@@ -473,15 +548,15 @@ describe("/store/carts", () => {
|
||||
last_name: "oli",
|
||||
},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
)
|
||||
|
||||
const cookie = customer.headers["set-cookie"][0];
|
||||
const cookie = customer.headers["set-cookie"][0]
|
||||
|
||||
const cart = await api.post("/store/carts");
|
||||
const cart = await api.post("/store/carts")
|
||||
|
||||
const updatedCart = await api.post(`/store/carts/${cart.data.cart.id}`, {
|
||||
customer_id: "test-customer",
|
||||
});
|
||||
})
|
||||
|
||||
const response = await api.get(
|
||||
`/store/carts/${updatedCart.data.cart.id}`,
|
||||
@@ -490,10 +565,10 @@ describe("/store/carts", () => {
|
||||
cookie,
|
||||
},
|
||||
}
|
||||
);
|
||||
)
|
||||
|
||||
expect(response.data.cart.customer_id).toEqual(customer.data.customer.id);
|
||||
expect(response.status).toEqual(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
expect(response.data.cart.customer_id).toEqual(customer.data.customer.id)
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user