-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Address support format * feat: Address support format * test: update snapshots * feat: optimize code
- Loading branch information
1 parent
a81b4c9
commit 6ad444f
Showing
9 changed files
with
204 additions
and
299 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/web3/src/address/__tests__/__snapshots__/index.test.tsx.snap
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,17 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Address > display address with default format 1`] = ` | ||
<div | ||
class="ant-space css-dev-only-do-not-override-1ck3jst ant-space-horizontal ant-space-align-center ant-space-gap-row-small ant-space-gap-col-small ant-web3-address css-dev-only-do-not-override-1ck3jst" | ||
> | ||
<div | ||
class="ant-space-item" | ||
> | ||
<span | ||
class="ant-web3-address-text" | ||
> | ||
0x 21CD f097 4d53 a6e9 6eF0 5d7B 324a 9803 735f Fd3B | ||
</span> | ||
</div> | ||
</div> | ||
`; |
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,98 @@ | ||
import { Address } from '..'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
import { mockClipboard } from '../../utils/test-utils'; | ||
import { readCopyText } from '../../utils'; | ||
|
||
describe('Address', () => { | ||
let resetMockClipboard: () => void; | ||
beforeEach(() => { | ||
resetMockClipboard = mockClipboard(); | ||
}); | ||
afterEach(() => { | ||
resetMockClipboard(); | ||
}); | ||
|
||
it('mount correctly', () => { | ||
expect(() => render(<Address />)).not.toThrow(); | ||
}); | ||
|
||
it('display address', () => { | ||
const { baseElement } = render( | ||
<Address address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" />, | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe( | ||
'0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B', | ||
); | ||
}); | ||
|
||
it('display address with ellipsis', () => { | ||
const { baseElement } = render( | ||
<Address ellipsis address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" />, | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe('0x21CD...Fd3B'); | ||
}); | ||
|
||
it('display address with ellipsis and custom clip', () => { | ||
const { baseElement } = render( | ||
<Address | ||
ellipsis={{ | ||
headClip: 3, | ||
tailClip: 3, | ||
}} | ||
address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" | ||
/>, | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe('0x2...d3B'); | ||
}); | ||
|
||
it('display address with tooltip', async () => { | ||
const { baseElement } = render( | ||
<Address address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" ellipsis />, | ||
); | ||
|
||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe('0x21CD...Fd3B'); | ||
fireEvent.mouseEnter(baseElement.querySelector('.ant-web3-address-text')!); | ||
await vi.waitFor(() => { | ||
expect(baseElement.querySelector('.ant-tooltip-inner')?.textContent).toBe( | ||
'0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B', | ||
); | ||
}); | ||
}); | ||
|
||
it('display address with default format', () => { | ||
const { baseElement } = render( | ||
<Address address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" format />, | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe( | ||
'0x 21CD f097 4d53 a6e9 6eF0 5d7B 324a 9803 735f Fd3B', | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')).toMatchSnapshot(); | ||
}); | ||
|
||
it('display address with custom format', () => { | ||
const { baseElement } = render( | ||
<Address | ||
address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" | ||
format={(input) => { | ||
return input.slice(0, 10); | ||
}} | ||
/>, | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe('0x21CDf097'); | ||
}); | ||
it('display address with copyable', async () => { | ||
const { baseElement } = render( | ||
<Address address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" ellipsis copyable />, | ||
); | ||
expect(baseElement.querySelector('.ant-web3-address')?.textContent).toBe('0x21CD...Fd3B'); | ||
fireEvent.click(baseElement.querySelector('.anticon-copy')!); | ||
await vi.waitFor(() => { | ||
expect(baseElement.querySelector('.ant-message')).not.toBeNull(); | ||
expect(baseElement.querySelector('.ant-message-notice-content')?.textContent?.trim()).toBe( | ||
'Address Copied!', | ||
); | ||
expect(readCopyText()).resolves.toBe('0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B'); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
import { Address } from '@ant-design/web3'; | ||
import { Space } from 'antd'; | ||
import { formatAddress } from '../../utils'; | ||
|
||
const App: React.FC = () => { | ||
return ( | ||
<Space direction="vertical"> | ||
<div> | ||
Default format: <Address address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'} format /> | ||
</div> | ||
<div> | ||
Custom format:{' '} | ||
<Address | ||
address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'} | ||
format={(input) => { | ||
return formatAddress(input, 5); | ||
}} | ||
/> | ||
</div> | ||
</Space> | ||
); | ||
}; | ||
|
||
export default App; |
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
Oops, something went wrong.