-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fix wagmi not disconnecting and add adapter tests (#2751)
Co-authored-by: tomiir <[email protected]>
- Loading branch information
Showing
6 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { describe, expect, test, vi } from 'vitest' | ||
import { connect, disconnect, getAccount as getAccount_wagmi } from '@wagmi/core' | ||
import { ConstantsUtil } from '@web3modal/scaffold-utils' | ||
import { appKitMock, mockAccount, wagmiConfigMock } from './mocks/adapter.mock' | ||
import { mainnet } from 'viem/chains' | ||
|
||
describe('wagmi adapter', () => { | ||
test('should connect to adapter', async () => { | ||
expect(appKitMock.getConnectors().length).toBe(1) | ||
expect(appKitMock.getIsConnectedState()).toBe(false) | ||
expect(appKitMock.getCaipAddress()).toBeUndefined() | ||
|
||
const setApprovedCaipNetworksData = vi | ||
.spyOn(appKitMock, 'setApprovedCaipNetworksData') | ||
.mockResolvedValue() | ||
|
||
await connect(wagmiConfigMock, { connector: wagmiConfigMock.connectors[0]! }) | ||
|
||
expect(setApprovedCaipNetworksData).toHaveBeenCalledOnce() | ||
|
||
expect(appKitMock.getIsConnectedState()).toBe(true) | ||
expect(appKitMock.getCaipAddress()).toBe( | ||
`${ConstantsUtil.EIP155}:${mainnet.id}:${mockAccount.address}` | ||
) | ||
|
||
const wagmiAccount = getAccount_wagmi(wagmiConfigMock) | ||
|
||
expect(wagmiAccount.status).toBe('connected') | ||
expect(wagmiAccount.address).toBe(mockAccount.address) | ||
}) | ||
|
||
test('should disconnect from adapter', async () => { | ||
// Check if already connected | ||
expect(appKitMock.getIsConnectedState()).toBe(true) | ||
|
||
const resetAccount = vi.spyOn(appKitMock, 'resetAccount').mockResolvedValue() | ||
const resetWcConnection = vi.spyOn(appKitMock, 'resetWcConnection').mockResolvedValue() | ||
const resetNetwork = vi.spyOn(appKitMock, 'resetNetwork').mockResolvedValue() | ||
|
||
await disconnect(wagmiConfigMock) | ||
|
||
expect(resetAccount).toHaveBeenCalledOnce() | ||
expect(resetWcConnection).toHaveBeenCalledOnce() | ||
expect(resetNetwork).toHaveBeenCalledOnce() | ||
|
||
expect(appKitMock.getIsConnectedState()).toBe(false) | ||
|
||
const wagmiAccount = getAccount_wagmi(wagmiConfigMock) | ||
|
||
expect(wagmiAccount.status).toBe('disconnected') | ||
expect(wagmiAccount.address).toBeUndefined() | ||
}) | ||
}) |
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,38 @@ | ||
import { EVMWagmiClient } from '@web3modal/base/adapters/evm/wagmi' | ||
import { mainnet } from 'viem/chains' | ||
import { createConfig, http } from 'wagmi' | ||
import { mock } from 'wagmi/connectors' | ||
|
||
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts' | ||
import { AppKit, type SdkVersion } from '@web3modal/base' | ||
|
||
const privateKey = generatePrivateKey() | ||
export const mockAccount = privateKeyToAccount(privateKey) | ||
|
||
export const wagmiConfigMock = createConfig({ | ||
chains: [mainnet], | ||
connectors: [mock({ accounts: [mockAccount.address] })], | ||
transports: { | ||
[mainnet.id]: http() | ||
} | ||
}) | ||
|
||
const wagmiAdapterMock = new EVMWagmiClient({ | ||
wagmiConfig: wagmiConfigMock | ||
}) | ||
|
||
const mockAppKitData = { | ||
defaultChain: wagmiAdapterMock.defaultChain, | ||
adapters: [wagmiAdapterMock], | ||
metadata: { | ||
description: 'Desc', | ||
name: 'Name', | ||
url: 'url.com', | ||
icons: ['icon.png'] | ||
}, | ||
projectId: '1234', | ||
sdkVersion: 'react-wagmi-4.0.13' as SdkVersion, | ||
sdkType: 'w3m' as const | ||
} | ||
|
||
export const appKitMock = new AppKit(mockAppKitData) |
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 |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
"baseUrl": "../../" | ||
}, | ||
"extends": "../../tsconfig.json", | ||
"include": ["exports"] | ||
"include": ["exports", "tests"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.