mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
Feat/lhac 202 reshape images and money (#4)
* feat: add reshape functions for money and image
This commit is contained in:
parent
dc586ce64a
commit
397efc9ff4
67
lib/commercetools/index.jest.test.ts
Normal file
67
lib/commercetools/index.jest.test.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import { reshapeMoney, reshapeImage } from ".";
|
||||
import {
|
||||
CentPrecisionMoney as CommercetoolsCentPrecisionMoney,
|
||||
HighPrecisionMoney as CommercetoolsHighPrecisionMoney
|
||||
} from "@commercetools/platform-sdk";
|
||||
|
||||
describe("reshape & fetch functions", () => {
|
||||
describe("reshapeMoney", () => {
|
||||
it("returns correct amount for cent precision", () => {
|
||||
const centPrecisionMoney: CommercetoolsCentPrecisionMoney = {
|
||||
type: "centPrecision",
|
||||
centAmount: 1001,
|
||||
currencyCode: "EUR",
|
||||
fractionDigits: 2
|
||||
};
|
||||
|
||||
const result = reshapeMoney(centPrecisionMoney);
|
||||
|
||||
const expectedResult = { amount: "10.01", currencyCode: "EUR" };
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
|
||||
it("returns correct amount for high precision", () => {
|
||||
const highPrecisionMoney: CommercetoolsHighPrecisionMoney = {
|
||||
type: "highPrecision",
|
||||
preciseAmount: 100001,
|
||||
centAmount: 1000,
|
||||
currencyCode: "EUR",
|
||||
fractionDigits: 4
|
||||
};
|
||||
|
||||
const result = reshapeMoney(highPrecisionMoney);
|
||||
|
||||
const expectedResult = { amount: "10.0001", currencyCode: "EUR" };
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe("reshapeImage", () => {
|
||||
it("returns correctly reshaped image with alt text", () => {
|
||||
const image = {
|
||||
url: "an-url",
|
||||
dimensions: { w: 800, h: 600 },
|
||||
label: "a-label"
|
||||
};
|
||||
|
||||
const result = reshapeImage(image);
|
||||
|
||||
const expectedResult = { url: "an-url", width: 800, height: 600, altText: "a-label" };
|
||||
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
|
||||
it("returns correctly reshaped image with empty string as alt text", () => {
|
||||
const image = {
|
||||
url: "an-url",
|
||||
dimensions: { w: 800, h: 600 }
|
||||
};
|
||||
|
||||
const result = reshapeImage(image);
|
||||
|
||||
const expectedResult = { url: "an-url", width: 800, height: 600, altText: "" };
|
||||
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,25 @@
|
||||
import {
|
||||
TypedMoney as CommercetoolsTypedMoney,
|
||||
Image as CommercetoolsImage,
|
||||
CentPrecisionMoney,
|
||||
HighPrecisionMoney,
|
||||
TypedMoney
|
||||
} from "@commercetools/platform-sdk";
|
||||
import { Image, Money } from "./types";
|
||||
|
||||
export function reshapeMoney(typedMoney: TypedMoney): Money {
|
||||
const { fractionDigits, currencyCode, type } = typedMoney;
|
||||
const typedAmount = type === "centPrecision" ? typedMoney.centAmount : typedMoney.preciseAmount;
|
||||
|
||||
const amount = (typedAmount / Math.pow(10, fractionDigits)).toString();
|
||||
return { amount, currencyCode };
|
||||
}
|
||||
|
||||
export function reshapeImage(image: CommercetoolsImage): Image {
|
||||
return {
|
||||
url: image.url,
|
||||
width: image.dimensions.w,
|
||||
height: image.dimensions.h,
|
||||
altText: image.label || ""
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user