-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: select proper MetaMask provider when conflicts (#34)
* feat: update eth-testing version * fix: filter providers in ethereum object of window * fix: use providers instead of providerMap * feat: update test titles * feat: update comment
- Loading branch information
Showing
6 changed files
with
178 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { generateTestingUtils } from "eth-testing"; | ||
|
||
import { useMetaMask, MetaMaskProvider } from "../"; | ||
|
||
describe("`window.ethereum` conflict tests", () => { | ||
describe("when the `ethereum` object has a `providerMap` field", () => { | ||
test("when the `providers` does not have a `MetaMask` provider, it should synchronise in `unavailable` status", async () => { | ||
let originalEth = (window as any).ethereum; | ||
const testingUtils = generateTestingUtils(); | ||
const coinbaseProvider = testingUtils.getProvider(); | ||
const ethereum = { | ||
providers: [coinbaseProvider], | ||
}; | ||
(window as any).ethereum = ethereum; | ||
|
||
const { result } = renderHook(useMetaMask, { wrapper: MetaMaskProvider }); | ||
|
||
expect(result.current.status).toEqual("unavailable"); | ||
|
||
(window as any).ethereum = originalEth; | ||
}); | ||
|
||
test("when the `providers` does have a valid MetaMask provider, it should synchronise in `notConnected` status", async () => { | ||
let originalEth = (window as any).ethereum; | ||
const testingUtils = generateTestingUtils(); | ||
const coinbaseProvider = testingUtils.getProvider(); | ||
const metaMaskTestingUtils = generateTestingUtils({ | ||
providerType: "MetaMask", | ||
}); | ||
const metaMaskProvider = metaMaskTestingUtils.getProvider(); | ||
const ethereum = { | ||
providers: [coinbaseProvider, metaMaskProvider], | ||
}; | ||
(window as any).ethereum = ethereum; | ||
|
||
metaMaskTestingUtils.mockNotConnectedWallet(); | ||
|
||
const { result, waitForNextUpdate } = renderHook(useMetaMask, { | ||
wrapper: MetaMaskProvider, | ||
}); | ||
|
||
expect(result.current.status).toEqual("initializing"); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.status).toEqual("notConnected"); | ||
|
||
(window as any).ethereum = originalEth; | ||
}); | ||
}); | ||
|
||
test("when the `MetaMask` provider is corrupted or removed in the meantime, it should throw", async () => { | ||
let originalEth = (window as any).ethereum; | ||
const testingUtils = generateTestingUtils(); | ||
const coinbaseProvider = testingUtils.getProvider(); | ||
const metaMaskTestingUtils = generateTestingUtils({ | ||
providerType: "MetaMask", | ||
}); | ||
const metaMaskProvider = metaMaskTestingUtils.getProvider(); | ||
const providers = [coinbaseProvider, metaMaskProvider]; | ||
const ethereum = { providers }; | ||
(window as any).ethereum = ethereum; | ||
|
||
metaMaskTestingUtils.mockNotConnectedWallet(); | ||
|
||
const { result, waitForNextUpdate } = renderHook(useMetaMask, { | ||
wrapper: MetaMaskProvider, | ||
}); | ||
|
||
expect(result.current.status).toEqual("initializing"); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.status).toEqual("notConnected"); | ||
|
||
providers.pop(); | ||
|
||
expect(() => result.current.connect()).toThrowError( | ||
"MetaMask provider must be present in order to use this method" | ||
); | ||
|
||
(window as any).ethereum = originalEth; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters