Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Oct 10, 2024
1 parent 007d36c commit bb32977
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1,686 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe("SaleorOrderToAvataxLinesTransformer", () => {
it("should transform lines and shipping from order into product and shipping lines ", () => {
const { order } = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();

const theOrder = order!;

expect(
saleorOrderToAvataxLinesTransformer.transform({
confirmedOrderEvent: saleorConfirmedOrderEvent,
Expand All @@ -26,16 +28,16 @@ describe("SaleorOrderToAvataxLinesTransformer", () => {
}),
).toStrictEqual([
{
amount: order.lines[0].totalPrice.gross.amount,
description: order.lines[0].productName,
itemCode: order.lines[0].productSku,
quantity: order.lines[0].quantity,
amount: theOrder.lines[0].totalPrice.gross.amount,
description: theOrder.lines[0].productName,
itemCode: theOrder.lines[0].productSku,
quantity: theOrder.lines[0].quantity,
taxCode: DEFAULT_TAX_CLASS_ID,
taxIncluded: true,
discounted: undefined,
},
{
amount: order.shippingPrice.gross.amount,
amount: theOrder.shippingPrice.gross.amount,
itemCode: SHIPPING_ITEM_CODE,
quantity: 1,
taxCode: avataxConfigMock.shippingTaxCode,
Expand All @@ -48,23 +50,23 @@ describe("SaleorOrderToAvataxLinesTransformer", () => {
it("should transform only lines from order into product if there is no shipping", () => {
const orderConfirmedEventPayload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();

const saleorConfirmedOrderEventWithoutShipping = SaleorOrderConfirmedEventMockFactory.create({
...orderConfirmedEventPayload,
order: {
...orderConfirmedEventPayload.order,
shippingPrice: {
gross: {
amount: 0,
},
net: {
amount: 0,
},
},
orderConfirmedEventPayload.order!.shippingPrice = {
gross: {
amount: 0,
},
net: {
amount: 0,
},
});
};

const saleorConfirmedOrderEventWithoutShipping = SaleorOrderConfirmedEventMockFactory.create(
orderConfirmedEventPayload,
);

const { order } = orderConfirmedEventPayload;

const theOrder = order!;

expect(
saleorOrderToAvataxLinesTransformer.transform({
confirmedOrderEvent: saleorConfirmedOrderEventWithoutShipping,
Expand All @@ -74,10 +76,10 @@ describe("SaleorOrderToAvataxLinesTransformer", () => {
}),
).toStrictEqual([
{
amount: order.lines[0].totalPrice.gross.amount,
description: order.lines[0].productName,
itemCode: order.lines[0].productSku,
quantity: order.lines[0].quantity,
amount: theOrder.lines[0].totalPrice.gross.amount,
description: theOrder.lines[0].productName,
itemCode: theOrder.lines[0].productSku,
quantity: theOrder.lines[0].quantity,
taxCode: DEFAULT_TAX_CLASS_ID,
taxIncluded: true,
discounted: undefined,
Expand Down
106 changes: 47 additions & 59 deletions apps/avatax/src/modules/saleor/order-confirmed/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe("SaleorOrderConfirmedEvent", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();
const result = SaleorOrderConfirmedEvent.createFromGraphQL({
...payload,
// @ts-expect-error testing
order: null,
});

Expand All @@ -38,13 +37,10 @@ describe("SaleorOrderConfirmedEvent", () => {

it("should return false if order has other status than FULLFILLED", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();
const event = SaleorOrderConfirmedEvent.createFromGraphQL({
...payload,
order: {
...payload.order,
status: "CANCELED",
},
})._unsafeUnwrap();

payload.order!.status = "CANCELED";

const event = SaleorOrderConfirmedEvent.createFromGraphQL(payload)._unsafeUnwrap();

expect(event.isFulfilled()).toBe(false);
});
Expand All @@ -60,20 +56,17 @@ describe("SaleorOrderConfirmedEvent", () => {

it("should return true if order has tax calculation strategy set to FLAT_RATES", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();
const event = SaleorOrderConfirmedEvent.createFromGraphQL({
...payload,
order: {
...payload.order,
channel: {
slug: "channel-slug",
id: "channel-id",
taxConfiguration: {
pricesEnteredWithTax: true,
taxCalculationStrategy: "FLAT_RATES",
},
},

payload.order!.channel = {
slug: "channel-slug",
id: "channel-id",
taxConfiguration: {
pricesEnteredWithTax: true,
taxCalculationStrategy: "FLAT_RATES",
},
})._unsafeUnwrap();
};

const event = SaleorOrderConfirmedEvent.createFromGraphQL(payload)._unsafeUnwrap();

expect(event.isStrategyFlatRates()).toBe(true);
});
Expand All @@ -82,20 +75,17 @@ describe("SaleorOrderConfirmedEvent", () => {
describe("hasShipping method", () => {
it("should return false if order has shippingPrice net set to 0", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();
const event = SaleorOrderConfirmedEvent.createFromGraphQL({
...payload,
order: {
...payload.order,
shippingPrice: {
gross: {
amount: 10,
},
net: {
amount: 0,
},
},

payload.order!.shippingPrice = {
gross: {
amount: 10,
},
})._unsafeUnwrap();
net: {
amount: 0,
},
};

const event = SaleorOrderConfirmedEvent.createFromGraphQL(payload)._unsafeUnwrap();

expect(event.hasShipping()).toEqual(false);
});
Expand All @@ -113,40 +103,38 @@ describe("SaleorOrderConfirmedEvent", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();
const event = SaleorOrderConfirmedEvent.createFromGraphQL(payload)._unsafeUnwrap();

expect(event.getShippingAmount()).toEqual(payload.order.shippingPrice.gross.amount);
expect(event.getShippingAmount()).toEqual(payload.order!.shippingPrice.gross.amount);
});

it("should get shipping amount as shippingPrice net without tax included", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();
const event = SaleorOrderConfirmedEvent.createFromGraphQL({
...payload,
order: {
...payload.order,
channel: {
slug: "channel-slug",
id: "channel-id",
taxConfiguration: {
pricesEnteredWithTax: false,
taxCalculationStrategy: "FLAT_RATES",
},
},

payload.order!.channel = {
slug: "channel-slug",
id: "channel-id",
taxConfiguration: {
pricesEnteredWithTax: false,
taxCalculationStrategy: "FLAT_RATES",
},
})._unsafeUnwrap();
};

const event = SaleorOrderConfirmedEvent.createFromGraphQL(payload)._unsafeUnwrap();

expect(event.getShippingAmount()).toEqual(payload.order.shippingPrice.net.amount);
expect(event.getShippingAmount()).toEqual(payload.order!.shippingPrice.net.amount);
});
});

describe("resolveUserEmailOrEmpty", () => {
it("Returns order.user.email if exists", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();

payload.order.user = {
payload.order!.user = {
email: "[email protected]",
id: "1",
avataxCustomerCode: null,
};

payload.order.userEmail = "[email protected]";
payload.order!.userEmail = "[email protected]";

const result = SaleorOrderConfirmedEvent.createFromGraphQL(payload);

Expand All @@ -156,9 +144,9 @@ describe("SaleorOrderConfirmedEvent", () => {
it("Returns order.userEmail.email if exists and user.email doesnt", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();

payload.order.user = undefined;
payload.order!.user = null;

payload.order.userEmail = "[email protected]";
payload.order!.userEmail = "[email protected]";

const result = SaleorOrderConfirmedEvent.createFromGraphQL(payload);

Expand All @@ -168,8 +156,8 @@ describe("SaleorOrderConfirmedEvent", () => {
it("Returns empty string if neither user.email or userEmail exist", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();

payload.order.user = null;
payload.order.userEmail = null;
payload.order!.user = null;
payload.order!.userEmail = null;

const result = SaleorOrderConfirmedEvent.createFromGraphQL(payload);

Expand All @@ -180,7 +168,7 @@ describe("SaleorOrderConfirmedEvent", () => {
describe("Resolving addresses", () => {
const payload = SaleorOrderConfirmedEventMockFactory.getGraphqlPayload();

payload.order.shippingAddress = {
payload.order!.shippingAddress = {
streetAddress2: "streetAddress2-shipping",
city: "city-shipping",
postalCode: "postalCode-shipping",
Expand All @@ -191,7 +179,7 @@ describe("SaleorOrderConfirmedEvent", () => {
streetAddress1: "streetAddress1-shipping",
};

payload.order.billingAddress = {
payload.order!.billingAddress = {
streetAddress2: "streetAddress2-billing",
city: "city-billing",
postalCode: "postalCode-billing",
Expand All @@ -205,11 +193,11 @@ describe("SaleorOrderConfirmedEvent", () => {
const event = SaleorOrderConfirmedEvent.createFromGraphQL(payload)._unsafeUnwrap();

it("Returns shipping address", () => {
expect(event.getOrderShippingAddress()).toEqual(payload.order.shippingAddress);
expect(event.getOrderShippingAddress()).toEqual(payload.order!.shippingAddress);
});

it("Returns billing address", () => {
expect(event.getOrderBillingAddress()).toEqual(payload.order.billingAddress);
expect(event.getOrderBillingAddress()).toEqual(payload.order!.billingAddress);
});
});
});
9 changes: 7 additions & 2 deletions apps/avatax/src/modules/saleor/order-confirmed/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ export class SaleorOrderConfirmedEventMockFactory {
} satisfies NonNullable<ResultOf<typeof OrderConfirmedFragment>>;

return {
issuedAt: "2021-01-01T00:00:00Z",
recipient: { privateMetadata: [] },
version: "",
order: o,
} as const;
__typename: "OrderConfirmed",
} satisfies OrderConfirmedPayload;
};

static create(graphqlPayload?: OrderConfirmedPayload) {
Expand All @@ -84,5 +88,6 @@ export class SaleorOrderConfirmedEventMockFactory {
return possibleOrderLine.value;
}

static getGraphqlPayload = () => SaleorOrderConfirmedEventMockFactory._getGraphqlPayload();
static getGraphqlPayload = (): OrderConfirmedPayload =>
SaleorOrderConfirmedEventMockFactory._getGraphqlPayload();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { verifyCalculateTaxesPayload } from "./validate-webhook-payload";

const getBasePayload = (): CalculateTaxesPayload => {
return {
issuedAt: new Date(2020, 1, 1).toISOString(),
version: "",
__typename: "CalculateTaxes",
recipient: {
privateMetadata: [],
Expand Down
Loading

0 comments on commit bb32977

Please sign in to comment.