-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: disconnect previous wallet #2712
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/SocialLoginModule' | ||
import { faker } from '@faker-js/faker' | ||
import type { EIP1193Provider, OnboardAPI, WalletState } from '@web3-onboard/core' | ||
import { getConnectedWallet, switchWallet } from '../useOnboard' | ||
|
||
// mock wallets | ||
jest.mock('@/hooks/wallets/wallets', () => ({ | ||
getDefaultWallets: jest.fn(() => []), | ||
getRecommendedInjectedWallets: jest.fn(() => []), | ||
})) | ||
|
||
describe('useOnboard', () => { | ||
describe('getConnectedWallet', () => { | ||
it('returns the connected wallet', () => { | ||
const wallets = [ | ||
{ | ||
label: 'Wallet 1', | ||
icon: 'wallet1.svg', | ||
provider: null as unknown as EIP1193Provider, | ||
chains: [{ id: '0x4' }], | ||
accounts: [ | ||
{ | ||
address: '0x1234567890123456789012345678901234567890', | ||
ens: null, | ||
balance: null, | ||
}, | ||
], | ||
}, | ||
{ | ||
label: 'Wallet 2', | ||
icon: 'wallet2.svg', | ||
provider: null as unknown as EIP1193Provider, | ||
chains: [{ id: '0x100' }], | ||
accounts: [ | ||
{ | ||
address: '0x2', | ||
ens: null, | ||
balance: null, | ||
}, | ||
], | ||
}, | ||
] as WalletState[] | ||
|
||
expect(getConnectedWallet(wallets)).toEqual({ | ||
label: 'Wallet 1', | ||
icon: 'wallet1.svg', | ||
address: '0x1234567890123456789012345678901234567890', | ||
provider: wallets[0].provider, | ||
chainId: '4', | ||
}) | ||
}) | ||
|
||
it('should return null if the address is invalid', () => { | ||
const wallets = [ | ||
{ | ||
label: 'Wallet 1', | ||
icon: 'wallet1.svg', | ||
provider: null as unknown as EIP1193Provider, | ||
chains: [{ id: '0x4' }], | ||
accounts: [ | ||
{ | ||
address: '0xinvalid', | ||
ens: null, | ||
balance: null, | ||
}, | ||
], | ||
}, | ||
] as WalletState[] | ||
|
||
expect(getConnectedWallet(wallets)).toBeNull() | ||
}) | ||
}) | ||
|
||
describe('switchWallet', () => { | ||
it('should keep the social signer wallet connected if switching wallets fails', async () => { | ||
const mockOnboard = { | ||
state: { | ||
get: jest.fn().mockReturnValue({ | ||
wallets: [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
}, | ||
], | ||
}), | ||
}, | ||
connectWallet: jest.fn().mockRejectedValue('Error'), | ||
disconnectWallet: jest.fn(), | ||
} | ||
|
||
await switchWallet(mockOnboard as unknown as OnboardAPI) | ||
|
||
expect(mockOnboard.connectWallet).toHaveBeenCalled() | ||
expect(mockOnboard.disconnectWallet).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should not disconnect the social signer wallet label if the same label connects', async () => { | ||
const mockNewState = [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
}, | ||
] | ||
|
||
const mockOnboard = { | ||
state: { | ||
get: jest.fn().mockReturnValue({ | ||
wallets: [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
}, | ||
], | ||
}), | ||
}, | ||
connectWallet: jest.fn().mockResolvedValue(mockNewState), | ||
disconnectWallet: jest.fn(), | ||
} | ||
|
||
await switchWallet(mockOnboard as unknown as OnboardAPI) | ||
|
||
expect(mockOnboard.connectWallet).toHaveBeenCalled() | ||
expect(mockOnboard.disconnectWallet).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should not disconnect non social signer wallets if new wallet connects', async () => { | ||
const mockNewState = [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: 'MetaMask', | ||
}, | ||
] | ||
|
||
const mockOnboard = { | ||
state: { | ||
get: jest.fn().mockReturnValue({ | ||
wallets: [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: 'Wallet Connect', | ||
}, | ||
], | ||
}), | ||
}, | ||
connectWallet: jest.fn().mockResolvedValue(mockNewState), | ||
disconnectWallet: jest.fn(), | ||
} | ||
|
||
await switchWallet(mockOnboard as unknown as OnboardAPI) | ||
|
||
expect(mockOnboard.connectWallet).toBeCalled() | ||
expect(mockOnboard.disconnectWallet).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should disconnect the social signer wallet label if new wallet connects', async () => { | ||
const mockNewState = [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: 'MetaMask', | ||
}, | ||
] | ||
|
||
const mockOnboard = { | ||
state: { | ||
get: jest.fn().mockReturnValue({ | ||
wallets: [ | ||
{ | ||
accounts: [ | ||
{ | ||
address: faker.finance.ethereumAddress(), | ||
ens: undefined, | ||
}, | ||
], | ||
chains: [ | ||
{ | ||
id: '5', | ||
}, | ||
], | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
}, | ||
], | ||
}), | ||
}, | ||
connectWallet: jest.fn().mockResolvedValue(mockNewState), | ||
disconnectWallet: jest.fn(), | ||
} | ||
|
||
await switchWallet(mockOnboard as unknown as OnboardAPI) | ||
|
||
expect(mockOnboard.connectWallet).toBeCalled() | ||
expect(mockOnboard.disconnectWallet).toHaveBeenCalledWith({ label: ONBOARD_MPC_MODULE_LABEL }) | ||
}) | ||
}) | ||
}) |
This file was deleted.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we disconnect if the old wallet is social login? What if the new wallet is social login and the old one is MM? Will it work fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly.
Only the Social signer will be disconnected if switched to another wallet.
We could also remove this behavior once we support every chain. But this is currently causing issues QA found.