|
1 | 1 | import { executeComponentTest } from '@/tests/test-component' |
2 | | -import { render, waitFor } from '@/tests/testing-library' |
| 2 | +import { fireEvent, render, waitFor } from '@/tests/testing-library' |
3 | 3 | import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
4 | 4 | import { LayoutPage } from '@/features/layout/pages/layout-page' |
5 | | -import { connectWalletLabel } from '@/features/wallet/components/connect-wallet-button' |
6 | | -import { useWallet } from '@txnlab/use-wallet' |
| 5 | +import { connectWalletLabel, selectAccountLabel, disconnectWalletLabel } from '@/features/wallet/components/connect-wallet-button' |
| 6 | +import { PROVIDER_ID, Provider, useWallet } from '@txnlab/use-wallet' |
7 | 7 | import { Event as TauriEvent, listen } from '@tauri-apps/api/event' |
8 | 8 | import { networkConfigAtom, settingsStore } from '@/features/settings/data' |
9 | 9 | import { useNavigate } from 'react-router-dom' |
@@ -61,15 +61,94 @@ describe('when rendering the layout page', () => { |
61 | 61 |
|
62 | 62 | const walletAddressText = component.getByText('CGRS...LQ5Q') |
63 | 63 | expect(walletAddressText).toBeTruthy() |
64 | | - |
65 | | - const linkElement = component.getByRole('link', { name: /CGRS...LQ5Q/i }) |
66 | | - expect(linkElement.getAttribute('href')).toBe('/explore/account/CGRSYAYF2M5HNH6KYY6RPLYANVZ5ZQO4OTC3M3YPI4D6JFBXZGRUSVLQ5Q') |
67 | 64 | }) |
68 | 65 | } |
69 | 66 | ) |
70 | 67 | }) |
71 | | - }) |
| 68 | + describe('and there is more than one account', () => { |
| 69 | + it('the account switcher should be shown', async () => { |
| 70 | + const original = await vi.importActual<{ useWallet: () => ReturnType<typeof useWallet> }>('@txnlab/use-wallet') |
| 71 | + vi.mocked(useWallet).mockImplementation(() => { |
| 72 | + return { |
| 73 | + ...original.useWallet(), |
| 74 | + activeAddress: 'CGRSYAYF2M5HNH6KYY6RPLYANVZ5ZQO4OTC3M3YPI4D6JFBXZGRUSVLQ5Q', |
| 75 | + status: 'active', |
| 76 | + isActive: true, |
| 77 | + isReady: true, |
| 78 | + connectedActiveAccounts: [ |
| 79 | + { |
| 80 | + providerId: PROVIDER_ID.PERA, |
| 81 | + address: 'CGRSYAYF2M5HNH6KYY6RPLYANVZ5ZQO4OTC3M3YPI4D6JFBXZGRUSVLQ5Q', |
| 82 | + name: 'Account 1', |
| 83 | + }, |
| 84 | + { |
| 85 | + providerId: PROVIDER_ID.PERA, |
| 86 | + address: 'SOMEOTHERADDRESS', |
| 87 | + name: 'Account 2', |
| 88 | + }, |
| 89 | + ], |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + await executeComponentTest( |
| 94 | + () => render(<LayoutPage />, undefined), |
| 95 | + async (component) => { |
| 96 | + await waitFor(async () => { |
| 97 | + const selectAccountTrigger = component.getByTitle('CGRSYAYF2M5HNH6KYY6RPLYANVZ5ZQO4OTC3M3YPI4D6JFBXZGRUSVLQ5Q') |
| 98 | + expect(selectAccountTrigger).toBeTruthy() |
| 99 | + fireEvent.click(selectAccountTrigger) |
| 100 | + |
| 101 | + const selectElement = component.getByLabelText(selectAccountLabel) |
| 102 | + expect(selectElement).toBeTruthy() |
| 103 | + }) |
| 104 | + } |
| 105 | + ) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe('and the user disconnects the wallet', () => { |
| 110 | + it('the wallet should be disconnected', async () => { |
| 111 | + const original = await vi.importActual<{ useWallet: () => ReturnType<typeof useWallet> }>('@txnlab/use-wallet') |
| 112 | + const disconnect = vi.fn() |
| 113 | + vi.mocked(useWallet).mockImplementation(() => { |
| 114 | + return { |
| 115 | + ...original.useWallet(), |
| 116 | + activeAddress: 'CGRSYAYF2M5HNH6KYY6RPLYANVZ5ZQO4OTC3M3YPI4D6JFBXZGRUSVLQ5Q', |
| 117 | + status: 'active', |
| 118 | + isActive: true, |
| 119 | + isReady: true, |
| 120 | + providers: [ |
| 121 | + { |
| 122 | + disconnect, |
| 123 | + isActive: true, |
| 124 | + metadata: { |
| 125 | + id: PROVIDER_ID.PERA, |
| 126 | + name: 'Pera', |
| 127 | + icon: 'someicon.png', |
| 128 | + isWalletConnect: true, |
| 129 | + }, |
| 130 | + } as unknown as Provider, |
| 131 | + ], |
| 132 | + } |
| 133 | + }) |
| 134 | + await executeComponentTest( |
| 135 | + () => render(<LayoutPage />, undefined), |
| 136 | + async (component) => { |
| 137 | + await waitFor(async () => { |
| 138 | + const selectAccountTrigger = component.getByTitle('CGRSYAYF2M5HNH6KYY6RPLYANVZ5ZQO4OTC3M3YPI4D6JFBXZGRUSVLQ5Q') |
| 139 | + expect(selectAccountTrigger).toBeTruthy() |
| 140 | + fireEvent.click(selectAccountTrigger) |
72 | 141 |
|
| 142 | + const disconnectButton = component.getByLabelText(disconnectWalletLabel) |
| 143 | + expect(disconnectButton).toBeTruthy() |
| 144 | + fireEvent.click(disconnectButton) |
| 145 | + expect(disconnect).toHaveBeenCalled() |
| 146 | + }) |
| 147 | + } |
| 148 | + ) |
| 149 | + }) |
| 150 | + }) |
| 151 | + }) |
73 | 152 | describe('and the user opens a deep link to mainnet', () => { |
74 | 153 | beforeAll(() => { |
75 | 154 | window.__TAURI__ = {} |
|
0 commit comments