Skip to content

Commit ae02146

Browse files
committed
update
1 parent 1b103d7 commit ae02146

18 files changed

+791
-5
lines changed

packages/thirdweb/src/exports/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// bytecode
22
export { detectMethod } from "../utils/bytecode/detectExtension.js";
33
export { extractIPFSUri } from "../utils/bytecode/extractIPFS.js";
4-
export { extractMinimalProxyImplementationAddress } from "../utils/bytecode/extractMnimalProxyImplementationAddress.js";
4+
export { extractMinimalProxyImplementationAddress } from "../utils/bytecode/extractMinimalProxyImplementationAddress.js";
55
export { isContractDeployed } from "../utils/bytecode/is-contract-deployed.js";
66
export { ensureBytecodePrefix } from "../utils/bytecode/prefix.js";
77
export { resolveImplementation } from "../utils/bytecode/resolveImplementation.js";
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from "vitest";
2+
import { CADIcon } from "../../../icons/currencies/CADIcon.js";
3+
import { EURIcon } from "../../../icons/currencies/EURIcon.js";
4+
import { GBPIcon } from "../../../icons/currencies/GBPIcon.js";
5+
import { JPYIcon } from "../../../icons/currencies/JPYIcon.js";
6+
import { USDIcon } from "../../../icons/currencies/USDIcon.js";
7+
import { currencies, getCurrencyMeta, usdCurrency } from "./currencies.js";
8+
9+
describe("Currency Utilities", () => {
10+
it("should have correct number of currencies", () => {
11+
expect(currencies.length).toBe(5);
12+
});
13+
14+
it("should have USD as the first currency", () => {
15+
expect(currencies[0]).toEqual(usdCurrency);
16+
});
17+
18+
it("should have correct properties for each currency", () => {
19+
for (const currency of currencies) {
20+
expect(currency).toHaveProperty("shorthand");
21+
expect(currency).toHaveProperty("name");
22+
expect(currency).toHaveProperty("icon");
23+
}
24+
});
25+
26+
describe("getCurrencyMeta function", () => {
27+
it("should return correct currency meta for valid shorthand", () => {
28+
const cadMeta = getCurrencyMeta("CAD");
29+
expect(cadMeta.shorthand).toBe("CAD");
30+
expect(cadMeta.name).toBe("Canadian Dollar");
31+
expect(cadMeta.icon).toBe(CADIcon);
32+
});
33+
34+
it("should be case-insensitive", () => {
35+
const eurMeta = getCurrencyMeta("eur");
36+
expect(eurMeta.shorthand).toBe("EUR");
37+
expect(eurMeta.name).toBe("Euro");
38+
expect(eurMeta.icon).toBe(EURIcon);
39+
});
40+
41+
it("should return unknown currency for invalid shorthand", () => {
42+
const unknownMeta = getCurrencyMeta("XYZ");
43+
expect(unknownMeta.shorthand).toBe("XYZ");
44+
expect(unknownMeta.name).toBe("XYZ");
45+
expect(unknownMeta.icon).not.toBe(USDIcon);
46+
expect(unknownMeta.icon).not.toBe(CADIcon);
47+
expect(unknownMeta.icon).not.toBe(GBPIcon);
48+
expect(unknownMeta.icon).not.toBe(EURIcon);
49+
expect(unknownMeta.icon).not.toBe(JPYIcon);
50+
});
51+
});
52+
});
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { BuyWithCryptoStatus } from "../../../../../../../pay/buyWithCrypto/getStatus.js";
3+
import type { BuyWithFiatStatus } from "../../../../../../../pay/buyWithFiat/getStatus.js";
4+
import {
5+
getBuyWithCryptoStatusMeta,
6+
getBuyWithFiatStatusMeta,
7+
} from "./statusMeta.js";
8+
9+
describe("getBuyWithCryptoStatusMeta", () => {
10+
it('returns "Unknown" for NOT_FOUND status', () => {
11+
const result = getBuyWithCryptoStatusMeta({
12+
status: "NOT_FOUND",
13+
} as BuyWithCryptoStatus);
14+
expect(result).toEqual({
15+
status: "Unknown",
16+
color: "secondaryText",
17+
});
18+
});
19+
20+
it('returns "Bridging" for WAITING_BRIDGE subStatus', () => {
21+
const result = getBuyWithCryptoStatusMeta({
22+
status: "PENDING",
23+
subStatus: "WAITING_BRIDGE",
24+
} as BuyWithCryptoStatus);
25+
expect(result).toEqual({
26+
status: "Bridging",
27+
color: "accentText",
28+
loading: true,
29+
});
30+
});
31+
32+
it('returns "Incomplete" for PARTIAL_SUCCESS subStatus', () => {
33+
const result = getBuyWithCryptoStatusMeta({
34+
status: "COMPLETED",
35+
subStatus: "PARTIAL_SUCCESS",
36+
} as BuyWithCryptoStatus);
37+
expect(result).toEqual({
38+
status: "Incomplete",
39+
color: "secondaryText",
40+
});
41+
});
42+
43+
it('returns "Pending" for PENDING status', () => {
44+
const result = getBuyWithCryptoStatusMeta({
45+
status: "PENDING",
46+
} as BuyWithCryptoStatus);
47+
expect(result).toEqual({
48+
status: "Pending",
49+
color: "accentText",
50+
loading: true,
51+
});
52+
});
53+
54+
it('returns "Failed" for FAILED status', () => {
55+
const result = getBuyWithCryptoStatusMeta({
56+
status: "FAILED",
57+
} as BuyWithCryptoStatus);
58+
expect(result).toEqual({
59+
status: "Failed",
60+
color: "danger",
61+
});
62+
});
63+
64+
it('returns "Completed" for COMPLETED status', () => {
65+
const result = getBuyWithCryptoStatusMeta({
66+
status: "COMPLETED",
67+
} as BuyWithCryptoStatus);
68+
expect(result).toEqual({
69+
status: "Completed",
70+
color: "success",
71+
});
72+
});
73+
74+
it('returns "Unknown" for unhandled status', () => {
75+
const result = getBuyWithCryptoStatusMeta({
76+
// @ts-ignore Test purpose
77+
status: "Unknown",
78+
});
79+
expect(result).toEqual({
80+
status: "Unknown",
81+
color: "secondaryText",
82+
});
83+
});
84+
});
85+
86+
describe("getBuyWithFiatStatusMeta", () => {
87+
it('returns "Incomplete" for CRYPTO_SWAP_FALLBACK status', () => {
88+
const result = getBuyWithFiatStatusMeta({
89+
status: "CRYPTO_SWAP_FALLBACK",
90+
} as BuyWithFiatStatus);
91+
expect(result).toEqual({
92+
status: "Incomplete",
93+
color: "danger",
94+
step: 2,
95+
progressStatus: "partialSuccess",
96+
});
97+
});
98+
99+
it('returns "Pending" for CRYPTO_SWAP_IN_PROGRESS status', () => {
100+
const result = getBuyWithFiatStatusMeta({
101+
status: "CRYPTO_SWAP_IN_PROGRESS",
102+
} as BuyWithFiatStatus);
103+
expect(result).toEqual({
104+
status: "Pending",
105+
color: "accentText",
106+
loading: true,
107+
step: 2,
108+
progressStatus: "pending",
109+
});
110+
});
111+
112+
it('returns "Pending" for PENDING_ON_RAMP_TRANSFER status', () => {
113+
const result = getBuyWithFiatStatusMeta({
114+
status: "PENDING_ON_RAMP_TRANSFER",
115+
} as BuyWithFiatStatus);
116+
expect(result).toEqual({
117+
status: "Pending",
118+
color: "accentText",
119+
loading: true,
120+
step: 1,
121+
progressStatus: "pending",
122+
});
123+
});
124+
125+
it('returns "Completed" for ON_RAMP_TRANSFER_COMPLETED status', () => {
126+
const result = getBuyWithFiatStatusMeta({
127+
status: "ON_RAMP_TRANSFER_COMPLETED",
128+
} as BuyWithFiatStatus);
129+
expect(result).toEqual({
130+
status: "Completed",
131+
color: "success",
132+
loading: true,
133+
step: 1,
134+
progressStatus: "completed",
135+
});
136+
});
137+
138+
it('returns "Action Required" for CRYPTO_SWAP_REQUIRED status', () => {
139+
const result = getBuyWithFiatStatusMeta({
140+
status: "CRYPTO_SWAP_REQUIRED",
141+
} as BuyWithFiatStatus);
142+
expect(result).toEqual({
143+
status: "Action Required",
144+
color: "accentText",
145+
step: 2,
146+
progressStatus: "actionRequired",
147+
});
148+
});
149+
150+
it('returns "Failed" for PAYMENT_FAILED status', () => {
151+
const result = getBuyWithFiatStatusMeta({
152+
status: "PAYMENT_FAILED",
153+
} as BuyWithFiatStatus);
154+
expect(result).toEqual({
155+
status: "Failed",
156+
color: "danger",
157+
step: 1,
158+
progressStatus: "failed",
159+
});
160+
});
161+
162+
it('returns "Unknown" for unhandled status', () => {
163+
const result = getBuyWithFiatStatusMeta({
164+
// @ts-ignore
165+
status: "UNKNOWN_STATUS",
166+
});
167+
expect(result).toEqual({
168+
status: "Unknown",
169+
color: "secondaryText",
170+
step: 1,
171+
progressStatus: "unknown",
172+
});
173+
});
174+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it } from "vitest";
2+
import { formatSeconds } from "./formatSeconds.js";
3+
4+
describe("formatSeconds", () => {
5+
it("formats seconds to hours and minutes when over 3600 seconds", () => {
6+
expect(formatSeconds(3601)).toBe("1 Hours 0 Minutes");
7+
expect(formatSeconds(7200)).toBe("2 Hours 0 Minutes");
8+
expect(formatSeconds(5400)).toBe("1 Hours 30 Minutes");
9+
expect(formatSeconds(12345)).toBe("3 Hours 25 Minutes");
10+
});
11+
12+
it("formats seconds to minutes when between 61 and 3600 seconds", () => {
13+
expect(formatSeconds(61)).toBe("2 Minutes");
14+
expect(formatSeconds(120)).toBe("2 Minutes");
15+
expect(formatSeconds(3599)).toBe("60 Minutes");
16+
expect(formatSeconds(1800)).toBe("30 Minutes");
17+
});
18+
19+
it('formats seconds to "Xs" when 60 seconds or less', () => {
20+
expect(formatSeconds(60)).toBe("60s");
21+
expect(formatSeconds(59)).toBe("59s");
22+
expect(formatSeconds(1)).toBe("1s");
23+
expect(formatSeconds(0)).toBe("0s");
24+
});
25+
26+
it("handles decimal inputs by rounding down for hours/minutes and up for minutes only", () => {
27+
expect(formatSeconds(3661.5)).toBe("1 Hours 1 Minutes");
28+
expect(formatSeconds(119.9)).toBe("2 Minutes");
29+
});
30+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
import { getBuyTokenAmountFontSize } from "./utils.js";
3+
4+
describe("getBuyTokenAmountFontSize", () => {
5+
it("returns 26px for strings longer than 10 characters", () => {
6+
expect(getBuyTokenAmountFontSize("12345678901")).toBe("26px");
7+
expect(getBuyTokenAmountFontSize("1234567890123")).toBe("26px");
8+
});
9+
10+
it("returns 34px for strings longer than 6 characters but not more than 10", () => {
11+
expect(getBuyTokenAmountFontSize("1234567")).toBe("34px");
12+
expect(getBuyTokenAmountFontSize("1234567890")).toBe("34px");
13+
});
14+
15+
it("returns 50px for strings 6 characters or shorter", () => {
16+
expect(getBuyTokenAmountFontSize("123456")).toBe("50px");
17+
expect(getBuyTokenAmountFontSize("12345")).toBe("50px");
18+
expect(getBuyTokenAmountFontSize("")).toBe("50px");
19+
});
20+
});

packages/thirdweb/src/utils/bigint.test.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { max, min, toBigInt } from "./bigint.js";
2+
import { max, min, replaceBigInts, toBigInt } from "./bigint.js";
33

44
describe("min", () => {
55
it("should return the smaller value when a is smaller than b", () => {
@@ -79,4 +79,94 @@ describe("toBigInt", () => {
7979
`Expected value to be an integer to convert to a bigint, got ${value} of type number`,
8080
);
8181
});
82+
83+
it("should convert a Uint8Array to a BigInt", () => {
84+
const uint8Array = new Uint8Array([1, 2, 3, 4]);
85+
const result = toBigInt(uint8Array);
86+
expect(result).toBe(BigInt("0x01020304"));
87+
});
88+
89+
it("should handle a Uint8Array with leading zeros", () => {
90+
const uint8Array = new Uint8Array([0, 0, 1, 2]);
91+
const result = toBigInt(uint8Array);
92+
expect(result).toBe(BigInt("0x00000102"));
93+
});
94+
95+
it("should handle a large Uint8Array", () => {
96+
const uint8Array = new Uint8Array([255, 255, 255, 255]);
97+
const result = toBigInt(uint8Array);
98+
expect(result).toBe(BigInt("0xffffffff"));
99+
});
100+
101+
describe("replaceBigInts", () => {
102+
it("should replace a single bigint value", () => {
103+
const input = BigInt(123);
104+
const result = replaceBigInts(input, (x) => x.toString());
105+
expect(result).toBe("123");
106+
});
107+
108+
it("should handle arrays containing bigints", () => {
109+
const input = [BigInt(1), BigInt(2), BigInt(3)];
110+
const result = replaceBigInts(input, (x) => x.toString());
111+
expect(result).toEqual(["1", "2", "3"]);
112+
});
113+
114+
it("should handle nested arrays with bigints", () => {
115+
const input = [BigInt(1), [BigInt(2), BigInt(3)]];
116+
const result = replaceBigInts(input, (x) => x.toString());
117+
expect(result).toEqual(["1", ["2", "3"]]);
118+
});
119+
120+
it("should handle objects containing bigints", () => {
121+
const input = { a: BigInt(1), b: BigInt(2) };
122+
const result = replaceBigInts(input, (x) => x.toString());
123+
expect(result).toEqual({ a: "1", b: "2" });
124+
});
125+
126+
it("should handle nested objects with bigints", () => {
127+
const input = { a: BigInt(1), b: { c: BigInt(2), d: BigInt(3) } };
128+
const result = replaceBigInts(input, (x) => x.toString());
129+
expect(result).toEqual({ a: "1", b: { c: "2", d: "3" } });
130+
});
131+
132+
it("should handle mixed arrays and objects", () => {
133+
const input = {
134+
a: [BigInt(1), { b: BigInt(2), c: [BigInt(3)] }],
135+
};
136+
const result = replaceBigInts(input, (x) => x.toString());
137+
expect(result).toEqual({ a: ["1", { b: "2", c: ["3"] }] });
138+
});
139+
140+
it("should handle empty arrays and objects", () => {
141+
expect(replaceBigInts([], (x) => x.toString())).toEqual([]);
142+
expect(replaceBigInts({}, (x) => x.toString())).toEqual({});
143+
});
144+
145+
it("should leave other types untouched", () => {
146+
const input = {
147+
a: "string",
148+
b: 42,
149+
c: null,
150+
d: undefined,
151+
e: true,
152+
f: [1, "test"],
153+
};
154+
const result = replaceBigInts(input, (x) => x.toString());
155+
expect(result).toEqual(input);
156+
});
157+
158+
it("should handle complex deeply nested structures", () => {
159+
const input = {
160+
a: BigInt(1),
161+
b: [BigInt(2), { c: [BigInt(3), { d: BigInt(4) }] }],
162+
e: { f: { g: BigInt(5) } },
163+
};
164+
const result = replaceBigInts(input, (x) => x.toString());
165+
expect(result).toEqual({
166+
a: "1",
167+
b: ["2", { c: ["3", { d: "4" }] }],
168+
e: { f: { g: "5" } },
169+
});
170+
});
171+
});
82172
});

0 commit comments

Comments
 (0)