Skip to content

Commit 46ad2f0

Browse files
committed
update
1 parent 1b103d7 commit 46ad2f0

File tree

13 files changed

+638
-2
lines changed

13 files changed

+638
-2
lines changed
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+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { beforeEach, describe, expect, it } from "vitest";
2+
import {
3+
DEFAULT_RPC_URL,
4+
getThirdwebBaseUrl,
5+
getThirdwebDomains,
6+
setThirdwebDomains,
7+
} from "./domains.js";
8+
9+
describe("Thirdweb Domains", () => {
10+
const defaultDomains = {
11+
rpc: "rpc.thirdweb.com",
12+
social: "social.thirdweb.com",
13+
inAppWallet: "embedded-wallet.thirdweb.com",
14+
pay: "pay.thirdweb.com",
15+
storage: "storage.thirdweb.com",
16+
bundler: "bundler.thirdweb.com",
17+
analytics: "c.thirdweb.com",
18+
};
19+
20+
beforeEach(() => {
21+
// Reset to default domains before each test
22+
setThirdwebDomains({});
23+
});
24+
25+
describe("getThirdwebDomains", () => {
26+
it("should return the default domains if no overrides are set", () => {
27+
expect(getThirdwebDomains()).toEqual(defaultDomains);
28+
});
29+
});
30+
31+
describe("setThirdwebDomains", () => {
32+
it("should override specific domains while keeping others as default", () => {
33+
setThirdwebDomains({
34+
rpc: "custom.rpc.com",
35+
analytics: "custom.analytics.com",
36+
});
37+
38+
expect(getThirdwebDomains()).toEqual({
39+
...defaultDomains,
40+
rpc: "custom.rpc.com",
41+
analytics: "custom.analytics.com",
42+
});
43+
});
44+
45+
it("should not modify domains that are not overridden", () => {
46+
setThirdwebDomains({ pay: "custom.pay.com" });
47+
48+
const domains = getThirdwebDomains();
49+
expect(domains.pay).toBe("custom.pay.com");
50+
expect(domains.rpc).toBe(defaultDomains.rpc);
51+
expect(domains.analytics).toBe(defaultDomains.analytics);
52+
});
53+
});
54+
55+
describe("getThirdwebBaseUrl", () => {
56+
it("should return an HTTPS URL for non-localhost domains", () => {
57+
const baseUrl = getThirdwebBaseUrl("rpc");
58+
expect(baseUrl).toBe(`https://${DEFAULT_RPC_URL}`);
59+
});
60+
61+
it("should return an HTTP URL for localhost domains", () => {
62+
setThirdwebDomains({ rpc: "localhost:8545" });
63+
const baseUrl = getThirdwebBaseUrl("rpc");
64+
expect(baseUrl).toBe("http://localhost:8545");
65+
});
66+
67+
it("should reflect the updated domain overrides", () => {
68+
setThirdwebDomains({ storage: "custom.storage.com" });
69+
const baseUrl = getThirdwebBaseUrl("storage");
70+
expect(baseUrl).toBe("https://custom.storage.com");
71+
});
72+
73+
it("should throw an error if an invalid service is requested", () => {
74+
// biome-ignore lint/suspicious/noExplicitAny: for test
75+
expect(() => getThirdwebBaseUrl("invalid" as any)).toThrow();
76+
});
77+
});
78+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vitest";
2+
import { encodedLabelToLabelhash } from "./encodeLabelToLabelhash.js";
3+
4+
describe("encodedLabelToLabelhash", () => {
5+
it("should return null if the label length is not 66", () => {
6+
expect(encodedLabelToLabelhash("")).toBeNull();
7+
expect(encodedLabelToLabelhash("[123456]")).toBeNull();
8+
expect(encodedLabelToLabelhash("[1234567890]".padEnd(67, "0"))).toBeNull();
9+
});
10+
11+
it("should return null if the label does not start with '['", () => {
12+
const input = "1234567890".padStart(66, "0");
13+
expect(encodedLabelToLabelhash(input)).toBeNull();
14+
});
15+
16+
it("should return null if the label does not end with ']' at position 65", () => {
17+
const input = "[1234567890".padEnd(66, "0");
18+
expect(encodedLabelToLabelhash(input)).toBeNull();
19+
});
20+
21+
it("should return the hash if the label is valid", () => {
22+
const validHash = "a".repeat(64);
23+
const input = `[${validHash}]`;
24+
25+
const result = encodedLabelToLabelhash(input);
26+
expect(result).toBe(`0x${validHash}`);
27+
});
28+
});

0 commit comments

Comments
 (0)