|
1 | | -import { type GrantRefundInputLine, squashLines } from "./useRefundWithinReturn"; |
| 1 | +import { type FormsetQuantityData } from "@dashboard/orders/components/OrderReturnPage/form"; |
| 2 | + |
| 3 | +import { |
| 4 | + type GrantRefundInputLine, |
| 5 | + prepareGrantRefundLines, |
| 6 | + squashLines, |
| 7 | +} from "./useRefundWithinReturn"; |
| 8 | + |
| 9 | +const getQuantityLine = ({ |
| 10 | + id, |
| 11 | + orderLineId, |
| 12 | + value, |
| 13 | +}: { |
| 14 | + id: string; |
| 15 | + orderLineId: string; |
| 16 | + value: number; |
| 17 | +}): FormsetQuantityData[number] => ({ |
| 18 | + id, |
| 19 | + label: "", |
| 20 | + value, |
| 21 | + data: { |
| 22 | + isFulfillment: true, |
| 23 | + isRefunded: false, |
| 24 | + orderLineId, |
| 25 | + }, |
| 26 | +}); |
| 27 | + |
| 28 | +describe("prepareGrantRefundLines", () => { |
| 29 | + it("includes fulfilled, waiting for approval and unfulfilled lines in the refund payload", () => { |
| 30 | + // Arrange |
| 31 | + const fulfilledItemsQuantities: FormsetQuantityData = [ |
| 32 | + getQuantityLine({ id: "fulfillment-line-1", orderLineId: "order-line-1", value: 2 }), |
| 33 | + ]; |
| 34 | + const waitingItemsQuantities: FormsetQuantityData = [ |
| 35 | + getQuantityLine({ id: "fulfillment-line-2", orderLineId: "order-line-2", value: 1 }), |
| 36 | + ]; |
| 37 | + const unfulfilledItemsQuantities: FormsetQuantityData = [ |
| 38 | + getQuantityLine({ id: "order-line-3", orderLineId: "order-line-3", value: 3 }), |
| 39 | + ]; |
| 40 | + |
| 41 | + // Act |
| 42 | + const result = prepareGrantRefundLines({ |
| 43 | + fulfilledItemsQuantities, |
| 44 | + waitingItemsQuantities, |
| 45 | + unfulfilledItemsQuantities, |
| 46 | + }); |
| 47 | + |
| 48 | + // Assert |
| 49 | + expect(result).toEqual([ |
| 50 | + { id: "order-line-1", quantity: 2 }, |
| 51 | + { id: "order-line-2", quantity: 1 }, |
| 52 | + { id: "order-line-3", quantity: 3 }, |
| 53 | + ]); |
| 54 | + }); |
| 55 | + it("maps waiting for approval lines to order line ids", () => { |
| 56 | + // Arrange |
| 57 | + const waitingItemsQuantities: FormsetQuantityData = [ |
| 58 | + getQuantityLine({ id: "fulfillment-line-1", orderLineId: "order-line-1", value: 4 }), |
| 59 | + ]; |
| 60 | + |
| 61 | + // Act |
| 62 | + const result = prepareGrantRefundLines({ |
| 63 | + fulfilledItemsQuantities: [], |
| 64 | + waitingItemsQuantities, |
| 65 | + unfulfilledItemsQuantities: [], |
| 66 | + }); |
| 67 | + |
| 68 | + // Assert |
| 69 | + expect(result).toEqual([{ id: "order-line-1", quantity: 4 }]); |
| 70 | + }); |
| 71 | + it("squashes lines referencing the same order line", () => { |
| 72 | + // Arrange |
| 73 | + const fulfilledItemsQuantities: FormsetQuantityData = [ |
| 74 | + getQuantityLine({ id: "fulfillment-line-1", orderLineId: "order-line-1", value: 2 }), |
| 75 | + ]; |
| 76 | + const waitingItemsQuantities: FormsetQuantityData = [ |
| 77 | + getQuantityLine({ id: "fulfillment-line-2", orderLineId: "order-line-1", value: 1 }), |
| 78 | + ]; |
| 79 | + |
| 80 | + // Act |
| 81 | + const result = prepareGrantRefundLines({ |
| 82 | + fulfilledItemsQuantities, |
| 83 | + waitingItemsQuantities, |
| 84 | + unfulfilledItemsQuantities: [], |
| 85 | + }); |
| 86 | + |
| 87 | + // Assert |
| 88 | + expect(result).toEqual([{ id: "order-line-1", quantity: 3 }]); |
| 89 | + }); |
| 90 | +}); |
2 | 91 |
|
3 | 92 | describe("squashLines", () => { |
4 | 93 | it("merges items with the same ID", () => { |
|
0 commit comments