Skip to content

Commit c4556d9

Browse files
author
Erik Ritter
authored
Merge pull request #180 from gjsyme/feature/test-usewallet
Create test for useWallet
2 parents 8032441 + e79b399 commit c4556d9

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@preconstruct/cli": "^2.1.5",
4141
"@testing-library/jest-dom": "^5.15.1",
4242
"@testing-library/react": "^12.1.2",
43+
"@testing-library/react-hooks": "^7.0.2",
4344
"chromatic": "^6.1.0",
4445
"husky": "^7.0.4",
4546
"jest": "^26.6.3",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import { useWallet } from './useWallet';
3+
import { Provider } from '../Provider';
4+
import { renderHook } from '@testing-library/react-hooks';
5+
6+
describe('useWallet tests', () => {
7+
let windowSpy;
8+
9+
beforeEach(() => {
10+
windowSpy = jest.spyOn(window, 'window', 'get');
11+
});
12+
13+
afterEach(() => {
14+
windowSpy.mockRestore();
15+
});
16+
17+
test('should instantiate for valid input types', () => {
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+
});
28+
29+
// initially is chainId undefined / requires network per the argument
30+
test('should be able to switch networks', () => {
31+
const testNetwork = 111;
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+
}
49+
}
50+
}));
51+
52+
result.current.switchToCorrectNetwork();
53+
expect(windowSpy).toHaveBeenCalled();
54+
expect(requestArgs).toStrictEqual({
55+
method: 'wallet_switchEthereumChain',
56+
// this passes (for high number networks)
57+
params: [{ chainId: `0x${testNetwork}` }]
58+
// The test should probably look like this
59+
// if we are in fact converting network numbers to hex
60+
// but fails with the current implementation
61+
// params: [{ chainId: `0x${Number(testNetwork).toString(16)}` }]
62+
});
63+
});
64+
});

yarn.lock

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,17 @@
40294029
lodash "^4.17.15"
40304030
redent "^3.0.0"
40314031

4032+
"@testing-library/react-hooks@^7.0.2":
4033+
version "7.0.2"
4034+
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-7.0.2.tgz#3388d07f562d91e7f2431a4a21b5186062ecfee0"
4035+
integrity sha512-dYxpz8u9m4q1TuzfcUApqi8iFfR6R0FaMbr2hjZJy1uC8z+bO/K4v8Gs9eogGKYQop7QsrBTFkv/BCF7MzD2Cg==
4036+
dependencies:
4037+
"@babel/runtime" "^7.12.5"
4038+
"@types/react" ">=16.9.0"
4039+
"@types/react-dom" ">=16.9.0"
4040+
"@types/react-test-renderer" ">=16.9.0"
4041+
react-error-boundary "^3.1.0"
4042+
40324043
"@testing-library/react@^12.1.2":
40334044
version "12.1.2"
40344045
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-12.1.2.tgz#f1bc9a45943461fa2a598bb4597df1ae044cfc76"
@@ -4315,6 +4326,13 @@
43154326
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb"
43164327
integrity sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==
43174328

4329+
"@types/react-dom@>=16.9.0":
4330+
version "17.0.11"
4331+
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.11.tgz#e1eadc3c5e86bdb5f7684e00274ae228e7bcc466"
4332+
integrity sha512-f96K3k+24RaLGVu/Y2Ng3e1EbZ8/cVJvypZWd7cy0ofCBaf2lcM46xNhycMZ2xGwbBjRql7hOlZ+e2WlJ5MH3Q==
4333+
dependencies:
4334+
"@types/react" "*"
4335+
43184336
"@types/react-dom@^16.9.10":
43194337
version "16.9.14"
43204338
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.9.14.tgz#674b8f116645fe5266b40b525777fc6bb8eb3bcd"
@@ -4329,6 +4347,13 @@
43294347
dependencies:
43304348
"@types/react" "*"
43314349

4350+
"@types/react-test-renderer@>=16.9.0":
4351+
version "17.0.1"
4352+
resolved "https://registry.yarnpkg.com/@types/react-test-renderer/-/react-test-renderer-17.0.1.tgz#3120f7d1c157fba9df0118dae20cb0297ee0e06b"
4353+
integrity sha512-3Fi2O6Zzq/f3QR9dRnlnHso9bMl7weKCviFmfF6B4LS1Uat6Hkm15k0ZAQuDz+UBq6B3+g+NM6IT2nr5QgPzCw==
4354+
dependencies:
4355+
"@types/react" "*"
4356+
43324357
"@types/react@*", "@types/react@^17.0.15", "@types/react@^17.0.36":
43334358
version "17.0.37"
43344359
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.37.tgz#6884d0aa402605935c397ae689deed115caad959"
@@ -4338,6 +4363,15 @@
43384363
"@types/scheduler" "*"
43394364
csstype "^3.0.2"
43404365

4366+
"@types/react@>=16.9.0":
4367+
version "17.0.38"
4368+
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.38.tgz#f24249fefd89357d5fa71f739a686b8d7c7202bd"
4369+
integrity sha512-SI92X1IA+FMnP3qM5m4QReluXzhcmovhZnLNm3pyeQlooi02qI7sLiepEYqT678uNiyc25XfCqxREFpy3W7YhQ==
4370+
dependencies:
4371+
"@types/prop-types" "*"
4372+
"@types/scheduler" "*"
4373+
csstype "^3.0.2"
4374+
43414375
"@types/react@^16":
43424376
version "16.14.21"
43434377
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.21.tgz#35199b21a278355ec7a3c40003bd6a334bd4ae4a"
@@ -13632,6 +13666,13 @@ react-element-to-jsx-string@^14.3.4:
1363213666
is-plain-object "5.0.0"
1363313667
react-is "17.0.2"
1363413668

13669+
react-error-boundary@^3.1.0:
13670+
version "3.1.4"
13671+
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
13672+
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
13673+
dependencies:
13674+
"@babel/runtime" "^7.12.5"
13675+
1363513676
react-error-overlay@^6.0.9:
1363613677
version "6.0.9"
1363713678
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.9.tgz#3c743010c9359608c375ecd6bc76f35d93995b0a"

0 commit comments

Comments
 (0)