-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 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
107 changes: 107 additions & 0 deletions
107
src/components/common/ConnectWallet/__tests__/MPCLogin.test.tsx
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,107 @@ | ||
import { act, render, waitFor } from '@/tests/test-utils' | ||
import * as useWallet from '@/hooks/wallets/useWallet' | ||
import * as useMPCWallet from '@/hooks/wallets/mpc/useMPCWallet' | ||
import MPCLogin from '../MPCLogin' | ||
import { hexZeroPad } from '@ethersproject/bytes' | ||
import { type EIP1193Provider } from '@web3-onboard/common' | ||
import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/module' | ||
import { MpcWalletProvider } from '../MPCWalletProvider' | ||
|
||
describe('WalletLogin', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should render continue with connected account', async () => { | ||
const mockOnLogin = jest.fn() | ||
const walletAddress = hexZeroPad('0x1', 20) | ||
jest.spyOn(useWallet, 'default').mockReturnValue({ | ||
address: walletAddress, | ||
chainId: '5', | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
provider: {} as unknown as EIP1193Provider, | ||
}) | ||
jest.spyOn(useMPCWallet, 'useMPCWallet').mockReturnValue({ | ||
userInfo: { | ||
email: '[email protected]', | ||
name: 'Test Testermann', | ||
profileImage: 'test.png', | ||
}, | ||
triggerLogin: jest.fn(), | ||
walletState: useMPCWallet.MPCWalletState.READY, | ||
} as unknown as useMPCWallet.MPCWalletHook) | ||
|
||
const result = render( | ||
<MpcWalletProvider> | ||
<MPCLogin onLogin={mockOnLogin} /> | ||
</MpcWalletProvider>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(result.findByText('Continue as Test Testermann')).resolves.toBeDefined() | ||
}) | ||
|
||
// We do not automatically invoke the callback as the user did not actively connect | ||
expect(mockOnLogin).not.toHaveBeenCalled() | ||
|
||
const button = await result.findByRole('button') | ||
button.click() | ||
|
||
expect(mockOnLogin).toHaveBeenCalled() | ||
}) | ||
|
||
it('should render connect wallet and invoke the callback on connection if no wallet is connected', async () => { | ||
const mockOnLogin = jest.fn() | ||
const walletAddress = hexZeroPad('0x1', 20) | ||
const mockUseWallet = jest.spyOn(useWallet, 'default').mockReturnValue(null) | ||
const mockTriggerLogin = jest.fn() | ||
const mockUseMPCWallet = jest.spyOn(useMPCWallet, 'useMPCWallet').mockReturnValue({ | ||
userInfo: { | ||
email: undefined, | ||
name: undefined, | ||
profileImage: undefined, | ||
}, | ||
triggerLogin: mockTriggerLogin, | ||
walletState: useMPCWallet.MPCWalletState.NOT_INITIALIZED, | ||
} as unknown as useMPCWallet.MPCWalletHook) | ||
|
||
const result = render( | ||
<MpcWalletProvider> | ||
<MPCLogin onLogin={mockOnLogin} /> | ||
</MpcWalletProvider>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(result.findByText('Continue with Google')).resolves.toBeDefined() | ||
}) | ||
|
||
// We do not automatically invoke the callback as the user did not actively connect | ||
expect(mockOnLogin).not.toHaveBeenCalled() | ||
|
||
await act(async () => { | ||
// Click the button and mock a successful login | ||
const button = await result.findByRole('button') | ||
button.click() | ||
mockUseMPCWallet.mockReset().mockReturnValue({ | ||
userInfo: { | ||
email: '[email protected]', | ||
name: 'Test Testermann', | ||
profileImage: 'test.png', | ||
}, | ||
triggerLogin: jest.fn(), | ||
walletState: useMPCWallet.MPCWalletState.READY, | ||
} as unknown as useMPCWallet.MPCWalletHook) | ||
|
||
mockUseWallet.mockReset().mockReturnValue({ | ||
address: walletAddress, | ||
chainId: '5', | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
provider: {} as unknown as EIP1193Provider, | ||
}) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(mockOnLogin).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
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