Skip to content

Commit 40af80c

Browse files
Add connection mock creator
1 parent 73dcf60 commit 40af80c

File tree

3 files changed

+79
-59
lines changed

3 files changed

+79
-59
lines changed

packages/keychain/.storybook/preview.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { BrowserRouter } from "react-router-dom";
66
import { voyager, jsonRpcProvider, StarknetConfig } from "@starknet-react/core";
77
import { sepolia, mainnet } from "@starknet-react/chains";
88
import { num } from "starknet";
9-
import { mockedConnection } from "#hooks/connection.mock";
9+
import { createMockConnection } from "#hooks/connection.mock";
1010
import { controllerConfigs, defaultTheme } from "@cartridge/presets";
1111
import { UIProvider } from "#components/provider/ui";
1212

@@ -55,18 +55,17 @@ const preview: Preview = {
5555
: defaultTheme,
5656
assetUrl: "",
5757
});
58+
const connection = createMockConnection();
5859

5960
return (
6061
// Render only third party providers which consumer hook is directly used across the source code and hard to mock otherwise
6162
<BrowserRouter>
6263
<StarknetConfig
6364
explorer={voyager}
6465
chains={[sepolia, mainnet]}
65-
defaultChainId={num.toBigInt(
66-
mockedConnection.controller!.chainId(),
67-
)}
66+
defaultChainId={num.toBigInt(connection.controller!.chainId())}
6867
provider={jsonRpcProvider({
69-
rpc: () => ({ nodeUrl: mockedConnection.rpcUrl }),
68+
rpc: () => ({ nodeUrl: connection.rpcUrl }),
7069
})}
7170
>
7271
{/* TODO: Remove once #1436 is merged */}

packages/keychain/src/components/transaction/ConfirmTransaction.stories.tsx

+38-35
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,47 @@ import type { Meta, StoryObj } from "@storybook/react";
33
import { ConfirmTransaction } from "./ConfirmTransaction";
44
import { ETH_CONTRACT_ADDRESS } from "@cartridge/utils";
55
import { VerifiableControllerTheme } from "#components/provider/connection";
6+
import { useConnection, createMockConnection } from "#hooks/connection.mock";
67

78
const meta = {
89
component: ConfirmTransaction,
9-
parameters: {
10-
connection: {
11-
controller: {
12-
estimateInvokeFee: () => ({
13-
suggestedMaxFee: "100",
14-
}),
15-
hasSession: () => true,
16-
session: () => true,
17-
},
18-
context: {
19-
origin: "http://localhost:6001",
20-
type: "execute",
21-
transactions: [
22-
{
23-
contractAddress: ETH_CONTRACT_ADDRESS,
24-
entrypoint: "approve",
25-
calldata: [
26-
"0x0000000000000000000000000000000000000000000000000000000000000000",
27-
"0x0",
28-
"0x0",
29-
],
30-
},
31-
{
32-
contractAddress: ETH_CONTRACT_ADDRESS,
33-
entrypoint: "transfer",
34-
calldata: [
35-
"0x0000000000000000000000000000000000000000000000000000000000000000",
36-
"0x0",
37-
"0x0",
38-
],
39-
},
40-
],
41-
onCancel: () => {},
42-
},
43-
},
10+
beforeEach: () => {
11+
useConnection.mockReturnValue(
12+
createMockConnection({
13+
controller: {
14+
estimateInvokeFee: () => ({
15+
suggestedMaxFee: "100",
16+
}),
17+
hasSession: () => true,
18+
session: () => true,
19+
},
20+
context: {
21+
origin: "http://localhost:6001",
22+
type: "execute",
23+
transactions: [
24+
{
25+
contractAddress: ETH_CONTRACT_ADDRESS,
26+
entrypoint: "approve",
27+
calldata: [
28+
"0x0000000000000000000000000000000000000000000000000000000000000000",
29+
"0x0",
30+
"0x0",
31+
],
32+
},
33+
{
34+
contractAddress: ETH_CONTRACT_ADDRESS,
35+
entrypoint: "transfer",
36+
calldata: [
37+
"0x0000000000000000000000000000000000000000000000000000000000000000",
38+
"0x0",
39+
"0x0",
40+
],
41+
},
42+
],
43+
onCancel: () => {},
44+
},
45+
}),
46+
);
4447
},
4548
} satisfies Meta<typeof ConfirmTransaction>;
4649

packages/keychain/src/hooks/connection.mock.ts

+37-19
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,27 @@ import Controller from "#utils/controller";
1212

1313
export * from "./connection";
1414

15-
export const mockedController = {
16-
address: fn(
17-
() => "0x0000000000000000000000000000000000000000000000000000000000000000",
18-
),
19-
username: fn(() => "user"),
20-
chainId: fn(() => constants.StarknetChainId.SN_MAIN),
21-
provider: new RpcProvider({
22-
nodeUrl: "https://api.cartridge.gg/x/starknet/mainnet",
23-
}),
24-
cartridge: {} as CartridgeAccount,
25-
cartridgeMeta: {} as CartridgeAccountMeta,
26-
} as unknown as Controller;
27-
28-
export const mockedConnection: ConnectionContextValue = {
15+
const defaultMockConnection: ConnectionContextValue = {
2916
context: {
3017
type: "connect",
3118
origin: "http://localhost:3002",
3219
policies: [],
3320
resolve: fn(),
3421
reject: fn(),
3522
} as ConnectCtx,
36-
controller: mockedController,
23+
controller: {
24+
address: fn(
25+
() =>
26+
"0x0000000000000000000000000000000000000000000000000000000000000000",
27+
),
28+
username: fn(() => "user"),
29+
chainId: fn(() => constants.StarknetChainId.SN_MAIN),
30+
provider: new RpcProvider({
31+
nodeUrl: "https://api.cartridge.gg/x/starknet/mainnet",
32+
}),
33+
cartridge: {} as CartridgeAccount,
34+
cartridgeMeta: {} as CartridgeAccountMeta,
35+
} as unknown as Controller,
3736
origin: "http://localhost:3002",
3837
rpcUrl: "http://api.cartridge.gg/x/starknet/mainnet",
3938
policies: {} as ParsedSessionPolicies,
@@ -47,10 +46,29 @@ export const mockedConnection: ConnectionContextValue = {
4746
openSettings: fn(),
4847
};
4948

50-
export const useConnectionValue: Mock<() => ConnectionContextValue> = fn(() => {
51-
return mockedConnection;
52-
});
49+
export function createMockConnection(
50+
// Better way to type this? Failed to implement `DeepPartial<UpgradeInterface>` type
51+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52+
overrides?: any,
53+
) {
54+
return {
55+
...defaultMockConnection,
56+
...overrides,
57+
controller: {
58+
...defaultMockConnection.controller,
59+
...overrides?.controller,
60+
},
61+
context: {
62+
...defaultMockConnection.context,
63+
...overrides?.context,
64+
},
65+
};
66+
}
67+
68+
export const useConnectionValue: Mock<() => ConnectionContextValue> = fn(
69+
() => defaultMockConnection,
70+
);
5371

5472
export const useConnection: Mock<() => ConnectionContextValue> = fn(
55-
() => mockedConnection,
73+
() => defaultMockConnection,
5674
);

0 commit comments

Comments
 (0)