-
Notifications
You must be signed in to change notification settings - Fork 79
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
1 parent
7a57307
commit 7162f23
Showing
35 changed files
with
1,332 additions
and
602 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
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,7 @@ | ||
import { cn } from 'utilities'; | ||
|
||
export interface ContainerProps extends React.HTMLAttributes<HTMLDivElement> {} | ||
|
||
export const Container: React.FC<ContainerProps> = ({ className, ...otherProps }) => ( | ||
<div className={cn('w-full', className)} {...otherProps} /> | ||
); |
91 changes: 91 additions & 0 deletions
91
apps/evm/src/containers/ConnectWallet/__tests__/index.spec.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,91 @@ | ||
import { fireEvent, waitFor } from '@testing-library/react'; | ||
import type Vi from 'vitest'; | ||
|
||
import fakeAccountAddress from '__mocks__/models/address'; | ||
import { en } from 'libs/translations'; | ||
import { useAuthModal, useSwitchChain } from 'libs/wallet'; | ||
import { renderComponent } from 'testUtils/render'; | ||
import { ChainId } from 'types'; | ||
import { ConnectWallet } from '..'; | ||
|
||
describe('ConnectWallet', () => { | ||
beforeEach(() => { | ||
(useAuthModal as Vi.Mock).mockReturnValue({ openAuthModal: vi.fn() }); | ||
(useSwitchChain as Vi.Mock).mockReturnValue({ switchChain: vi.fn() }); | ||
}); | ||
|
||
it('renders without crashing', () => { | ||
renderComponent(<ConnectWallet />); | ||
}); | ||
|
||
it('displays authentication button when user is not connected', async () => { | ||
const { getByText } = renderComponent(<ConnectWallet />); | ||
const connectButton = getByText(en.connectWallet.connectButton); | ||
expect(connectButton).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls openAuthModal when connect button is clicked', async () => { | ||
const mockOpenAuthModal = vi.fn(); | ||
(useAuthModal as Vi.Mock).mockReturnValue({ openAuthModal: mockOpenAuthModal }); | ||
|
||
const { getByText } = renderComponent(<ConnectWallet />); | ||
const connectButton = getByText(en.connectWallet.connectButton); | ||
|
||
fireEvent.click(connectButton); | ||
|
||
await waitFor(() => { | ||
expect(mockOpenAuthModal).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
it('displays chain switching button when current chain is different from chainId parameter', async () => { | ||
const { getByText } = renderComponent(<ConnectWallet chainId={ChainId.OPBNB_TESTNET} />, { | ||
accountAddress: fakeAccountAddress, | ||
}); | ||
|
||
const switchChainButton = getByText( | ||
en.connectWallet.switchChain.replace('{{chainName}}', 'opBNB testnet'), | ||
); | ||
expect(switchChainButton).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls switchChain when switch chain button is clicked', async () => { | ||
const mockSwitchChain = vi.fn(); | ||
(useSwitchChain as Vi.Mock).mockReturnValue({ switchChain: mockSwitchChain }); | ||
|
||
const { getByText } = renderComponent(<ConnectWallet chainId={ChainId.OPBNB_TESTNET} />, { | ||
accountAddress: fakeAccountAddress, | ||
}); | ||
|
||
const switchChainButton = getByText( | ||
en.connectWallet.switchChain.replace('{{chainName}}', 'opBNB testnet'), | ||
); | ||
|
||
fireEvent.click(switchChainButton); | ||
|
||
await waitFor(() => { | ||
expect(mockSwitchChain).toHaveBeenCalledTimes(1); | ||
expect(mockSwitchChain).toHaveBeenCalledWith({ chainId: ChainId.OPBNB_TESTNET }); | ||
}); | ||
}); | ||
|
||
it('renders children when user is connected and on the correct chain', () => { | ||
const { getByText } = renderComponent( | ||
<ConnectWallet chainId={ChainId.BSC_TESTNET}> | ||
<div>Child Component</div> | ||
</ConnectWallet>, | ||
{ | ||
accountAddress: fakeAccountAddress, | ||
}, | ||
); | ||
|
||
expect(getByText('Child Component')).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays the message prop when provided', () => { | ||
const message = 'Custom connect wallet message'; | ||
const { getByText } = renderComponent(<ConnectWallet message={message} />); | ||
|
||
expect(getByText(message)).toBeInTheDocument(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,69 @@ | ||
/** @jsxImportSource @emotion/react */ | ||
import { useTranslation } from 'libs/translations'; | ||
import { useAccountAddress, useAuthModal } from 'libs/wallet'; | ||
import { useAccountAddress, useAuthModal, useChainId, useSwitchChain } from 'libs/wallet'; | ||
|
||
import { CHAIN_METADATA } from 'constants/chainMetadata'; | ||
import { useMemo } from 'react'; | ||
import type { ChainId } from 'types'; | ||
import { Button, type Variant } from '../../components/Button'; | ||
import { NoticeInfo } from '../../components/Notice'; | ||
import { useStyles } from './styles'; | ||
import { Container } from './Container'; | ||
|
||
export interface PromptProps { | ||
openAuthModal: () => void; | ||
connected: boolean; | ||
export interface ConnectWalletProps extends React.HTMLAttributes<HTMLDivElement> { | ||
chainId?: ChainId; | ||
buttonVariant?: Variant; | ||
message?: string; | ||
className?: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
export const Prompt: React.FC<PromptProps> = ({ | ||
message, | ||
openAuthModal, | ||
className, | ||
export const ConnectWallet: React.FC<ConnectWalletProps> = ({ | ||
children, | ||
connected, | ||
buttonVariant = 'secondary', | ||
chainId, | ||
message, | ||
buttonVariant, | ||
...otherProps | ||
}) => { | ||
const styles = useStyles(); | ||
const { t } = useTranslation(); | ||
const { accountAddress } = useAccountAddress(); | ||
const isUserConnected = !!accountAddress; | ||
|
||
return ( | ||
<div css={styles.container} className={className}> | ||
{connected ? ( | ||
children | ||
) : ( | ||
<> | ||
{!!message && <NoticeInfo css={styles.notice} description={message} />} | ||
|
||
<Button variant={buttonVariant} className="w-full" onClick={openAuthModal}> | ||
{t('connectWallet.connectButton')} | ||
</Button> | ||
</> | ||
)} | ||
</div> | ||
const { chainId: currentChainId } = useChainId(); | ||
|
||
const isOnWrongChain = useMemo( | ||
() => chainId && currentChainId !== chainId, | ||
[currentChainId, chainId], | ||
); | ||
}; | ||
|
||
export const ConnectWallet: React.FC<Omit<PromptProps, 'connected' | 'openAuthModal'>> = props => { | ||
const { accountAddress } = useAccountAddress(); | ||
const chainMetadata = chainId && CHAIN_METADATA[chainId]; | ||
|
||
const { openAuthModal } = useAuthModal(); | ||
return <Prompt {...props} openAuthModal={openAuthModal} connected={!!accountAddress} />; | ||
const { switchChain } = useSwitchChain(); | ||
const handleSwitchChain = () => chainId && switchChain({ chainId }); | ||
|
||
const { t } = useTranslation(); | ||
|
||
if (!isUserConnected) { | ||
return ( | ||
<Container {...otherProps}> | ||
{!!message && <NoticeInfo className="mb-8" description={message} />} | ||
|
||
<Button variant={buttonVariant} className="w-full" onClick={openAuthModal}> | ||
{t('connectWallet.connectButton')} | ||
</Button> | ||
</Container> | ||
); | ||
} | ||
|
||
if (isOnWrongChain) { | ||
return ( | ||
<Container {...otherProps}> | ||
<Button variant={buttonVariant} className="w-full" onClick={handleSwitchChain}> | ||
{t('connectWallet.switchChain', { | ||
chainName: chainMetadata?.name, | ||
})} | ||
</Button> | ||
</Container> | ||
); | ||
} | ||
|
||
return <Container {...otherProps}>{children}</Container>; | ||
}; |
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.