Skip to content

Commit 06c93fb

Browse files
committed
fix: Add test suites for analytics files using vitest and msw
1 parent 3f57d9e commit 06c93fb

File tree

10 files changed

+543
-38
lines changed

10 files changed

+543
-38
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, it, expect, beforeAll, afterAll, afterEach } from "vitest";
2+
import { setupServer } from "msw/node";
3+
import { http, HttpResponse } from "msw";
4+
import { trackConnect } from "./connect.js";
5+
import type { ThirdwebClient } from "../../client/client.js";
6+
7+
const server = setupServer(
8+
http.post("https://c.thirdweb.com/event", () => {
9+
return HttpResponse.json({});
10+
}),
11+
);
12+
13+
describe("trackConnect", () => {
14+
beforeAll(() => server.listen());
15+
afterEach(() => server.resetHandlers());
16+
afterAll(() => server.close());
17+
18+
it("should send a POST request with correct data", async () => {
19+
const mockClient: ThirdwebClient = {
20+
clientId: "test-client-id",
21+
secretKey: undefined,
22+
};
23+
24+
let requestBody: any;
25+
server.use(
26+
http.post("https://c.thirdweb.com/event", async (handler) => {
27+
requestBody = await handler.request.json();
28+
return HttpResponse.json({});
29+
}),
30+
);
31+
32+
await trackConnect({
33+
client: mockClient,
34+
walletType: "metamask",
35+
walletAddress: "0x1234567890123456789012345678901234567890",
36+
});
37+
38+
expect(requestBody).toEqual({
39+
source: "connectWallet",
40+
action: "connect",
41+
walletType: "metamask",
42+
walletAddress: "0x1234567890123456789012345678901234567890",
43+
});
44+
});
45+
46+
it("should send a POST request with correct headers", async () => {
47+
const mockClient: ThirdwebClient = {
48+
clientId: "test-client-id",
49+
secretKey: undefined,
50+
};
51+
52+
let requestHeaders: any;
53+
server.use(
54+
http.post("https://c.thirdweb.com/event", (handler) => {
55+
requestHeaders = handler.request.headers;
56+
return HttpResponse.json({});
57+
}),
58+
);
59+
60+
await trackConnect({
61+
client: mockClient,
62+
ecosystem: {
63+
id: "ecosystem.test-ecosystem-id",
64+
partnerId: "test-partner-id",
65+
},
66+
walletType: "metamask",
67+
walletAddress: "0x1234567890123456789012345678901234567890",
68+
});
69+
70+
expect(requestHeaders.get("x-client-id")).toEqual("test-client-id");
71+
expect(requestHeaders.get("x-ecosystem-id")).toEqual(
72+
"ecosystem.test-ecosystem-id",
73+
);
74+
expect(requestHeaders.get("x-ecosystem-partner-id")).toEqual(
75+
"test-partner-id",
76+
);
77+
});
78+
});
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
import type { Ecosystem } from "src/wallets/in-app/core/wallet/types.js";
12
import type { ThirdwebClient } from "../../client/client.js";
23
import { track } from "./index.js";
34

