From 46a3cb3710c3796a3cef96a995d57db04796b574 Mon Sep 17 00:00:00 2001 From: --list Date: Fri, 2 Jul 2021 10:53:27 +0200 Subject: [PATCH] corrected tests --- packages/medusa/.vscode/settings.json | 11 ++ packages/medusa/src/api/routes/admin/index.js | 2 +- .../orders/__tests__/create-fulfillment.js | 3 +- .../admin/orders/__tests__/get-order.js | 7 ++ .../medusa/src/services/__tests__/claim.js | 38 +++--- .../medusa/src/services/__tests__/order.js | 108 ++++++++++-------- .../medusa/src/services/__tests__/swap.js | 2 +- packages/medusa/src/services/claim.js | 4 +- packages/medusa/src/services/order.js | 77 ++++++++----- packages/medusa/src/services/swap.js | 16 +-- 10 files changed, 164 insertions(+), 104 deletions(-) create mode 100644 packages/medusa/.vscode/settings.json diff --git a/packages/medusa/.vscode/settings.json b/packages/medusa/.vscode/settings.json new file mode 100644 index 0000000000..f78775143f --- /dev/null +++ b/packages/medusa/.vscode/settings.json @@ -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" +} \ No newline at end of file diff --git a/packages/medusa/src/api/routes/admin/index.js b/packages/medusa/src/api/routes/admin/index.js index febf93e467..a4be5a9c59 100644 --- a/packages/medusa/src/api/routes/admin/index.js +++ b/packages/medusa/src/api/routes/admin/index.js @@ -65,7 +65,7 @@ export default (app, container, config) => { returnRoutes(route) variantRoutes(route) draftOrderRoutes(route) - collectionRoutes(route) //test + collectionRoutes(route) notificationRoutes(route) returnReasonRoutes(route) diff --git a/packages/medusa/src/api/routes/admin/orders/__tests__/create-fulfillment.js b/packages/medusa/src/api/routes/admin/orders/__tests__/create-fulfillment.js index 4a3f04c0ed..c4a074f899 100644 --- a/packages/medusa/src/api/routes/admin/orders/__tests__/create-fulfillment.js +++ b/packages/medusa/src/api/routes/admin/orders/__tests__/create-fulfillment.js @@ -43,8 +43,7 @@ describe("POST /admin/orders/:id/fulfillment", () => { quantity: 1, }, ], - undefined, - undefined + {"metadata": undefined, "noNotification": undefined} ) }) diff --git a/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js b/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js index 57cb1db7ea..42b676dccb 100644 --- a/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js +++ b/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js @@ -17,13 +17,20 @@ const defaultRelations = [ "returns.items.reason", "gift_cards", "gift_card_transactions", + "items", + "items.variant", + "items.variant.product", "claims", "claims.return_order", "claims.shipping_methods", "claims.shipping_address", "claims.additional_items", + "claims.additional_items.variant", + "claims.additional_items.variant.product", "claims.fulfillments", "claims.claim_items", + "claims.claim_items.variant", + "claims.claim_items.variant.product", "claims.claim_items.images", "swaps", "swaps.return_order", diff --git a/packages/medusa/src/services/__tests__/claim.js b/packages/medusa/src/services/__tests__/claim.js index 00cc35fe7e..61b6f03ef4 100644 --- a/packages/medusa/src/services/__tests__/claim.js +++ b/packages/medusa/src/services/__tests__/claim.js @@ -215,19 +215,21 @@ describe("ClaimService", () => { it.each( [ [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({ - ...testClaim, - no_notification: input, - }) - - expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{ - id: expect.any(String), - no_notification: expected + ...testClaim, + no_notification: input, }) - }) + + expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), { + id: expect.any(String), + no_notification: expected, + }) + } + ) }) describe("retrieve", () => { @@ -314,7 +316,9 @@ describe("ClaimService", () => { }) 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).toHaveBeenCalledWith("eventBus") @@ -434,7 +438,10 @@ describe("ClaimService", () => { ) await claimService.createShipment("claim", "ful_123", ["track1234"], { - meta: "data", + metadata: { + meta: "data", + }, + noNotification: false, }) expect(withTransactionMock).toHaveBeenCalledTimes(3) @@ -446,7 +453,12 @@ describe("ClaimService", () => { expect(fulfillmentService.createShipment).toHaveBeenCalledWith( "ful_123", ["track1234"], - { meta: "data" } + { + metadata: { + meta: "data", + }, + noNotification: false, + } ) expect(lineItemService.update).toHaveBeenCalledTimes(1) diff --git a/packages/medusa/src/services/__tests__/order.js b/packages/medusa/src/services/__tests__/order.js index e2783d38e1..4b89496653 100644 --- a/packages/medusa/src/services/__tests__/order.js +++ b/packages/medusa/src/services/__tests__/order.js @@ -866,27 +866,28 @@ describe("OrderService", () => { [true, true], [false, false], [undefined, true], - ])("emits correct no_notification option with '%s'", async (input, expected) => { - await orderService.createFulfillment( - "test-order", - [ - { - item_id: "item_1", - quantity: 1, - }, - ], - input - ) + ])( + "emits correct no_notification option with '%s'", + async (input, expected) => { + await orderService.createFulfillment( + "test-order", + [ + { + item_id: "item_1", + quantity: 1, + }, + ], + { noNotification: 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("registerReturnReceived", () => { const order = { items: [ @@ -1023,7 +1024,11 @@ describe("OrderService", () => { } const fulfillmentService = { - retrieve: () => Promise.resolve({ order_id: IdMap.getId("test") }), + retrieve: () => + Promise.resolve({ + order_id: IdMap.getId("test"), + no_notification: true, + }), createShipment: jest .fn() .mockImplementation((shipmentId, tracking, meta) => { @@ -1063,10 +1068,12 @@ describe("OrderService", () => { ) expect(fulfillmentService.createShipment).toHaveBeenCalledTimes(1) - expect(fulfillmentService.createShipment).toHaveBeenCalledWith( + expect( + fulfillmentService.createShipment + ).toHaveBeenCalledWith( IdMap.getId("fulfillment"), [{ tracking_number: "1234" }, { tracking_number: "2345" }], - {} + { metadata: undefined, noNotification: true } ) expect(orderRepo.save).toHaveBeenCalledTimes(1) @@ -1080,20 +1087,22 @@ describe("OrderService", () => { [true, true], [false, false], [undefined, true], - ])("2emits correct no_notification option with '%s'", async (input, expected) => { - await orderService.createShipment( - IdMap.getId("test"), - IdMap.getId("fulfillment"), - [{ tracking_number: "1234" }, { tracking_number: "2345" }], - input, - {} - ) + ])( + "emits correct no_notification option with '%s'", + async (input, expected) => { + await orderService.createShipment( + IdMap.getId("test"), + IdMap.getId("fulfillment"), + [{ tracking_number: "1234" }, { tracking_number: "2345" }], + { noNotification: 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("createRefund", () => { @@ -1180,20 +1189,23 @@ describe("OrderService", () => { it.each([ [false, false], [undefined, true], - ])("emits correct no_notification option with '%s'", async (input, expected) => { - await orderService.createRefund( - IdMap.getId("order_123"), - 100, - "discount", - "note", - input - ) + ])( + "emits correct no_notification option with '%s'", + async (input, expected) => { + await orderService.createRefund( + IdMap.getId("order_123"), + 100, + "discount", + "note", + { noNotification: input } + ) - expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{ - id: expect.any(String), - no_notification: expected, - refund_id: expect.any(String) - } ) - }) + expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String), { + id: expect.any(String), + no_notification: expected, + refund_id: expect.any(String), + }) + } + ) }) }) diff --git a/packages/medusa/src/services/__tests__/swap.js b/packages/medusa/src/services/__tests__/swap.js index ef56621044..ba9834c456 100644 --- a/packages/medusa/src/services/__tests__/swap.js +++ b/packages/medusa/src/services/__tests__/swap.js @@ -355,7 +355,7 @@ describe("SwapService", () => { id: IdMap.getId("return-shipping"), price: 20, }, - input + {noNotification: input} ) expect(eventBusService.emit).toHaveBeenCalledWith( diff --git a/packages/medusa/src/services/claim.js b/packages/medusa/src/services/claim.js index fc9521266e..68c7d24887 100644 --- a/packages/medusa/src/services/claim.js +++ b/packages/medusa/src/services/claim.js @@ -459,7 +459,7 @@ class ClaimService extends BaseService { } async createShipment(id, fulfillmentId, trackingLinks, config = { - metadata: [], + metadata: {}, noNotification: undefined, }) { const { metadata, noNotification } = config @@ -503,7 +503,7 @@ class ClaimService extends BaseService { .emit(ClaimService.Events.SHIPMENT_CREATED, { id, fulfillment_id: shipment.id, - no_notification: result.no_notification + no_notification: evaluatedNoNotification }) return result diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index 962f24ba1a..7409fa4a6e 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -1,4 +1,3 @@ -import { metadata } from "core-js/fn/reflect" import _ from "lodash" import { Validator, MedusaError } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" @@ -312,7 +311,6 @@ class OrderService extends BaseService { * @return {Promise} the order document */ async retrieve(orderId, config = {}) { - const orderRepo = this.manager_.getCustomRepository(this.orderRepository_) const validatedId = this.validateId_(orderId) @@ -409,7 +407,7 @@ class OrderService extends BaseService { OrderService.Events.COMPLETED, { 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.no_notification = draft.no_notification_order - } const o = await orderRepo.create(toCreate) @@ -557,7 +554,7 @@ class OrderService extends BaseService { .withTransaction(manager) .emit(OrderService.Events.PLACED, { id: result.id, - no_notification: result.no_notification + no_notification: result.no_notification, }) return result @@ -576,10 +573,15 @@ class OrderService extends BaseService { * the fulfillment * @return {order} the resulting order following the update. */ - async createShipment(orderId, fulfillmentId, trackingLinks, config = { - metadata: {}, - noNotification: undefined, - }) { + async createShipment( + orderId, + fulfillmentId, + trackingLinks, + config = { + metadata: {}, + noNotification: undefined, + } + ) { const { metadata, noNotification } = config 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_ .withTransaction(manager) - .createShipment(fulfillmentId, trackingLinks, {metadata, noNotification: evaluatedNoNotification}) + .createShipment(fulfillmentId, trackingLinks, { + metadata, + noNotification: evaluatedNoNotification, + }) order.fulfillment_status = "shipped" for (const item of order.items) { @@ -626,7 +632,7 @@ class OrderService extends BaseService { .emit(OrderService.Events.SHIPMENT_CREATED, { id: orderId, fulfillment_id: shipmentRes.id, - no_notification: evaluatedNoNotification + no_notification: evaluatedNoNotification, }) return result @@ -647,7 +653,7 @@ class OrderService extends BaseService { .withTransaction(manager) .emit(OrderService.Events.PLACED, { id: result.id, - no_notification: order.no_notification + no_notification: order.no_notification, }) return result }) @@ -815,7 +821,7 @@ class OrderService extends BaseService { await this.updateBillingAddress_(order, update.billing_address) } - if("no_notification" in update){ + if ("no_notification" in update) { order.no_notification = update.no_notification } @@ -839,7 +845,7 @@ class OrderService extends BaseService { .withTransaction(manager) .emit(OrderService.Events.UPDATED, { id: orderId, - no_notification: order.no_notification + no_notification: order.no_notification, }) return result }) @@ -890,7 +896,7 @@ class OrderService extends BaseService { .withTransaction(manager) .emit(OrderService.Events.CANCELED, { id: order.id, - no_notification: order.no_notification + no_notification: order.no_notification, }) return result }) @@ -919,7 +925,7 @@ class OrderService extends BaseService { id: orderId, payment_id: p.id, error: err, - no_notification: order.no_notification + no_notification: order.no_notification, }) }) @@ -945,7 +951,7 @@ class OrderService extends BaseService { .withTransaction(manager) .emit(OrderService.Events.PAYMENT_CAPTURED, { 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. * @return {Promise} result of the update operation. */ - async createFulfillment(orderId, itemsToFulfill, config = { + async createFulfillment( + orderId, + itemsToFulfill, + config = { noNotification: undefined, metadata: {}, - }) { + } + ) { const { metadata, noNotification } = config return this.atomicPhase_(async manager => { @@ -1031,7 +1041,6 @@ class OrderService extends BaseService { "Cannot fulfill an order that lacks shipping methods" ) } - const fulfillments = await this.fulfillmentService_ .withTransaction(manager) @@ -1077,7 +1086,8 @@ class OrderService extends BaseService { order.fulfillments = [...order.fulfillments, ...fulfillments] 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) { await this.eventBus_ @@ -1085,7 +1095,7 @@ class OrderService extends BaseService { .emit(OrderService.Events.FULFILLMENT_CREATED, { id: orderId, 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. */ - async createRefund(orderId, refundAmount, reason, note, config = { - noNotification: undefined, - }) { + async createRefund( + orderId, + refundAmount, + reason, + note, + config = { + noNotification: undefined, + } + ) { const { noNotification } = config return this.atomicPhase_(async manager => { @@ -1165,12 +1181,13 @@ class OrderService extends BaseService { 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, { id: result.id, refund_id: refund.id, - no_notification: evaluatedNoNotification + no_notification: evaluatedNoNotification, }) return result }) @@ -1274,7 +1291,7 @@ class OrderService extends BaseService { .emit(OrderService.Events.RETURN_ACTION_REQUIRED, { id: result.id, return_id: receivedReturn.id, - no_notification: receivedReturn.no_notification + no_notification: receivedReturn.no_notification, }) return result } @@ -1306,7 +1323,7 @@ class OrderService extends BaseService { .emit(OrderService.Events.ITEMS_RETURNED, { id: order.id, return_id: receivedReturn.id, - no_notification: receivedReturn.no_notification + no_notification: receivedReturn.no_notification, }) return result }) diff --git a/packages/medusa/src/services/swap.js b/packages/medusa/src/services/swap.js index 09f467d82b..2b4217bdf1 100644 --- a/packages/medusa/src/services/swap.js +++ b/packages/medusa/src/services/swap.js @@ -215,9 +215,11 @@ class SwapService extends BaseService { returnItems, additionalItems, returnShipping, - custom = {} - ) { - + custom = { + noNotification: undefined + } + ){ + const { noNotification, ...rest } = custom return this.atomicPhase_(async manager => { if ( 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 swapRepo = manager.getCustomRepository(this.swapRepository_) @@ -746,9 +746,11 @@ class SwapService extends BaseService { await this.eventBus_ .withTransaction(manager) - .emit(SwapService.Events.FULFILLMENT_CREATED, { + .emit(SwapService.Events.FULFILLMENT_CREATED, + + { id: swapId, - fulfillment_id: shipment.id, + fulfillment_id: result.id, no_notification: evaluatedNoNotification })