@frak-labs/core-sdk 0.1.0-beta.8d103039 → 0.1.0-beta.d9302e66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +20 -16
- package/src/actions/displayEmbeddedWallet.test.ts +194 -0
- package/src/actions/displayModal.test.ts +387 -0
- package/src/actions/getProductInformation.test.ts +133 -0
- package/src/actions/index.ts +19 -19
- package/src/actions/openSso.test.ts +407 -0
- package/src/actions/prepareSso.test.ts +223 -0
- package/src/actions/referral/processReferral.ts +1 -1
- package/src/actions/referral/referralInteraction.ts +1 -1
- package/src/actions/sendInteraction.test.ts +219 -0
- package/src/actions/trackPurchaseStatus.test.ts +287 -0
- package/src/actions/watchWalletStatus.test.ts +372 -0
- package/src/bundle.ts +1 -1
- package/src/clients/createIFrameFrakClient.ts +2 -2
- package/src/clients/index.ts +1 -1
- package/src/clients/setupClient.ts +3 -1
- package/src/clients/transports/iframeLifecycleManager.ts +3 -1
- package/src/index.ts +72 -74
- package/src/interactions/index.ts +2 -2
- package/src/interactions/pressEncoder.test.ts +215 -0
- package/src/interactions/pressEncoder.ts +1 -1
- package/src/interactions/purchaseEncoder.test.ts +291 -0
- package/src/interactions/purchaseEncoder.ts +8 -3
- package/src/interactions/referralEncoder.test.ts +170 -0
- package/src/interactions/retailEncoder.test.ts +107 -0
- package/src/interactions/retailEncoder.ts +1 -1
- package/src/interactions/webshopEncoder.test.ts +56 -0
- package/src/types/index.ts +51 -50
- package/src/types/lifecycle/index.ts +1 -1
- package/src/types/rpc/embedded/loggedIn.ts +1 -1
- package/src/types/rpc/embedded/loggedOut.ts +1 -1
- package/src/types/rpc/modal/index.ts +11 -11
- package/src/utils/FrakContext.test.ts +338 -0
- package/src/utils/FrakContext.ts +8 -2
- package/src/utils/compression/b64.test.ts +181 -0
- package/src/utils/compression/compress.test.ts +123 -0
- package/src/utils/compression/decompress.test.ts +145 -0
- package/src/utils/compression/index.ts +1 -1
- package/src/utils/computeProductId.test.ts +80 -0
- package/src/utils/constants.test.ts +23 -0
- package/src/utils/formatAmount.test.ts +113 -0
- package/src/utils/getCurrencyAmountKey.test.ts +44 -0
- package/src/utils/getSupportedCurrency.test.ts +51 -0
- package/src/utils/getSupportedLocale.test.ts +64 -0
- package/src/utils/iframeHelper.test.ts +450 -0
- package/src/utils/iframeHelper.ts +4 -3
- package/src/utils/index.ts +12 -12
- package/src/utils/sso.test.ts +361 -0
- package/src/utils/trackEvent.test.ts +162 -0
- package/cdn/bundle.js +0 -19
- package/cdn/bundle.js.LICENSE.txt +0 -10
- package/dist/actions.cjs +0 -1
- package/dist/actions.d.cts +0 -1481
- package/dist/actions.d.ts +0 -1481
- package/dist/actions.js +0 -1
- package/dist/bundle.cjs +0 -13
- package/dist/bundle.d.cts +0 -2087
- package/dist/bundle.d.ts +0 -2087
- package/dist/bundle.js +0 -13
- package/dist/index.cjs +0 -13
- package/dist/index.d.cts +0 -1387
- package/dist/index.d.ts +0 -1387
- package/dist/index.js +0 -13
- package/dist/interactions.cjs +0 -1
- package/dist/interactions.d.cts +0 -182
- package/dist/interactions.d.ts +0 -182
- package/dist/interactions.js +0 -1
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for trackPurchaseStatus action
|
|
3
|
+
* Tests webhook registration for purchase tracking
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
afterEach,
|
|
8
|
+
beforeEach,
|
|
9
|
+
describe,
|
|
10
|
+
expect,
|
|
11
|
+
it,
|
|
12
|
+
vi,
|
|
13
|
+
} from "../../tests/vitest-fixtures";
|
|
14
|
+
import { trackPurchaseStatus } from "./trackPurchaseStatus";
|
|
15
|
+
|
|
16
|
+
describe("trackPurchaseStatus", () => {
|
|
17
|
+
let mockSessionStorage: {
|
|
18
|
+
getItem: ReturnType<typeof vi.fn>;
|
|
19
|
+
setItem: ReturnType<typeof vi.fn>;
|
|
20
|
+
removeItem: ReturnType<typeof vi.fn>;
|
|
21
|
+
};
|
|
22
|
+
let fetchSpy: any;
|
|
23
|
+
let consoleWarnSpy: any;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
// Mock sessionStorage
|
|
27
|
+
mockSessionStorage = {
|
|
28
|
+
getItem: vi.fn(),
|
|
29
|
+
setItem: vi.fn(),
|
|
30
|
+
removeItem: vi.fn(),
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(window, "sessionStorage", {
|
|
33
|
+
value: mockSessionStorage,
|
|
34
|
+
writable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Mock fetch
|
|
39
|
+
fetchSpy = vi.fn().mockResolvedValue({
|
|
40
|
+
ok: true,
|
|
41
|
+
status: 200,
|
|
42
|
+
});
|
|
43
|
+
global.fetch = fetchSpy;
|
|
44
|
+
|
|
45
|
+
// Mock console.warn
|
|
46
|
+
consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(() => {
|
|
50
|
+
vi.clearAllMocks();
|
|
51
|
+
consoleWarnSpy.mockRestore();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("successful tracking", () => {
|
|
55
|
+
it("should send POST request with correct parameters", async () => {
|
|
56
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
57
|
+
|
|
58
|
+
await trackPurchaseStatus({
|
|
59
|
+
customerId: "cust-456",
|
|
60
|
+
orderId: "order-789",
|
|
61
|
+
token: "purchase-token",
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
65
|
+
"https://backend.frak.id/interactions/listenForPurchase",
|
|
66
|
+
{
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: {
|
|
69
|
+
Accept: "application/json",
|
|
70
|
+
"Content-Type": "application/json",
|
|
71
|
+
"x-wallet-sdk-auth": "token-123",
|
|
72
|
+
},
|
|
73
|
+
body: JSON.stringify({
|
|
74
|
+
customerId: "cust-456",
|
|
75
|
+
orderId: "order-789",
|
|
76
|
+
token: "purchase-token",
|
|
77
|
+
}),
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should read interaction token from sessionStorage", async () => {
|
|
83
|
+
mockSessionStorage.getItem.mockReturnValue("my-token");
|
|
84
|
+
|
|
85
|
+
await trackPurchaseStatus({
|
|
86
|
+
customerId: "cust-1",
|
|
87
|
+
orderId: "order-1",
|
|
88
|
+
token: "token-1",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
expect(mockSessionStorage.getItem).toHaveBeenCalledWith(
|
|
92
|
+
"frak-wallet-interaction-token"
|
|
93
|
+
);
|
|
94
|
+
expect(fetchSpy).toHaveBeenCalled();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should handle numeric customerId", async () => {
|
|
98
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
99
|
+
|
|
100
|
+
await trackPurchaseStatus({
|
|
101
|
+
customerId: 12345,
|
|
102
|
+
orderId: "order-789",
|
|
103
|
+
token: "purchase-token",
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
107
|
+
expect.any(String),
|
|
108
|
+
expect.objectContaining({
|
|
109
|
+
body: JSON.stringify({
|
|
110
|
+
customerId: 12345,
|
|
111
|
+
orderId: "order-789",
|
|
112
|
+
token: "purchase-token",
|
|
113
|
+
}),
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should handle numeric orderId", async () => {
|
|
119
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
120
|
+
|
|
121
|
+
await trackPurchaseStatus({
|
|
122
|
+
customerId: "cust-456",
|
|
123
|
+
orderId: 67890,
|
|
124
|
+
token: "purchase-token",
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
128
|
+
expect.any(String),
|
|
129
|
+
expect.objectContaining({
|
|
130
|
+
body: JSON.stringify({
|
|
131
|
+
customerId: "cust-456",
|
|
132
|
+
orderId: 67890,
|
|
133
|
+
token: "purchase-token",
|
|
134
|
+
}),
|
|
135
|
+
})
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should handle both numeric customerId and orderId", async () => {
|
|
140
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
141
|
+
|
|
142
|
+
await trackPurchaseStatus({
|
|
143
|
+
customerId: 12345,
|
|
144
|
+
orderId: 67890,
|
|
145
|
+
token: "purchase-token",
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
149
|
+
expect.any(String),
|
|
150
|
+
expect.objectContaining({
|
|
151
|
+
body: JSON.stringify({
|
|
152
|
+
customerId: 12345,
|
|
153
|
+
orderId: 67890,
|
|
154
|
+
token: "purchase-token",
|
|
155
|
+
}),
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("missing interaction token", () => {
|
|
162
|
+
it("should warn when no interaction token found", async () => {
|
|
163
|
+
mockSessionStorage.getItem.mockReturnValue(null);
|
|
164
|
+
|
|
165
|
+
await trackPurchaseStatus({
|
|
166
|
+
customerId: "cust-456",
|
|
167
|
+
orderId: "order-789",
|
|
168
|
+
token: "purchase-token",
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
172
|
+
"[Frak] No frak session found, skipping purchase check"
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should not send request when no interaction token", async () => {
|
|
177
|
+
mockSessionStorage.getItem.mockReturnValue(null);
|
|
178
|
+
|
|
179
|
+
await trackPurchaseStatus({
|
|
180
|
+
customerId: "cust-456",
|
|
181
|
+
orderId: "order-789",
|
|
182
|
+
token: "purchase-token",
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(fetchSpy).not.toHaveBeenCalled();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should not send request when interaction token is empty string", async () => {
|
|
189
|
+
mockSessionStorage.getItem.mockReturnValue("");
|
|
190
|
+
|
|
191
|
+
await trackPurchaseStatus({
|
|
192
|
+
customerId: "cust-456",
|
|
193
|
+
orderId: "order-789",
|
|
194
|
+
token: "purchase-token",
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(fetchSpy).not.toHaveBeenCalled();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("network errors", () => {
|
|
202
|
+
it("should handle fetch rejection", async () => {
|
|
203
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
204
|
+
fetchSpy.mockRejectedValue(new Error("Network error"));
|
|
205
|
+
|
|
206
|
+
await expect(
|
|
207
|
+
trackPurchaseStatus({
|
|
208
|
+
customerId: "cust-456",
|
|
209
|
+
orderId: "order-789",
|
|
210
|
+
token: "purchase-token",
|
|
211
|
+
})
|
|
212
|
+
).rejects.toThrow("Network error");
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("should handle fetch with error response", async () => {
|
|
216
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
217
|
+
fetchSpy.mockResolvedValue({
|
|
218
|
+
ok: false,
|
|
219
|
+
status: 500,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// Function doesn't check response, so it should complete
|
|
223
|
+
await trackPurchaseStatus({
|
|
224
|
+
customerId: "cust-456",
|
|
225
|
+
orderId: "order-789",
|
|
226
|
+
token: "purchase-token",
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
expect(fetchSpy).toHaveBeenCalled();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("request format", () => {
|
|
234
|
+
it("should include correct headers", async () => {
|
|
235
|
+
mockSessionStorage.getItem.mockReturnValue("my-auth-token");
|
|
236
|
+
|
|
237
|
+
await trackPurchaseStatus({
|
|
238
|
+
customerId: "cust-1",
|
|
239
|
+
orderId: "order-1",
|
|
240
|
+
token: "token-1",
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
244
|
+
expect.any(String),
|
|
245
|
+
expect.objectContaining({
|
|
246
|
+
headers: {
|
|
247
|
+
Accept: "application/json",
|
|
248
|
+
"Content-Type": "application/json",
|
|
249
|
+
"x-wallet-sdk-auth": "my-auth-token",
|
|
250
|
+
},
|
|
251
|
+
})
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("should use POST method", async () => {
|
|
256
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
257
|
+
|
|
258
|
+
await trackPurchaseStatus({
|
|
259
|
+
customerId: "cust-1",
|
|
260
|
+
orderId: "order-1",
|
|
261
|
+
token: "token-1",
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
265
|
+
expect.any(String),
|
|
266
|
+
expect.objectContaining({
|
|
267
|
+
method: "POST",
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should target correct backend URL", async () => {
|
|
273
|
+
mockSessionStorage.getItem.mockReturnValue("token-123");
|
|
274
|
+
|
|
275
|
+
await trackPurchaseStatus({
|
|
276
|
+
customerId: "cust-1",
|
|
277
|
+
orderId: "order-1",
|
|
278
|
+
token: "token-1",
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
expect(fetchSpy).toHaveBeenCalledWith(
|
|
282
|
+
"https://backend.frak.id/interactions/listenForPurchase",
|
|
283
|
+
expect.any(Object)
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
});
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for watchWalletStatus action
|
|
3
|
+
* Tests wallet status watching and side effects
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { vi } from "vitest";
|
|
7
|
+
|
|
8
|
+
// Mock Deferred before imports
|
|
9
|
+
vi.mock("@frak-labs/frame-connector", () => ({
|
|
10
|
+
Deferred: class {
|
|
11
|
+
promise: Promise<any>;
|
|
12
|
+
resolve: (value: any) => void;
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
let resolveFunc: (value: any) => void;
|
|
16
|
+
this.promise = new Promise((resolve) => {
|
|
17
|
+
resolveFunc = resolve;
|
|
18
|
+
});
|
|
19
|
+
this.resolve = resolveFunc!;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
import type { Address } from "viem";
|
|
25
|
+
import {
|
|
26
|
+
afterEach,
|
|
27
|
+
beforeEach,
|
|
28
|
+
describe,
|
|
29
|
+
expect,
|
|
30
|
+
it,
|
|
31
|
+
} from "../../tests/vitest-fixtures";
|
|
32
|
+
import type { FrakClient, WalletStatusReturnType } from "../types";
|
|
33
|
+
import { watchWalletStatus } from "./watchWalletStatus";
|
|
34
|
+
|
|
35
|
+
describe("watchWalletStatus", () => {
|
|
36
|
+
let mockSessionStorage: {
|
|
37
|
+
getItem: ReturnType<typeof vi.fn>;
|
|
38
|
+
setItem: ReturnType<typeof vi.fn>;
|
|
39
|
+
removeItem: ReturnType<typeof vi.fn>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
// Mock sessionStorage
|
|
44
|
+
mockSessionStorage = {
|
|
45
|
+
getItem: vi.fn(),
|
|
46
|
+
setItem: vi.fn(),
|
|
47
|
+
removeItem: vi.fn(),
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(window, "sessionStorage", {
|
|
50
|
+
value: mockSessionStorage,
|
|
51
|
+
writable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
vi.clearAllMocks();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("without callback", () => {
|
|
61
|
+
it("should make one-shot request", async () => {
|
|
62
|
+
const mockStatus: WalletStatusReturnType = {
|
|
63
|
+
key: "connected",
|
|
64
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
65
|
+
interactionToken: "token-123",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const mockClient = {
|
|
69
|
+
request: vi.fn().mockResolvedValue(mockStatus),
|
|
70
|
+
openPanel: {
|
|
71
|
+
setGlobalProperties: vi.fn(),
|
|
72
|
+
},
|
|
73
|
+
} as unknown as FrakClient;
|
|
74
|
+
|
|
75
|
+
const result = await watchWalletStatus(mockClient);
|
|
76
|
+
|
|
77
|
+
expect(mockClient.request).toHaveBeenCalledWith({
|
|
78
|
+
method: "frak_listenToWalletStatus",
|
|
79
|
+
});
|
|
80
|
+
expect(result).toEqual(mockStatus);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should save interaction token to sessionStorage", async () => {
|
|
84
|
+
const mockStatus: WalletStatusReturnType = {
|
|
85
|
+
key: "connected",
|
|
86
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
87
|
+
interactionToken: "token-abc",
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const mockClient = {
|
|
91
|
+
request: vi.fn().mockResolvedValue(mockStatus),
|
|
92
|
+
openPanel: {
|
|
93
|
+
setGlobalProperties: vi.fn(),
|
|
94
|
+
},
|
|
95
|
+
} as unknown as FrakClient;
|
|
96
|
+
|
|
97
|
+
await watchWalletStatus(mockClient);
|
|
98
|
+
|
|
99
|
+
expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
|
|
100
|
+
"frak-wallet-interaction-token",
|
|
101
|
+
"token-abc"
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("should remove interaction token when not present", async () => {
|
|
106
|
+
const mockStatus: WalletStatusReturnType = {
|
|
107
|
+
key: "not-connected",
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const mockClient = {
|
|
111
|
+
request: vi.fn().mockResolvedValue(mockStatus),
|
|
112
|
+
openPanel: {
|
|
113
|
+
setGlobalProperties: vi.fn(),
|
|
114
|
+
},
|
|
115
|
+
} as unknown as FrakClient;
|
|
116
|
+
|
|
117
|
+
await watchWalletStatus(mockClient);
|
|
118
|
+
|
|
119
|
+
expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(
|
|
120
|
+
"frak-wallet-interaction-token"
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should update OpenPanel global properties", async () => {
|
|
125
|
+
const mockStatus: WalletStatusReturnType = {
|
|
126
|
+
key: "connected",
|
|
127
|
+
wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const mockClient = {
|
|
131
|
+
request: vi.fn().mockResolvedValue(mockStatus),
|
|
132
|
+
openPanel: {
|
|
133
|
+
setGlobalProperties: vi.fn(),
|
|
134
|
+
},
|
|
135
|
+
} as unknown as FrakClient;
|
|
136
|
+
|
|
137
|
+
await watchWalletStatus(mockClient);
|
|
138
|
+
|
|
139
|
+
expect(
|
|
140
|
+
mockClient.openPanel?.setGlobalProperties
|
|
141
|
+
).toHaveBeenCalledWith({
|
|
142
|
+
wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd",
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("should set wallet to null when not connected", async () => {
|
|
147
|
+
const mockStatus: WalletStatusReturnType = {
|
|
148
|
+
key: "not-connected",
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const mockClient = {
|
|
152
|
+
request: vi.fn().mockResolvedValue(mockStatus),
|
|
153
|
+
openPanel: {
|
|
154
|
+
setGlobalProperties: vi.fn(),
|
|
155
|
+
},
|
|
156
|
+
} as unknown as FrakClient;
|
|
157
|
+
|
|
158
|
+
await watchWalletStatus(mockClient);
|
|
159
|
+
|
|
160
|
+
expect(
|
|
161
|
+
mockClient.openPanel?.setGlobalProperties
|
|
162
|
+
).toHaveBeenCalledWith({
|
|
163
|
+
wallet: null,
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should work without openPanel", async () => {
|
|
168
|
+
const mockStatus: WalletStatusReturnType = {
|
|
169
|
+
key: "connected",
|
|
170
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const mockClient = {
|
|
174
|
+
request: vi.fn().mockResolvedValue(mockStatus),
|
|
175
|
+
} as unknown as FrakClient;
|
|
176
|
+
|
|
177
|
+
const result = await watchWalletStatus(mockClient);
|
|
178
|
+
|
|
179
|
+
expect(result).toEqual(mockStatus);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe("with callback", () => {
|
|
184
|
+
it("should setup listener request", async () => {
|
|
185
|
+
const mockStatus: WalletStatusReturnType = {
|
|
186
|
+
key: "connected",
|
|
187
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const mockCallback = vi.fn();
|
|
191
|
+
const mockClient = {
|
|
192
|
+
listenerRequest: vi.fn((_params, callback) => {
|
|
193
|
+
// Simulate status update
|
|
194
|
+
setTimeout(() => callback(mockStatus), 0);
|
|
195
|
+
}),
|
|
196
|
+
openPanel: {
|
|
197
|
+
setGlobalProperties: vi.fn(),
|
|
198
|
+
},
|
|
199
|
+
} as unknown as FrakClient;
|
|
200
|
+
|
|
201
|
+
const resultPromise = watchWalletStatus(mockClient, mockCallback);
|
|
202
|
+
|
|
203
|
+
expect(mockClient.listenerRequest).toHaveBeenCalled();
|
|
204
|
+
|
|
205
|
+
await resultPromise;
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("should call callback with status updates", async () => {
|
|
209
|
+
const mockStatus: WalletStatusReturnType = {
|
|
210
|
+
key: "connected",
|
|
211
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
212
|
+
interactionToken: "token-123",
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const mockCallback = vi.fn();
|
|
216
|
+
const mockClient = {
|
|
217
|
+
listenerRequest: vi.fn((_params, callback) => {
|
|
218
|
+
setTimeout(() => callback(mockStatus), 0);
|
|
219
|
+
}),
|
|
220
|
+
openPanel: {
|
|
221
|
+
setGlobalProperties: vi.fn(),
|
|
222
|
+
},
|
|
223
|
+
} as unknown as FrakClient;
|
|
224
|
+
|
|
225
|
+
await watchWalletStatus(mockClient, mockCallback);
|
|
226
|
+
|
|
227
|
+
expect(mockCallback).toHaveBeenCalledWith(mockStatus);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("should resolve promise with first status", async () => {
|
|
231
|
+
const firstStatus: WalletStatusReturnType = {
|
|
232
|
+
key: "connected",
|
|
233
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const secondStatus: WalletStatusReturnType = {
|
|
237
|
+
key: "connected",
|
|
238
|
+
wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const mockCallback = vi.fn();
|
|
242
|
+
const mockClient = {
|
|
243
|
+
listenerRequest: vi.fn((_params, callback) => {
|
|
244
|
+
setTimeout(() => {
|
|
245
|
+
callback(firstStatus);
|
|
246
|
+
callback(secondStatus);
|
|
247
|
+
}, 0);
|
|
248
|
+
}),
|
|
249
|
+
openPanel: {
|
|
250
|
+
setGlobalProperties: vi.fn(),
|
|
251
|
+
},
|
|
252
|
+
} as unknown as FrakClient;
|
|
253
|
+
|
|
254
|
+
const result = await watchWalletStatus(mockClient, mockCallback);
|
|
255
|
+
|
|
256
|
+
expect(result).toEqual(firstStatus);
|
|
257
|
+
expect(mockCallback).toHaveBeenCalledTimes(2);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("should handle side effects for each status update", async () => {
|
|
261
|
+
const firstStatus: WalletStatusReturnType = {
|
|
262
|
+
key: "connected",
|
|
263
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
264
|
+
interactionToken: "token-1",
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const secondStatus: WalletStatusReturnType = {
|
|
268
|
+
key: "connected",
|
|
269
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
270
|
+
interactionToken: "token-2",
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const mockCallback = vi.fn();
|
|
274
|
+
const mockClient = {
|
|
275
|
+
listenerRequest: vi.fn((_params, callback) => {
|
|
276
|
+
setTimeout(() => {
|
|
277
|
+
callback(firstStatus);
|
|
278
|
+
callback(secondStatus);
|
|
279
|
+
}, 0);
|
|
280
|
+
}),
|
|
281
|
+
openPanel: {
|
|
282
|
+
setGlobalProperties: vi.fn(),
|
|
283
|
+
},
|
|
284
|
+
} as unknown as FrakClient;
|
|
285
|
+
|
|
286
|
+
await watchWalletStatus(mockClient, mockCallback);
|
|
287
|
+
|
|
288
|
+
// Both tokens should be saved
|
|
289
|
+
expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
|
|
290
|
+
"frak-wallet-interaction-token",
|
|
291
|
+
"token-1"
|
|
292
|
+
);
|
|
293
|
+
expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
|
|
294
|
+
"frak-wallet-interaction-token",
|
|
295
|
+
"token-2"
|
|
296
|
+
);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("should save and remove interaction token based on status", async () => {
|
|
300
|
+
const connectedStatus: WalletStatusReturnType = {
|
|
301
|
+
key: "connected",
|
|
302
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
303
|
+
interactionToken: "token-123",
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const disconnectedStatus: WalletStatusReturnType = {
|
|
307
|
+
key: "not-connected",
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const mockCallback = vi.fn();
|
|
311
|
+
const mockClient = {
|
|
312
|
+
listenerRequest: vi.fn((_params, callback) => {
|
|
313
|
+
setTimeout(() => {
|
|
314
|
+
callback(connectedStatus);
|
|
315
|
+
callback(disconnectedStatus);
|
|
316
|
+
}, 0);
|
|
317
|
+
}),
|
|
318
|
+
openPanel: {
|
|
319
|
+
setGlobalProperties: vi.fn(),
|
|
320
|
+
},
|
|
321
|
+
} as unknown as FrakClient;
|
|
322
|
+
|
|
323
|
+
await watchWalletStatus(mockClient, mockCallback);
|
|
324
|
+
|
|
325
|
+
expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
|
|
326
|
+
"frak-wallet-interaction-token",
|
|
327
|
+
"token-123"
|
|
328
|
+
);
|
|
329
|
+
expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(
|
|
330
|
+
"frak-wallet-interaction-token"
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("should update OpenPanel properties with each status", async () => {
|
|
335
|
+
const firstStatus: WalletStatusReturnType = {
|
|
336
|
+
key: "connected",
|
|
337
|
+
wallet: "0x1111111111111111111111111111111111111111" as Address,
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const secondStatus: WalletStatusReturnType = {
|
|
341
|
+
key: "connected",
|
|
342
|
+
wallet: "0x2222222222222222222222222222222222222222" as Address,
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const mockCallback = vi.fn();
|
|
346
|
+
const mockClient = {
|
|
347
|
+
listenerRequest: vi.fn((_params, callback) => {
|
|
348
|
+
setTimeout(() => {
|
|
349
|
+
callback(firstStatus);
|
|
350
|
+
callback(secondStatus);
|
|
351
|
+
}, 0);
|
|
352
|
+
}),
|
|
353
|
+
openPanel: {
|
|
354
|
+
setGlobalProperties: vi.fn(),
|
|
355
|
+
},
|
|
356
|
+
} as unknown as FrakClient;
|
|
357
|
+
|
|
358
|
+
await watchWalletStatus(mockClient, mockCallback);
|
|
359
|
+
|
|
360
|
+
expect(
|
|
361
|
+
mockClient.openPanel?.setGlobalProperties
|
|
362
|
+
).toHaveBeenCalledWith({
|
|
363
|
+
wallet: "0x1111111111111111111111111111111111111111",
|
|
364
|
+
});
|
|
365
|
+
expect(
|
|
366
|
+
mockClient.openPanel?.setGlobalProperties
|
|
367
|
+
).toHaveBeenCalledWith({
|
|
368
|
+
wallet: "0x2222222222222222222222222222222222222222",
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
});
|
package/src/bundle.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
+
createRpcClient,
|
|
2
3
|
FrakRpcError,
|
|
3
4
|
type RpcClient,
|
|
4
5
|
RpcErrorCodes,
|
|
5
|
-
createRpcClient,
|
|
6
6
|
} from "@frak-labs/frame-connector";
|
|
7
7
|
import { createClientCompressionMiddleware } from "@frak-labs/frame-connector/middleware";
|
|
8
8
|
import { OpenPanel } from "@openpanel/web";
|
|
@@ -14,8 +14,8 @@ import { BACKUP_KEY } from "../utils/constants";
|
|
|
14
14
|
import { setupSsoUrlListener } from "../utils/ssoUrlListener";
|
|
15
15
|
import { DebugInfoGatherer } from "./DebugInfo";
|
|
16
16
|
import {
|
|
17
|
-
type IframeLifecycleManager,
|
|
18
17
|
createIFrameLifecycleManager,
|
|
18
|
+
type IframeLifecycleManager,
|
|
19
19
|
} from "./transports/iframeLifecycleManager";
|
|
20
20
|
|
|
21
21
|
type SdkRpcClient = RpcClient<IFrameRpcSchema, FrakLifecycleEvent>;
|
package/src/clients/index.ts
CHANGED
|
@@ -19,7 +19,9 @@ import { createIframe, getSupportedCurrency } from "../utils";
|
|
|
19
19
|
*/
|
|
20
20
|
export async function setupClient({
|
|
21
21
|
config,
|
|
22
|
-
}: {
|
|
22
|
+
}: {
|
|
23
|
+
config: FrakWalletSdkConfig;
|
|
24
|
+
}): Promise<FrakClient | undefined> {
|
|
23
25
|
// Prepare the config
|
|
24
26
|
const preparedConfig = prepareConfig(config);
|
|
25
27
|
|