45
/**
56
* @internal
67
*/
7-
export function trackConnect(args: {
8+
export async function trackConnect(args: {
89
client: ThirdwebClient;
10+
ecosystem?: Ecosystem;
911
walletType: string;
1012
walletAddress: string;
1113
}) {
12-
const { client, walletType, walletAddress } = args;
13-
track(client, {
14-
source: "connectWallet",
15-
action: "connect",
16-
walletType,
17-
walletAddress,
14+
const { client, ecosystem, walletType, walletAddress } = args;
15+
return track({
16+
client,
17+
ecosystem,
18+
data: {
19+
source: "connectWallet",
20+
action: "connect",
21+
walletType,
22+
walletAddress,
23+
},
1824
});
1925
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Ecosystem } from "src/wallets/in-app/core/wallet/types.js";
12
import type { ThirdwebClient } from "../../client/client.js";
23
import { getClientFetch } from "../../utils/fetch.js";
34
import { stringify } from "../../utils/json.js";
@@ -7,15 +8,23 @@ const ANALYTICS_ENDPOINT = "https://c.thirdweb.com/event";
78
/**
89
* @internal
910
*/
10-
export function track(client: ThirdwebClient, data: object) {
11-
const fetch = getClientFetch(client);
11+
export async function track({
12+
client,
13+
ecosystem,
14+
data,
15+
}: {
16+
client: ThirdwebClient;
17+
ecosystem?: Ecosystem;
18+
data: object;
19+
}) {
20+
const fetch = getClientFetch(client, ecosystem);
1221
const event = {
1322
source: "sdk",
1423
...data,
1524
};
1625

17-
fetch(ANALYTICS_ENDPOINT, {
26+
return fetch(ANALYTICS_ENDPOINT, {
1827
method: "POST",
1928
body: stringify(event),
20-
}).catch(() => {});
29+
}).catch(() => { });
2130
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, it, expect, beforeAll, afterAll, afterEach } from "vitest";
2+
import { setupServer } from "msw/node";
3+
import { http, HttpResponse } from "msw";
4+
import { trackPayEvent } from "./pay.js";
5+
import type { ThirdwebClient } from "../../client/client.js";
6+
7+
const server = setupServer(
8+
http.post("https://c.thirdweb.com/event", () => {
9+
return HttpResponse.json({});
10+
}),
11+
);
12+
13+
describe("trackPayEvent", () => {
14+
beforeAll(() => server.listen());
15+
afterEach(() => server.resetHandlers());
16+
afterAll(() => server.close());
17+
18+
it("should send a POST request with correct data", async () => {
19+
const mockClient: ThirdwebClient = {
20+
clientId: "test-client-id",
21+
secretKey: undefined,
22+
};
23+
24+
let requestBody: any;
25+
server.use(
26+
http.post("https://c.thirdweb.com/event", async (handler) => {
27+
requestBody = await handler.request.json();
28+
return HttpResponse.json({});
29+
}),
30+
);
31+
32+
await trackPayEvent({
33+
client: mockClient,
34+
event: "test-event",
35+
walletAddress: "0x1234567890123456789012345678901234567890",
36+
walletType: "io.metamask",
37+
fromToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
38+
fromAmount: "1000000",
39+
toToken: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
40+
toAmount: "1000000",
41+
chainId: 1,
42+
dstChainId: 137,
43+
});
44+
45+
expect(requestBody).toEqual({
46+
source: "pay",
47+
action: "test-event",
48+
clientId: "test-client-id",
49+
chainId: 1,
50+
walletAddress: "0x1234567890123456789012345678901234567890",
51+
walletType: "io.metamask",
52+
tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
53+
amountWei: "1000000",
54+
dstTokenAddress: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
55+
dstChainId: 1,
56+
});
57+
});
58+
59+
it("should send a POST request with correct headers", async () => {
60+
const mockClient: ThirdwebClient = {
61+
clientId: "test-client-id",
62+
secretKey: undefined,
63+
};
64+
65+
let requestHeaders: any;
66+
server.use(
67+
http.post("https://c.thirdweb.com/event", (handler) => {
68+
requestHeaders = handler.request.headers;
69+
return HttpResponse.json({});
70+
}),
71+
);
72+
73+
await trackPayEvent({
74+
client: mockClient,
75+
ecosystem: {
76+
id: "ecosystem.test-ecosystem-id",
77+
partnerId: "test-partner-id",
78+
},
79+
event: "test-event",
80+
walletAddress: "0x1234567890123456789012345678901234567890",
81+
walletType: "io.metamask",
82+
fromToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
83+
fromAmount: "1000000",
84+
toToken: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
85+
toAmount: "1000000",
86+
chainId: 1,
87+
dstChainId: 137,
88+
});
89+
90+
expect(requestHeaders.get("x-client-id")).toEqual("test-client-id");
91+
expect(requestHeaders.get("x-ecosystem-id")).toEqual(
92+
"ecosystem.test-ecosystem-id",
93+
);
94+
expect(requestHeaders.get("x-ecosystem-partner-id")).toEqual(
95+
"test-partner-id",
96+
);
97+
});
98+
});
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import type { Ecosystem } from "src/wallets/in-app/core/wallet/types.js";
12
import type { ThirdwebClient } from "../../client/client.js";
23
import { track } from "./index.js";
34

45
/**
56
* @internal
67
*/
7-
export function trackPayEvent(args: {
8+
export async function trackPayEvent(args: {
89
client: ThirdwebClient;
10+
ecosystem?: Ecosystem;
911
event: string;
1012
walletAddress?: string;
1113
walletType?: string;
@@ -16,16 +18,20 @@ export function trackPayEvent(args: {
1618
chainId?: number;
1719
dstChainId?: number;
1820
}) {
19-
track(args.client, {
20-
source: "pay",
21-
action: args.event,
22-
clientId: args.client.clientId,
23-
chainId: args.chainId,
24-
walletAddress: args.walletAddress,
25-
walletType: args.walletType,
26-
tokenAddress: args.fromToken,
27-
amountWei: args.fromAmount,
28-
dstTokenAddress: args.toToken,
29-
dstChainId: args.chainId,
21+
return track({
22+
client: args.client,
23+
ecosystem: args.ecosystem,
24+
data: {
25+
source: "pay",
26+
action: args.event,
27+
clientId: args.client.clientId,
28+
chainId: args.chainId,
29+
walletAddress: args.walletAddress,
30+
walletType: args.walletType,
31+
tokenAddress: args.fromToken,
32+
amountWei: args.fromAmount,
33+
dstTokenAddress: args.toToken,
34+
dstChainId: args.chainId,
35+
},
3036
});
3137
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { ThirdwebClient } from "../../client/client.js";
2+
import { stringify } from "../../utils/json.js";
3+
import { track } from "./index.js";
4+
5+
type SiweEvent = {
6+
client: ThirdwebClient;
7+
walletAddress?: string;
8+
walletType?: string;
9+
chainId?: number;
10+
error?: {
11+
message: string;
12+
code: string;
13+
};
14+
};
15+
16+
type SiweSuccessEvent = SiweEvent & {
17+
error?: undefined;
18+
};
19+
20+
/**
21+
* @internal
22+
*/
23+
export function trackLogin(event: SiweSuccessEvent) {
24+
trackSiweEvent({
25+
...event,
26+
type: "login:success",
27+
});
28+
}
29+
30+
type SiweErrorEvent = SiweEvent & {
31+
error: {
32+
message: string;
33+
code: string;
34+
};
35+
};
36+
37+
/**
38+
* @internal
39+
*/
40+
export function trackLoginError(event: SiweErrorEvent) {
41+
trackSiweEvent({
42+
...event,
43+
type: "login:error",
44+
});
45+
}
46+
47+
/**
48+
* @internal
49+
*/
50+
function trackSiweEvent(
51+
event: SiweEvent & {
52+
type: "login:success" | "login:error";
53+
},
54+
) {
55+
track(event.client, {
56+
action: event.type,
57+
clientId: event.client.clientId,
58+
chainId: event.chainId,
59+
walletAddress: event.walletAddress,
60+
walletType: event.walletType,
61+
errorCode: stringify(event.error),
62+
});
63+
}

0 commit comments

Comments
 (0)