-
Notifications
You must be signed in to change notification settings - Fork 1.5k
chore: add scaffold-ui tests #2913
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df1624f
chore: add scaffold-utils tests
tomiir ecb8e05
chore: add coverage to test command
tomiir 1447de0
Merge branch 'main' into chore/utils-tests
tomiir 6175e70
Merge branch 'main' of github.com:WalletConnect/web3modal into chore/…
tomiir 85bae5a
chore: CR
tomiir 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -25,16 +25,17 @@ | |
"build": "tsc --build", | ||
"watch": "tsc --watch", | ||
"typecheck": "tsc --noEmit", | ||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx" | ||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx", | ||
"test": "vitest run --coverage.enabled=true -- coverage.reporter=json --coverage.reporter=json-summary --coverage.reportOnFailure=true" | ||
}, | ||
"dependencies": { | ||
"@reown/appkit-common": "workspace:*", | ||
"@reown/appkit-utils": "workspace:*", | ||
"@reown/appkit-core": "workspace:*", | ||
"@reown/appkit-ui": "workspace:*", | ||
"lit": "3.1.0", | ||
"@reown/appkit-siwe": "workspace:*", | ||
"@reown/appkit-wallet": "workspace:*" | ||
"@reown/appkit-ui": "workspace:*", | ||
"@reown/appkit-utils": "workspace:*", | ||
"@reown/appkit-wallet": "workspace:*", | ||
"lit": "3.1.0" | ||
}, | ||
"author": "Reown <[email protected]> (https://reown.com)", | ||
"license": "Apache-2.0", | ||
|
@@ -45,5 +46,8 @@ | |
}, | ||
"bugs": { | ||
"url": "https://github.com/WalletConnect/web3modal/issues" | ||
}, | ||
"devDependencies": { | ||
"@vitest/coverage-v8": "2" | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,237 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
import { WalletUtil } from '../src/utils/WalletUtil' | ||
import { | ||
ConnectorController, | ||
CoreHelperUtil, | ||
OptionsController, | ||
StorageUtil | ||
} from '@reown/appkit-core' | ||
import type { WcWallet } from '@reown/appkit-core' | ||
|
||
// Connectors | ||
const mockMetamaskConnector = { | ||
info: { rdns: 'io.metamask' }, | ||
id: '1', | ||
explorerId: '1', | ||
chain: 'eip155' as const, | ||
type: 'ANNOUNCED' as const | ||
} | ||
|
||
const mockRainbowConnector = { | ||
info: { rdns: 'io.rainbow' }, | ||
id: '2', | ||
explorerId: '2', | ||
chain: 'eip155' as const, | ||
type: 'ANNOUNCED' as const | ||
} | ||
const mockMetamaskMobileConnector = { | ||
info: { rdns: 'io.metamask.mobile' }, | ||
id: '4', | ||
explorerId: '4', | ||
chain: 'eip155' as const, | ||
type: 'ANNOUNCED' as const | ||
} | ||
const mockCoinbaseconnector = { | ||
info: { rdns: 'io.coinbase' }, | ||
id: '5', | ||
explorerId: '5', | ||
chain: 'eip155' as const, | ||
type: 'EXTERNAL' as const | ||
} | ||
|
||
// Wallets | ||
const mockMetamaskWallet: WcWallet = { id: '1', name: 'Wallet 1', rdns: 'io.metamask' } | ||
const mockRainbowWallet: WcWallet = { id: '2', name: 'Wallet 2', rdns: 'io.rainbow' } | ||
const mockTrustWallet: WcWallet = { id: '3', name: 'Wallet 3', rdns: 'io.trustwallet' } | ||
const mockCoinbaseWallet: WcWallet = { id: '5', name: 'Wallet 5', rdns: 'io.coinbase' } | ||
|
||
describe('WalletUtil', () => { | ||
const mockWallets: WcWallet[] = [mockMetamaskWallet, mockRainbowWallet, mockTrustWallet] | ||
|
||
beforeEach(() => { | ||
// Reset all mocks before each test | ||
vi.restoreAllMocks() | ||
OptionsController.state.enableEIP6963 = true | ||
}) | ||
|
||
describe('filterOutDuplicatesByRDNS', () => { | ||
it('should filter out wallets with RDNS from connectors and recent wallets', () => { | ||
const mockConnectors = [mockMetamaskConnector, mockRainbowConnector, mockCoinbaseconnector] | ||
const mockRecentWallets = [mockTrustWallet] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue(mockRecentWallets) | ||
vi.spyOn(CoreHelperUtil, 'isMobile').mockReturnValue(false) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicatesByRDNS(mockWallets) | ||
|
||
expect(filteredWallets).toEqual([]) // All RDNS wallets should be filtered out | ||
}) | ||
|
||
it('should replace "io.metamask.mobile" with "io.metamask" on mobile', () => { | ||
const mockConnectors = [mockMetamaskMobileConnector] | ||
const mockRecentWallets = [] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue(mockRecentWallets) | ||
vi.spyOn(CoreHelperUtil, 'isMobile').mockReturnValue(true) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicatesByRDNS(mockWallets) | ||
|
||
expect(filteredWallets).toEqual(mockWallets.filter(wallet => wallet.rdns !== 'io.metamask')) | ||
}) | ||
|
||
it('should return all wallets if no connectors or recent wallets have RDNS', () => { | ||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue([]) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue([]) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicatesByRDNS(mockWallets) | ||
|
||
expect(filteredWallets).toEqual(mockWallets) | ||
}) | ||
}) | ||
|
||
describe('filterOutDuplicatesByIds', () => { | ||
it('should filter out wallets with IDs from connectors and recent wallets', () => { | ||
const mockConnectors = [mockMetamaskConnector, mockRainbowConnector] | ||
const mockRecentWallets = [mockTrustWallet] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue(mockRecentWallets) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicatesByIds(mockWallets) | ||
|
||
expect(filteredWallets).toEqual([]) // All IDs are filtered out. MM and Rainbow from connectors, TW from recent | ||
}) | ||
|
||
it('should not filter if connector is not Injected or Announced', () => { | ||
const mockConnectors = [mockMetamaskConnector, mockRainbowConnector] | ||
const mockRecentWallets = [mockTrustWallet] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue(mockRecentWallets) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicatesByIds([ | ||
...mockWallets, | ||
mockCoinbaseWallet | ||
]) | ||
|
||
expect(filteredWallets).toEqual([mockCoinbaseWallet]) | ||
}) | ||
|
||
it('should return all wallets if no connectors or recent wallets have IDs', () => { | ||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue([]) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue([]) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicatesByIds(mockWallets) | ||
|
||
expect(filteredWallets).toEqual(mockWallets) | ||
}) | ||
}) | ||
|
||
describe('filterOutDuplicateWallets', () => { | ||
it('should filter out wallets with duplicate RDNS and IDs', () => { | ||
const mockConnectors = [mockMetamaskConnector] | ||
const mockRecentWallets = [mockTrustWallet] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue(mockRecentWallets) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicateWallets(mockWallets) | ||
|
||
expect(filteredWallets).toEqual([mockRainbowWallet]) | ||
}) | ||
|
||
it('should return all wallets if no duplicates exist by RDNS or IDs', () => { | ||
const mockConnectors = [] | ||
const mockRecentWallets = [] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
vi.spyOn(StorageUtil, 'getRecentWallets').mockReturnValue(mockRecentWallets) | ||
|
||
const filteredWallets = WalletUtil.filterOutDuplicateWallets(mockWallets) | ||
|
||
expect(filteredWallets).toEqual(mockWallets) | ||
}) | ||
}) | ||
|
||
describe('markWalletsAsInstalled', () => { | ||
const mockWallets: WcWallet[] = [ | ||
{ id: '1', name: 'Wallet 1', rdns: 'io.metamask' }, | ||
{ id: '2', name: 'Wallet 2', rdns: 'io.rainbow' }, | ||
{ id: '3', name: 'Wallet 3', rdns: 'io.trustwallet' } | ||
] | ||
|
||
beforeEach(() => { | ||
// Reset all mocks before each test | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('should mark wallets as installed based on connectors', () => { | ||
const mockConnectors = [mockMetamaskConnector, mockRainbowConnector] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
|
||
const result = WalletUtil.markWalletsAsInstalled(mockWallets) | ||
|
||
expect(result).toEqual([ | ||
{ ...mockMetamaskWallet, installed: true }, | ||
{ ...mockRainbowWallet, installed: true }, | ||
{ ...mockTrustWallet, installed: false } | ||
]) | ||
}) | ||
|
||
it('should return wallets sorted by installed status', () => { | ||
const mockConnectors = [mockRainbowConnector] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
|
||
const result = WalletUtil.markWalletsAsInstalled(mockWallets) | ||
|
||
expect(result).toEqual([ | ||
{ ...mockRainbowWallet, installed: true }, | ||
{ ...mockMetamaskWallet, installed: false }, | ||
{ ...mockTrustWallet, installed: false } | ||
]) | ||
}) | ||
|
||
it('should return wallets with installed false if no matching connectors are found', () => { | ||
const mockConnectors = [ | ||
{ | ||
type: 'ANNOUNCED' as const, | ||
info: { rdns: 'io.someotherwallet' }, | ||
id: '1233', | ||
chain: 'eip155' as const | ||
} | ||
] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
|
||
const result = WalletUtil.markWalletsAsInstalled(mockWallets) | ||
|
||
expect(result).toEqual([ | ||
{ ...mockMetamaskWallet, installed: false }, | ||
{ ...mockRainbowWallet, installed: false }, | ||
{ ...mockTrustWallet, installed: false } | ||
]) | ||
}) | ||
|
||
it('should handle wallets without RDNS', () => { | ||
const walletsWithoutRDNS: WcWallet[] = [ | ||
{ id: '1', name: 'Wallet 1' }, | ||
{ id: '2', name: 'Wallet 2', rdns: 'io.rainbow' } | ||
] | ||
|
||
const mockConnectors = [mockRainbowConnector] | ||
|
||
vi.spyOn(ConnectorController.state, 'connectors', 'get').mockReturnValue(mockConnectors) | ||
|
||
const result = WalletUtil.markWalletsAsInstalled(walletsWithoutRDNS) | ||
|
||
expect(result).toEqual([ | ||
{ id: '2', name: 'Wallet 2', rdns: 'io.rainbow', installed: true }, | ||
{ id: '1', name: 'Wallet 1', installed: false } | ||
]) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.