/
inventory-hooks.app-test.js
121 lines (113 loc) · 3.97 KB
/
inventory-hooks.app-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* eslint dot-notation: 0 */
import { Meteor } from "meteor/meteor";
import { Inventory, Orders } from "/lib/collections";
import { Reaction } from "/server/api";
import { expect } from "meteor/practicalmeteor:chai";
import { sinon } from "meteor/practicalmeteor:sinon";
import Fixtures from "/server/imports/fixtures";
import { getShop } from "/server/imports/fixtures/shops";
Fixtures();
describe("Inventory Hooks", function () {
let originals;
let sandbox;
before(function () {
originals = {
mergeCart: Meteor.server.method_handlers["cart/mergeCart"],
createCart: Meteor.server.method_handlers["cart/createCart"],
copyCartToOrder: Meteor.server.method_handlers["cart/copyCartToOrder"],
addToCart: Meteor.server.method_handlers["cart/addToCart"],
setShipmentAddress: Meteor.server.method_handlers["cart/setShipmentAddress"],
setPaymentAddress: Meteor.server.method_handlers["cart/setPaymentAddress"]
};
});
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
function spyOnMethod(method, id) {
return sandbox.stub(Meteor.server.method_handlers, `cart/${method}`, function () {
check(arguments, [Match.Any]); // to prevent audit_arguments from complaining
this.userId = id;
return originals[method].apply(this, arguments);
});
}
it("should move allocated inventory to 'sold' when an order is created", function () {
this.timeout(50000);
Inventory.direct.remove({});
const cart = Factory.create("cartToOrder");
sandbox.stub(Reaction, "getShopId", function () {
return cart.shopId;
});
sandbox.stub(Meteor.server.method_handlers, "orders/sendNotification", function () {
check(arguments, [Match.Any]);
});
let shop = getShop();
let product = cart.items[0];
const inventoryItem = Inventory.insert({
productId: product.productId,
variantId: product.variants._id,
shopId: shop._id,
workflow: {
status: "reserved"
},
orderItemId: product._id
});
expect(inventoryItem).to.not.be.undefined;
Inventory.update(inventoryItem._id,
{
$set: {
"workflow.status": "reserved",
"orderItemId": product._id
}
});
spyOnMethod("copyCartToOrder", cart.userId);
Meteor.call("cart/copyCartToOrder", cart._id);
let updatedInventoryItem = Inventory.findOne({
productId: product.productId,
variantId: product.variants._id,
shopId: shop._id,
orderItemId: product._id
});
expect(updatedInventoryItem.workflow.status).to.equal("sold");
});
it("should move allocated inventory to 'shipped' when an order is shipped", function () {
this.timeout(50000);
Inventory.direct.remove({});
const cart = Factory.create("cartToOrder");
sandbox.stub(Reaction, "getShopId", function () {
return cart.shopId;
});
sandbox.stub(Reaction, "hasPermission", () => true);
sandbox.stub(Meteor.server.method_handlers, "orders/sendNotification", function () {
check(arguments, [Match.Any]);
});
let shop = getShop();
let product = cart.items[0];
const inventoryItem = Inventory.insert({
productId: product.productId,
variantId: product.variants._id,
shopId: shop._id,
workflow: {
status: "reserved"
},
orderItemId: product._id
});
expect(inventoryItem).to.not.be.undefined;
Inventory.update(inventoryItem._id,
{
$set: {
"workflow.status": "reserved",
"orderItemId": product._id
}
});
spyOnMethod("copyCartToOrder", cart.userId);
const orderId = Meteor.call("cart/copyCartToOrder", cart._id);
const order = Orders.findOne(orderId);
const shipping = { items: [] };
Meteor.call("orders/shipmentShipped", order, shipping);
const shippedInventoryItem = Inventory.findOne(inventoryItem._id);
expect(shippedInventoryItem.workflow.status).to.equal("shipped");
});
});