Skip to content

Commit b74b5a8

Browse files
committed
Add test for Snap calling wallet_switchEthereumChain
1 parent 7095e61 commit b74b5a8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,61 @@ describe('switchEthereumChainHandler', () => {
175175
},
176176
);
177177
});
178+
179+
it('calls `grantPermittedChainsPermissionIncremental` if the origin is a Snap', async () => {
180+
const mocks = makeMocks({
181+
overrides: {
182+
grantPermittedChainsPermissionIncremental: jest.fn(),
183+
},
184+
});
185+
186+
const switchEthereumChainHandler = switchEthereumChain.implementation;
187+
await switchEthereumChainHandler(
188+
{
189+
origin: 'npm:foo-snap',
190+
params: [{ chainId: CHAIN_IDS.MAINNET }],
191+
},
192+
{},
193+
jest.fn(),
194+
jest.fn(),
195+
mocks,
196+
);
197+
198+
expect(
199+
mocks.grantPermittedChainsPermissionIncremental,
200+
).toHaveBeenCalledTimes(1);
201+
expect(mocks.setActiveNetwork).toHaveBeenCalledTimes(1);
202+
expect(mocks.setActiveNetwork).toHaveBeenCalledWith(
203+
createMockMainnetConfiguration().rpcEndpoints[0].networkClientId,
204+
);
205+
});
206+
207+
it('does not call `grantPermittedChainsPermissionIncremental` if the origin is a Snap, but the permission is already set', async () => {
208+
const mocks = makeMocks({
209+
permissionedChainIds: [CHAIN_IDS.MAINNET],
210+
overrides: {
211+
grantPermittedChainsPermissionIncremental: jest.fn(),
212+
},
213+
});
214+
215+
const switchEthereumChainHandler = switchEthereumChain.implementation;
216+
await switchEthereumChainHandler(
217+
{
218+
origin: 'npm:foo-snap',
219+
params: [{ chainId: CHAIN_IDS.MAINNET }],
220+
},
221+
{},
222+
jest.fn(),
223+
jest.fn(),
224+
mocks,
225+
);
226+
227+
expect(
228+
mocks.grantPermittedChainsPermissionIncremental,
229+
).not.toHaveBeenCalled();
230+
expect(mocks.setActiveNetwork).toHaveBeenCalledTimes(1);
231+
expect(mocks.setActiveNetwork).toHaveBeenCalledWith(
232+
createMockMainnetConfiguration().rpcEndpoints[0].networkClientId,
233+
);
234+
});
178235
});

0 commit comments

Comments
 (0)