Skip to content

Commit cccc3e5

Browse files
committed
Wrap the useWallet tests in a describe, add comments
Improve readability of the tests and their output
1 parent 71d3357 commit cccc3e5

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

packages/hooks/src/hooks/useWallet.test.tsx

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,47 @@ afterEach(() => {
1313
windowSpy.mockRestore();
1414
});
1515

16-
test('should use the wallet', () => {
17-
const wrapper = ({ children }) => (
18-
<Provider network={1} infuraId={'abc123'}>
19-
{children}
20-
</Provider>
21-
);
22-
const { result } = renderHook(() => useWallet(), { wrapper });
23-
24-
expect(result.current.connected).toBe(false);
25-
expect(typeof result.current.connectWallet).toBe('function');
26-
});
16+
describe('useWallet tests', () => {
17+
test('should use the wallet', () => {
18+
const wrapper = ({ children }) => (
19+
<Provider network={1} infuraId={'abc123'}>
20+
{children}
21+
</Provider>
22+
);
23+
const { result } = renderHook(() => useWallet(), { wrapper });
24+
25+
expect(result.current.connected).toBe(false);
26+
expect(typeof result.current.connectWallet).toBe('function');
27+
});
2728

28-
// initially is chainId undefined / requires network per the argument
29-
test('should be able to switch networks', () => {
30-
const testNetwork = 2;
31-
const wrapper = ({ children }) => (
32-
<Provider network={testNetwork} infuraId={'abc123'}>
33-
{children}
34-
</Provider>
35-
);
36-
const { result } = renderHook(() => useWallet(), { wrapper });
37-
38-
let requestArgs;
39-
40-
windowSpy.mockImplementation(() => ({
41-
ethereum: {
42-
request: arg => {
43-
requestArgs = arg;
29+
// initially is chainId undefined / requires network per the argument
30+
test('should be able to switch networks', () => {
31+
const testNetwork = 2;
32+
const wrapper = ({ children }) => (
33+
<Provider network={testNetwork} infuraId={'abc123'}>
34+
{children}
35+
</Provider>
36+
);
37+
const { result } = renderHook(() => useWallet(), { wrapper });
38+
39+
let requestArgs;
40+
41+
// hijack the call to window.ethereum.request to capture those args
42+
// this ensures that we have values in the requestArgs if/when request is called
43+
// those are checked at the end
44+
windowSpy.mockImplementation(() => ({
45+
ethereum: {
46+
request: arg => {
47+
requestArgs = arg;
48+
}
4449
}
45-
}
46-
}));
47-
48-
result.current.switchToCorrectNetwork();
49-
expect(windowSpy).toHaveBeenCalled();
50-
expect(requestArgs).toStrictEqual({
51-
method: 'wallet_switchEthereumChain',
52-
params: [{ chainId: `0x${testNetwork}` }]
50+
}));
51+
52+
result.current.switchToCorrectNetwork();
53+
expect(windowSpy).toHaveBeenCalled();
54+
expect(requestArgs).toStrictEqual({
55+
method: 'wallet_switchEthereumChain',
56+
params: [{ chainId: `0x${testNetwork}` }]
57+
});
5358
});
5459
});

0 commit comments

Comments
 (0)