corrected tests

This commit is contained in:
--list
2021-07-02 10:53:27 +02:00
parent bb85edb5ca
commit 46a3cb3710
10 changed files with 164 additions and 104 deletions
+11
View File
@@ -0,0 +1,11 @@
{
"editor.formatOnSave": true,
"[javascript]": {
"editor.formatOnSave": true
},
"eslint.format.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
@@ -65,7 +65,7 @@ export default (app, container, config) => {
returnRoutes(route) returnRoutes(route)
variantRoutes(route) variantRoutes(route)
draftOrderRoutes(route) draftOrderRoutes(route)
collectionRoutes(route) //test collectionRoutes(route)
notificationRoutes(route) notificationRoutes(route)
returnReasonRoutes(route) returnReasonRoutes(route)
@@ -43,8 +43,7 @@ describe("POST /admin/orders/:id/fulfillment", () => {
quantity: 1, quantity: 1,
}, },
], ],
undefined, {"metadata": undefined, "noNotification": undefined}
undefined
) )
}) })
@@ -17,13 +17,20 @@ const defaultRelations = [
"returns.items.reason", "returns.items.reason",
"gift_cards", "gift_cards",
"gift_card_transactions", "gift_card_transactions",
"items",
"items.variant",
"items.variant.product",
"claims", "claims",
"claims.return_order", "claims.return_order",
"claims.shipping_methods", "claims.shipping_methods",
"claims.shipping_address", "claims.shipping_address",
"claims.additional_items", "claims.additional_items",
"claims.additional_items.variant",
"claims.additional_items.variant.product",
"claims.fulfillments", "claims.fulfillments",
"claims.claim_items", "claims.claim_items",
"claims.claim_items.variant",
"claims.claim_items.variant.product",
"claims.claim_items.images", "claims.claim_items.images",
"swaps", "swaps",
"swaps.return_order", "swaps.return_order",
+25 -13
View File
@@ -215,19 +215,21 @@ describe("ClaimService", () => {
it.each( it.each(
[ [
[false, false], [false, false],
[undefined, true] [undefined, true],
], ],
"passes correct no_notification status to event bus", async (input, expected) => { "passes correct no_notification status to event bus",
async (input, expected) => {
await claimService.create({ await claimService.create({
...testClaim, ...testClaim,
no_notification: input, no_notification: input,
})
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{
id: expect.any(String),
no_notification: expected
}) })
})
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String),
no_notification: expected,
})
}
)
}) })
describe("retrieve", () => { describe("retrieve", () => {
@@ -314,7 +316,9 @@ describe("ClaimService", () => {
}) })
it("successfully creates fulfillment", async () => { it("successfully creates fulfillment", async () => {
await claimService.createFulfillment("claim_id", { meta: "data" }) await claimService.createFulfillment("claim_id", {
metadata: { meta: "data" },
})
expect(withTransactionMock).toHaveBeenCalledTimes(3) expect(withTransactionMock).toHaveBeenCalledTimes(3)
expect(withTransactionMock).toHaveBeenCalledWith("eventBus") expect(withTransactionMock).toHaveBeenCalledWith("eventBus")
@@ -434,7 +438,10 @@ describe("ClaimService", () => {
) )
await claimService.createShipment("claim", "ful_123", ["track1234"], { await claimService.createShipment("claim", "ful_123", ["track1234"], {
meta: "data", metadata: {
meta: "data",
},
noNotification: false,
}) })
expect(withTransactionMock).toHaveBeenCalledTimes(3) expect(withTransactionMock).toHaveBeenCalledTimes(3)
@@ -446,7 +453,12 @@ describe("ClaimService", () => {
expect(fulfillmentService.createShipment).toHaveBeenCalledWith( expect(fulfillmentService.createShipment).toHaveBeenCalledWith(
"ful_123", "ful_123",
["track1234"], ["track1234"],
{ meta: "data" } {
metadata: {
meta: "data",
},
noNotification: false,
}
) )
expect(lineItemService.update).toHaveBeenCalledTimes(1) expect(lineItemService.update).toHaveBeenCalledTimes(1)
+60 -48
View File
@@ -866,27 +866,28 @@ describe("OrderService", () => {
[true, true], [true, true],
[false, false], [false, false],
[undefined, true], [undefined, true],
])("emits correct no_notification option with '%s'", async (input, expected) => { ])(
await orderService.createFulfillment( "emits correct no_notification option with '%s'",
"test-order", async (input, expected) => {
[ await orderService.createFulfillment(
{ "test-order",
item_id: "item_1", [
quantity: 1, {
}, item_id: "item_1",
], quantity: 1,
input },
) ],
{ noNotification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{ expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String), id: expect.any(String),
no_notification: expected, no_notification: expected,
}) })
}) }
)
}) })
describe("registerReturnReceived", () => { describe("registerReturnReceived", () => {
const order = { const order = {
items: [ items: [
@@ -1023,7 +1024,11 @@ describe("OrderService", () => {
} }
const fulfillmentService = { const fulfillmentService = {
retrieve: () => Promise.resolve({ order_id: IdMap.getId("test") }), retrieve: () =>
Promise.resolve({
order_id: IdMap.getId("test"),
no_notification: true,
}),
createShipment: jest createShipment: jest
.fn() .fn()
.mockImplementation((shipmentId, tracking, meta) => { .mockImplementation((shipmentId, tracking, meta) => {
@@ -1063,10 +1068,12 @@ describe("OrderService", () => {
) )
expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1) expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1)
expect(fulfillmentService.createShipment).toHaveBeenCalledWith( expect(
fulfillmentService.createShipment
).toHaveBeenCalledWith(
IdMap.getId("fulfillment"), IdMap.getId("fulfillment"),
[{ tracking_number: "1234" }, { tracking_number: "2345" }], [{ tracking_number: "1234" }, { tracking_number: "2345" }],
{} { metadata: undefined, noNotification: true }
) )
expect(orderRepo.save).toHaveBeenCalledTimes(1) expect(orderRepo.save).toHaveBeenCalledTimes(1)
@@ -1080,20 +1087,22 @@ describe("OrderService", () => {
[true, true], [true, true],
[false, false], [false, false],
[undefined, true], [undefined, true],
])("2emits correct no_notification option with '%s'", async (input, expected) => { ])(
await orderService.createShipment( "emits correct no_notification option with '%s'",
IdMap.getId("test"), async (input, expected) => {
IdMap.getId("fulfillment"), await orderService.createShipment(
[{ tracking_number: "1234" }, { tracking_number: "2345" }], IdMap.getId("test"),
input, IdMap.getId("fulfillment"),
{} [{ tracking_number: "1234" }, { tracking_number: "2345" }],
) { noNotification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{ expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String), id: expect.any(String),
no_notification: expected, no_notification: expected,
}) })
}) }
)
}) })
describe("createRefund", () => { describe("createRefund", () => {
@@ -1180,20 +1189,23 @@ describe("OrderService", () => {
it.each([ it.each([
[false, false], [false, false],
[undefined, true], [undefined, true],
])("emits correct no_notification option with '%s'", async (input, expected) => { ])(
await orderService.createRefund( "emits correct no_notification option with '%s'",
IdMap.getId("order_123"), async (input, expected) => {
100, await orderService.createRefund(
"discount", IdMap.getId("order_123"),
"note", 100,
input "discount",
) "note",
{ noNotification: input }
)
expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{ expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), {
id: expect.any(String), id: expect.any(String),
no_notification: expected, no_notification: expected,
refund_id: expect.any(String) refund_id: expect.any(String),
} ) })
}) }
)
}) })
}) })
@@ -355,7 +355,7 @@ describe("SwapService", () => {
id: IdMap.getId("return-shipping"), id: IdMap.getId("return-shipping"),
price: 20, price: 20,
}, },
input {noNotification: input}
) )
expect(eventBusService.emit).toHaveBeenCalledWith( expect(eventBusService.emit).toHaveBeenCalledWith(
+2 -2
View File
@@ -459,7 +459,7 @@ class ClaimService extends BaseService {
} }
async createShipment(id, fulfillmentId, trackingLinks, config = { async createShipment(id, fulfillmentId, trackingLinks, config = {
metadata: [], metadata: {},
noNotification: undefined, noNotification: undefined,
}) { }) {
const { metadata, noNotification } = config const { metadata, noNotification } = config
@@ -503,7 +503,7 @@ class ClaimService extends BaseService {
.emit(ClaimService.Events.SHIPMENT_CREATED, { .emit(ClaimService.Events.SHIPMENT_CREATED, {
id, id,
fulfillment_id: shipment.id, fulfillment_id: shipment.id,
no_notification: result.no_notification no_notification: evaluatedNoNotification
}) })
return result return result
+47 -30
View File
@@ -1,4 +1,3 @@
import { metadata } from "core-js/fn/reflect"
import _ from "lodash" import _ from "lodash"
import { Validator, MedusaError } from "medusa-core-utils" import { Validator, MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces" import { BaseService } from "medusa-interfaces"
@@ -312,7 +311,6 @@ class OrderService extends BaseService {
* @return {Promise<Order>} the order document * @return {Promise<Order>} the order document
*/ */
async retrieve(orderId, config = {}) { async retrieve(orderId, config = {}) {
const orderRepo = this.manager_.getCustomRepository(this.orderRepository_) const orderRepo = this.manager_.getCustomRepository(this.orderRepository_)
const validatedId = this.validateId_(orderId) const validatedId = this.validateId_(orderId)
@@ -409,7 +407,7 @@ class OrderService extends BaseService {
OrderService.Events.COMPLETED, OrderService.Events.COMPLETED,
{ {
id: orderId, id: orderId,
no_notification: order.no_notification no_notification: order.no_notification,
} }
) )
@@ -510,7 +508,6 @@ class OrderService extends BaseService {
toCreate.draft_order_id = draft.id toCreate.draft_order_id = draft.id
toCreate.no_notification = draft.no_notification_order toCreate.no_notification = draft.no_notification_order
} }
const o = await orderRepo.create(toCreate) const o = await orderRepo.create(toCreate)
@@ -557,7 +554,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.PLACED, { .emit(OrderService.Events.PLACED, {
id: result.id, id: result.id,
no_notification: result.no_notification no_notification: result.no_notification,
}) })
return result return result
@@ -576,10 +573,15 @@ class OrderService extends BaseService {
* the fulfillment * the fulfillment
* @return {order} the resulting order following the update. * @return {order} the resulting order following the update.
*/ */
async createShipment(orderId, fulfillmentId, trackingLinks, config = { async createShipment(
metadata: {}, orderId,
noNotification: undefined, fulfillmentId,
}) { trackingLinks,
config = {
metadata: {},
noNotification: undefined,
}
) {
const { metadata, noNotification } = config const { metadata, noNotification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
@@ -593,11 +595,15 @@ class OrderService extends BaseService {
) )
} }
const evaluatedNoNotification = noNotification !== undefined ? noNotification : shipment.no_notification const evaluatedNoNotification =
noNotification !== undefined ? noNotification : shipment.no_notification
const shipmentRes = await this.fulfillmentService_ const shipmentRes = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createShipment(fulfillmentId, trackingLinks, {metadata, noNotification: evaluatedNoNotification}) .createShipment(fulfillmentId, trackingLinks, {
metadata,
noNotification: evaluatedNoNotification,
})
order.fulfillment_status = "shipped" order.fulfillment_status = "shipped"
for (const item of order.items) { for (const item of order.items) {
@@ -626,7 +632,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.SHIPMENT_CREATED, { .emit(OrderService.Events.SHIPMENT_CREATED, {
id: orderId, id: orderId,
fulfillment_id: shipmentRes.id, fulfillment_id: shipmentRes.id,
no_notification: evaluatedNoNotification no_notification: evaluatedNoNotification,
}) })
return result return result
@@ -647,7 +653,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.PLACED, { .emit(OrderService.Events.PLACED, {
id: result.id, id: result.id,
no_notification: order.no_notification no_notification: order.no_notification,
}) })
return result return result
}) })
@@ -815,7 +821,7 @@ class OrderService extends BaseService {
await this.updateBillingAddress_(order, update.billing_address) await this.updateBillingAddress_(order, update.billing_address)
} }
if("no_notification" in update){ if ("no_notification" in update) {
order.no_notification = update.no_notification order.no_notification = update.no_notification
} }
@@ -839,7 +845,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.UPDATED, { .emit(OrderService.Events.UPDATED, {
id: orderId, id: orderId,
no_notification: order.no_notification no_notification: order.no_notification,
}) })
return result return result
}) })
@@ -890,7 +896,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.CANCELED, { .emit(OrderService.Events.CANCELED, {
id: order.id, id: order.id,
no_notification: order.no_notification no_notification: order.no_notification,
}) })
return result return result
}) })
@@ -919,7 +925,7 @@ class OrderService extends BaseService {
id: orderId, id: orderId,
payment_id: p.id, payment_id: p.id,
error: err, error: err,
no_notification: order.no_notification no_notification: order.no_notification,
}) })
}) })
@@ -945,7 +951,7 @@ class OrderService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(OrderService.Events.PAYMENT_CAPTURED, { .emit(OrderService.Events.PAYMENT_CAPTURED, {
id: result.id, id: result.id,
no_notification: order.no_notification no_notification: order.no_notification,
}) })
} }
@@ -992,10 +998,14 @@ class OrderService extends BaseService {
* @param {string} orderId - id of order to cancel. * @param {string} orderId - id of order to cancel.
* @return {Promise} result of the update operation. * @return {Promise} result of the update operation.
*/ */
async createFulfillment(orderId, itemsToFulfill, config = { async createFulfillment(
orderId,
itemsToFulfill,
config = {
noNotification: undefined, noNotification: undefined,
metadata: {}, metadata: {},
}) { }
) {
const { metadata, noNotification } = config const { metadata, noNotification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
@@ -1032,7 +1042,6 @@ class OrderService extends BaseService {
) )
} }
const fulfillments = await this.fulfillmentService_ const fulfillments = await this.fulfillmentService_
.withTransaction(manager) .withTransaction(manager)
.createFulfillment(order, itemsToFulfill, { .createFulfillment(order, itemsToFulfill, {
@@ -1077,7 +1086,8 @@ class OrderService extends BaseService {
order.fulfillments = [...order.fulfillments, ...fulfillments] order.fulfillments = [...order.fulfillments, ...fulfillments]
const result = await orderRepo.save(order) const result = await orderRepo.save(order)
const evaluatedNoNotification = noNotification !== undefined ? noNotification : order.no_notification const evaluatedNoNotification =
noNotification !== undefined ? noNotification : order.no_notification
for (const fulfillment of fulfillments) { for (const fulfillment of fulfillments) {
await this.eventBus_ await this.eventBus_
@@ -1085,7 +1095,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.FULFILLMENT_CREATED, { .emit(OrderService.Events.FULFILLMENT_CREATED, {
id: orderId, id: orderId,
fulfillment_id: fulfillment.id, fulfillment_id: fulfillment.id,
no_notification: evaluatedNoNotification no_notification: evaluatedNoNotification,
}) })
} }
@@ -1141,9 +1151,15 @@ class OrderService extends BaseService {
/** /**
* Refunds a given amount back to the customer. * Refunds a given amount back to the customer.
*/ */
async createRefund(orderId, refundAmount, reason, note, config = { async createRefund(
noNotification: undefined, orderId,
}) { refundAmount,
reason,
note,
config = {
noNotification: undefined,
}
) {
const { noNotification } = config const { noNotification } = config
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
@@ -1165,12 +1181,13 @@ class OrderService extends BaseService {
const result = await this.retrieve(orderId) const result = await this.retrieve(orderId)
const evaluatedNoNotification = noNotification !== undefined ? noNotification : order.no_notification const evaluatedNoNotification =
noNotification !== undefined ? noNotification : order.no_notification
this.eventBus_.emit(OrderService.Events.REFUND_CREATED, { this.eventBus_.emit(OrderService.Events.REFUND_CREATED, {
id: result.id, id: result.id,
refund_id: refund.id, refund_id: refund.id,
no_notification: evaluatedNoNotification no_notification: evaluatedNoNotification,
}) })
return result return result
}) })
@@ -1274,7 +1291,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.RETURN_ACTION_REQUIRED, { .emit(OrderService.Events.RETURN_ACTION_REQUIRED, {
id: result.id, id: result.id,
return_id: receivedReturn.id, return_id: receivedReturn.id,
no_notification: receivedReturn.no_notification no_notification: receivedReturn.no_notification,
}) })
return result return result
} }
@@ -1306,7 +1323,7 @@ class OrderService extends BaseService {
.emit(OrderService.Events.ITEMS_RETURNED, { .emit(OrderService.Events.ITEMS_RETURNED, {
id: order.id, id: order.id,
return_id: receivedReturn.id, return_id: receivedReturn.id,
no_notification: receivedReturn.no_notification no_notification: receivedReturn.no_notification,
}) })
return result return result
}) })
+9 -7
View File
@@ -215,9 +215,11 @@ class SwapService extends BaseService {
returnItems, returnItems,
additionalItems, additionalItems,
returnShipping, returnShipping,
custom = {} custom = {
) { noNotification: undefined
}
){
const { noNotification, ...rest } = custom
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
if ( if (
order.fulfillment_status === "not_fulfilled" || order.fulfillment_status === "not_fulfilled" ||
@@ -239,8 +241,6 @@ class SwapService extends BaseService {
}) })
) )
const { noNotification, ...rest } = custom
const evaluatedNoNotification = noNotification !== undefined ? noNotification : order.no_notification const evaluatedNoNotification = noNotification !== undefined ? noNotification : order.no_notification
const swapRepo = manager.getCustomRepository(this.swapRepository_) const swapRepo = manager.getCustomRepository(this.swapRepository_)
@@ -746,9 +746,11 @@ class SwapService extends BaseService {
await this.eventBus_ await this.eventBus_
.withTransaction(manager) .withTransaction(manager)
.emit(SwapService.Events.FULFILLMENT_CREATED, { .emit(SwapService.Events.FULFILLMENT_CREATED,
{
id: swapId, id: swapId,
fulfillment_id: shipment.id, fulfillment_id: result.id,
no_notification: evaluatedNoNotification no_notification: evaluatedNoNotification
}) })