Skip to content

Commit

Permalink
test: add more unit tests at utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendn committed Oct 29, 2024
1 parent a4fea40 commit 1e1de97
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/berajs/src/utils/encoder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, test } from "@jest/globals";
import { encrypt, decrypt } from "./encoder";

describe("Encoder Utils", () => {
const message = "Hello, World!";
const key = "secretkey123";

test("encrypt should return a non-empty string", () => {
const cipherText = encrypt(message, key);
expect(typeof cipherText).toBe("string");
expect(cipherText.length).toBeGreaterThan(0);
});

test("decrypt should return the original message with 0x prefix", () => {
const cipherText = encrypt(message, key);
const decryptedMessage = decrypt(cipherText, key);
expect(decryptedMessage).toBe(`0x${message}`);
});

test('decrypt should return "0x" for invalid cipher', () => {
const invalidCipher = "invalidciphertext";
const result = decrypt(invalidCipher, key);
expect(result).toBe("0x");
});
});
17 changes: 17 additions & 0 deletions packages/berajs/src/utils/errorMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, test } from "@jest/globals";

import { getCustomAppErrorMessages, getErrorMessage } from "./errorMessages";

describe("Error Messages Utils", () => {
test("getCustomAppErrorMessages should return correct error message for PERPS", () => {
const error = { metaMessages: ["PriceImpactTooHigh"] };
const message = getCustomAppErrorMessages(error, "PERPS");
expect(message).toBe("This position causes too much price impact.");
});

test("getErrorMessage should return GENERAL_ERROR for unknown error", () => {
const error = { unknownKey: "unknownValue" };
const message = getErrorMessage(error);
expect(message).toBe("Something went wrong. Please try again later.");
});
});
33 changes: 33 additions & 0 deletions packages/berajs/src/utils/fixedPointEthers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, test } from "@jest/globals";
import {
toQ64,
fromQ64,
toQ48,
fromQ48,
toSqrtPrice,
fromSqrtPrice,
} from "./fixedPointEthers";
import { BigNumber } from "ethers";

describe("Fixed Point Ethers Utils", () => {
test("toQ64 and fromQ64 should correctly convert values", () => {
const value = 123.456;
const q64Value = toQ64(value);
const result = fromQ64(q64Value);
expect(result).toBeCloseTo(value, 5);
});

test("toQ48 and fromQ48 should correctly convert values", () => {
const value = 789.012;
const q48Value = toQ48(value);
const result = fromQ48(q48Value);
expect(result).toBeCloseTo(value, 5);
});

test("toSqrtPrice and fromSqrtPrice should correctly convert values", () => {
const price = 10000;
const sqrtPrice = toSqrtPrice(price);
const result = fromSqrtPrice(BigNumber.from(sqrtPrice));
expect(result).toBeCloseTo(price, 5);
});
});
27 changes: 27 additions & 0 deletions packages/berajs/src/utils/formatAmountSmall.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, test } from "@jest/globals";

import { formatAmountSmall } from "./formatAmountSmall";

describe("formatAmountSmall", () => {
test("should return isSmall as false and numericValue as 0 for NaN or zero", () => {
expect(formatAmountSmall("abc")).toEqual({
isSmall: false,
numericValue: 0,
});
expect(formatAmountSmall(0)).toEqual({ isSmall: false, numericValue: 0 });
});

test("should return isSmall as true for values less than 0.01", () => {
expect(formatAmountSmall(0.005)).toEqual({
isSmall: true,
numericValue: 0.01,
});
});

test("should return isSmall as false for values greater than or equal to 0.01", () => {
expect(formatAmountSmall(0.02)).toEqual({
isSmall: false,
numericValue: 0.02,
});
});
});
18 changes: 18 additions & 0 deletions packages/berajs/src/utils/formatNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, test } from "@jest/globals";

import { formatNumber } from "./formatNumber";

describe("formatNumber", () => {
test("should format numbers with default 8 decimals", () => {
expect(formatNumber(1234.56789)).toBe("1234.56789");
});

test("should format numbers with specified decimals", () => {
expect(formatNumber(1234.56789, 2)).toBe("1234.57");
});

test("should remove trailing zeros", () => {
expect(formatNumber(1234.5)).toBe("1234.5");
expect(formatNumber(1234.0)).toBe("1234");
});
});
18 changes: 18 additions & 0 deletions packages/berajs/src/utils/formatUsd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, test } from "@jest/globals";

import { formatUsd } from "./formatUsd";

describe("formatUsd with en-US locale", () => {
test("should format numbers as USD currency", () => {
expect(formatUsd(1234.56)).toBe("$1,234.56");
});

test("should format strings as USD currency", () => {
expect(formatUsd("1234.56")).toBe("$1,234.56");
});

test("should return $0.00 for invalid inputs", () => {
expect(formatUsd("abc")).toBe("$0.00");
expect(formatUsd({} as any)).toBe("$0.00");
});
});
29 changes: 29 additions & 0 deletions packages/berajs/src/utils/handle-native-bera.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, test, jest } from "@jest/globals";
import { handleNativeBera } from './handle-native-bera';
import { beraTokenAddress, nativeTokenAddress } from '@bera/config';
import { getAddress } from 'viem';

// From .env
jest.mock('@bera/config', () => ({
beraTokenAddress: '0x7507c1dc16935B82698e4C63f2746A2fCf994dF8',
nativeTokenAddress: '0x0000000000000000000000000000000000000000',
}));

describe('handleNativeBera', () => {
test('should return beraTokenAddress if tokenAddress is nativeTokenAddress', () => {
const result = handleNativeBera(nativeTokenAddress);
expect(result).toBe(getAddress(beraTokenAddress));
});

test('should return the same address if tokenAddress is not nativeTokenAddress', () => {
const someOtherAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const result = handleNativeBera(someOtherAddress);
expect(result).toBe(getAddress(someOtherAddress));
});

test('should return the input if it is not a valid address', () => {
const invalidAddress = 'invalidAddress';
const result = handleNativeBera(invalidAddress);
expect(result).toBe(invalidAddress);
});
});
29 changes: 29 additions & 0 deletions packages/berajs/src/utils/timeFrame.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, test } from "@jest/globals";

import { TimeFrame, getTime, timeFrameToNumber } from "./timeFrame";

describe("TimeFrame Utils", () => {
test("should calculate correct time for HOURLY", () => {
const currentTime = Math.floor(Date.now() / 1000);
const expectedTime = currentTime - timeFrameToNumber[TimeFrame.HOURLY];
expect(getTime(TimeFrame.HOURLY)).toBeCloseTo(expectedTime, 1);
});

test("should calculate correct time for WEEKLY", () => {
const currentTime = Math.floor(Date.now() / 1000);
const expectedTime = currentTime - timeFrameToNumber[TimeFrame.WEEKLY];
expect(getTime(TimeFrame.WEEKLY)).toBeCloseTo(expectedTime, 1);
});

test("should calculate correct time for MONTHLY", () => {
const currentTime = Math.floor(Date.now() / 1000);
const expectedTime = currentTime - timeFrameToNumber[TimeFrame.MONTHLY];
expect(getTime(TimeFrame.MONTHLY)).toBeCloseTo(expectedTime, 1);
});

test("should calculate correct time for QUARTERLY", () => {
const currentTime = Math.floor(Date.now() / 1000);
const expectedTime = currentTime - timeFrameToNumber[TimeFrame.QUARTERLY];
expect(getTime(TimeFrame.QUARTERLY)).toBeCloseTo(expectedTime, 1);
});
});

0 comments on commit 1e1de97

Please sign in to comment.