fix: adds tests for segment
This commit is contained in:
188
packages/medusa-plugin-segment/src/services/__tests__/segment.js
Normal file
188
packages/medusa-plugin-segment/src/services/__tests__/segment.js
Normal file
@@ -0,0 +1,188 @@
|
||||
import SegmentService from "../segment"
|
||||
|
||||
jest.mock("analytics-node")
|
||||
|
||||
const orderFactory = (config = {}) => {
|
||||
return {
|
||||
id: "12355",
|
||||
display_id: "1234",
|
||||
cart_id: "cart_13",
|
||||
region_id: "reg_123",
|
||||
items: [
|
||||
{
|
||||
title: "Test",
|
||||
variant: {
|
||||
product_id: "prod_123",
|
||||
sku: "TEST",
|
||||
},
|
||||
unit_price: 1100,
|
||||
quantity: 2,
|
||||
},
|
||||
],
|
||||
shipping_methods: [
|
||||
{
|
||||
name: "standard",
|
||||
price: 12399,
|
||||
},
|
||||
],
|
||||
payments: [
|
||||
{
|
||||
id: "123",
|
||||
},
|
||||
],
|
||||
tax_rate: 23.1,
|
||||
currency_code: "DKK",
|
||||
discounts: [],
|
||||
shipping_address: {
|
||||
first_name: "Test",
|
||||
last_name: "Testson",
|
||||
address_1: "Test",
|
||||
address_2: "TEst",
|
||||
postal_code: "1234",
|
||||
country_code: "DK",
|
||||
phone: "12345678",
|
||||
},
|
||||
email: "test@example.com",
|
||||
subtotal: 2200,
|
||||
total: 12399,
|
||||
tax_total: 0,
|
||||
shipping_total: 12399,
|
||||
discount_total: 0,
|
||||
gift_card_total: 0,
|
||||
...config,
|
||||
}
|
||||
}
|
||||
|
||||
describe("SegmentService", () => {
|
||||
const ProductService = {
|
||||
retrieve: () =>
|
||||
Promise.resolve({
|
||||
collection: { title: "Collection" },
|
||||
type: { value: "Type" },
|
||||
subtitle: "Subtitle",
|
||||
}),
|
||||
}
|
||||
|
||||
const TotalsService = {
|
||||
getLineItemRefund: (_, item) => {
|
||||
return item.unit_price
|
||||
},
|
||||
}
|
||||
|
||||
describe("buildOrder", () => {
|
||||
const segmentService = new SegmentService(
|
||||
{
|
||||
productService: ProductService,
|
||||
totalsService: TotalsService,
|
||||
},
|
||||
{ account: "test" }
|
||||
)
|
||||
|
||||
segmentService.getReportingValue = async (_, v) => {
|
||||
const num = v
|
||||
return Promise.resolve(Number(Math.round(num + "e2") + "e-2"))
|
||||
}
|
||||
|
||||
it("successfully builds sales order", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
const order = orderFactory()
|
||||
const segmentOrder = await segmentService.buildOrder(order)
|
||||
|
||||
expect(segmentOrder).toEqual({
|
||||
checkout_id: "cart_13",
|
||||
coupon: undefined,
|
||||
currency: "DKK",
|
||||
discount: 0,
|
||||
email: "test@example.com",
|
||||
order_id: "12355",
|
||||
payment_provider: "",
|
||||
products: [
|
||||
{
|
||||
category: "Collection",
|
||||
name: "Test",
|
||||
price: 4.47,
|
||||
product_id: "prod_123",
|
||||
quantity: 2,
|
||||
reporting_revenue: 8.94,
|
||||
sku: "",
|
||||
subtitle: "Subtitle",
|
||||
type: "Type",
|
||||
variant: "TEST",
|
||||
},
|
||||
],
|
||||
region_id: "reg_123",
|
||||
reporting_discount: 0,
|
||||
reporting_revenue: 123.99,
|
||||
reporting_shipping: 123.99,
|
||||
reporting_subtotal: 22,
|
||||
reporting_tax: 0,
|
||||
reporting_total: 123.99,
|
||||
revenue: 123.99,
|
||||
shipping: 123.99,
|
||||
shipping_city: undefined,
|
||||
shipping_country: "DK",
|
||||
shipping_methods: [
|
||||
{
|
||||
name: "standard",
|
||||
price: 12399,
|
||||
},
|
||||
],
|
||||
subtotal: 22,
|
||||
tax: 0,
|
||||
total: 123.99,
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully builds order with zero decimal currency", async () => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
const order = orderFactory({ currency_code: "krw" })
|
||||
const segmentOrder = await segmentService.buildOrder(order)
|
||||
|
||||
expect(segmentOrder).toEqual({
|
||||
checkout_id: "cart_13",
|
||||
coupon: undefined,
|
||||
currency: "KRW",
|
||||
discount: 0,
|
||||
email: "test@example.com",
|
||||
order_id: "12355",
|
||||
payment_provider: "",
|
||||
products: [
|
||||
{
|
||||
category: "Collection",
|
||||
name: "Test",
|
||||
price: 446.79,
|
||||
product_id: "prod_123",
|
||||
quantity: 2,
|
||||
reporting_revenue: 893.58,
|
||||
sku: "",
|
||||
subtitle: "Subtitle",
|
||||
type: "Type",
|
||||
variant: "TEST",
|
||||
},
|
||||
],
|
||||
region_id: "reg_123",
|
||||
reporting_discount: 0,
|
||||
reporting_revenue: 12399,
|
||||
reporting_shipping: 12399,
|
||||
reporting_subtotal: 2200,
|
||||
reporting_tax: 0,
|
||||
reporting_total: 12399,
|
||||
revenue: 12399,
|
||||
shipping: 12399,
|
||||
shipping_city: undefined,
|
||||
shipping_country: "DK",
|
||||
shipping_methods: [
|
||||
{
|
||||
name: "standard",
|
||||
price: 12399,
|
||||
},
|
||||
],
|
||||
subtotal: 2200,
|
||||
tax: 0,
|
||||
total: 12399,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -44,7 +44,7 @@ class SegmentService extends BaseService {
|
||||
"EUR"
|
||||
|
||||
if (fromCurrency === toCurrency) {
|
||||
return this.totalsService_.rounded(value)
|
||||
return this.rounded_(value)
|
||||
}
|
||||
|
||||
const exchangeRate = await axios
|
||||
@@ -55,9 +55,7 @@ class SegmentService extends BaseService {
|
||||
return data.rates[fromCurrency]
|
||||
})
|
||||
|
||||
// Two decimal places
|
||||
const num = value / exchangeRate
|
||||
return Number(Math.round(num + "e2") + "e-2")
|
||||
return this.rounded_(value / exchangeRate)
|
||||
}
|
||||
|
||||
async buildOrder(order) {
|
||||
@@ -146,7 +144,9 @@ class SegmentService extends BaseService {
|
||||
return {
|
||||
name,
|
||||
variant,
|
||||
price: humanizeAmount(lineTotal, curr) / item.quantity,
|
||||
price: this.rounded_(
|
||||
humanizeAmount(lineTotal, curr) / item.quantity
|
||||
),
|
||||
reporting_revenue: revenue,
|
||||
product_id: item.variant.product_id,
|
||||
category: product.collection?.title,
|
||||
@@ -161,6 +161,10 @@ class SegmentService extends BaseService {
|
||||
|
||||
return orderData
|
||||
}
|
||||
|
||||
rounded_(v) {
|
||||
return Number(Math.round(v + "e2") + "e-2")
|
||||
}
|
||||
}
|
||||
|
||||
export default SegmentService
|
||||
|
||||
Reference in New Issue
Block a user