forked from coinbase/onchainkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wallet address copy (coinbase#730)
- Loading branch information
Showing
7 changed files
with
208 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@coinbase/onchainkit": patch | ||
--- | ||
|
||
- **feat**: add copyAddressOnClick to Identity component. By @kyhyco #730 | ||
- **feat**: add EthBalance component to identity. By @kyhyco #729 | ||
- **feat**: add ConnectWallet component. By @kyhyco #720 #728 |
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 |
---|---|---|
@@ -1,16 +1,36 @@ | ||
import type { IdentityReact } from '../types'; | ||
import { IdentityProvider } from './IdentityProvider'; | ||
import { IdentityLayout } from './IdentityLayout'; | ||
import { useCallback } from 'react'; | ||
|
||
export function Identity({ | ||
address, | ||
children, | ||
className, | ||
schemaId, | ||
copyAddressOnClick, | ||
}: IdentityReact) { | ||
// istanbul ignore next | ||
const handleCopy = useCallback(async () => { | ||
if (!address) return false; | ||
|
||
try { | ||
await navigator.clipboard.writeText(address); | ||
return true; | ||
} catch (e) { | ||
console.error('Failed to copy: ', e); | ||
return false; | ||
} | ||
}, [address]); | ||
|
||
// istanbul ignore next | ||
const onClick = copyAddressOnClick ? handleCopy : undefined; | ||
|
||
return ( | ||
<IdentityProvider address={address} schemaId={schemaId}> | ||
<IdentityLayout className={className}>{children}</IdentityLayout> | ||
<IdentityLayout className={className} onClick={onClick}> | ||
{children} | ||
</IdentityLayout> | ||
</IdentityProvider> | ||
); | ||
} |
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,76 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import { IdentityLayout } from './IdentityLayout'; | ||
import { Avatar } from './Avatar'; | ||
import { Name } from './Name'; | ||
import { Address } from './Address'; | ||
import { EthBalance } from './EthBalance'; | ||
|
||
const handleCopy = jest.fn().mockResolvedValue(true); | ||
|
||
jest.mock('./Avatar', () => ({ | ||
Avatar: jest.fn(() => <div>Avatar</div>), | ||
})); | ||
|
||
jest.mock('./Name', () => ({ | ||
Name: jest.fn(() => <div>Name</div>), | ||
})); | ||
|
||
jest.mock('./Address', () => ({ | ||
Address: jest.fn(() => <div>Address</div>), | ||
})); | ||
|
||
jest.mock('./EthBalance', () => ({ | ||
EthBalance: jest.fn(() => <div>EthBalance</div>), | ||
})); | ||
|
||
const renderComponent = () => { | ||
return render( | ||
<IdentityLayout onClick={handleCopy} className="custom-class"> | ||
<Avatar /> | ||
<Name /> | ||
<Address /> | ||
<EthBalance /> | ||
</IdentityLayout>, | ||
); | ||
}; | ||
|
||
describe('IdentityLayout', () => { | ||
it('shows popover on hover and hides on mouse leave', async () => { | ||
renderComponent(); | ||
|
||
const container = screen.getByTestId('ockIdentity_container'); | ||
fireEvent.mouseEnter(container); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Copy')).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.mouseLeave(container); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('Copy')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('changes popover text to "Copied" on click', async () => { | ||
renderComponent(); | ||
|
||
const container = screen.getByTestId('ockIdentity_container'); | ||
fireEvent.mouseEnter(container); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Copy')).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(container); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Copied')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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