|
| 1 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 2 | +import { MemoryRouter } from 'react-router-dom'; |
| 3 | +import { vi } from 'vitest'; |
| 4 | +import { NestedATAForm } from './NestedATAForm'; |
| 5 | + |
| 6 | +function renderForm(onSubmit = vi.fn()) { |
| 7 | + const result = render( |
| 8 | + <MemoryRouter> |
| 9 | + <NestedATAForm onSubmit={onSubmit} /> |
| 10 | + </MemoryRouter> |
| 11 | + ); |
| 12 | + const q = (name: string) => |
| 13 | + result.container.querySelector<HTMLElement>(`[name="${name}"]`)!; |
| 14 | + return { ...result, q }; |
| 15 | +} |
| 16 | + |
| 17 | +function fillValidForm(q: (name: string) => HTMLElement) { |
| 18 | + fireEvent.change(q('userKey'), { |
| 19 | + target: { value: '{"iv":"abc","ct":"encryptedUserKey"}' }, |
| 20 | + }); |
| 21 | + fireEvent.change(q('backupKey'), { |
| 22 | + target: { value: '{"iv":"def","ct":"encryptedBackupKey"}' }, |
| 23 | + }); |
| 24 | + fireEvent.change(q('bitgoKey'), { |
| 25 | + target: { value: 'xpubBitGoKey' }, |
| 26 | + }); |
| 27 | + fireEvent.change(q('walletPassphrase'), { |
| 28 | + target: { value: 'test-passphrase' }, |
| 29 | + }); |
| 30 | + fireEvent.change(q('recoveryDestination'), { |
| 31 | + target: { value: '7YbcLmVorrH7KCKMj38rFidsruisWi2CmvCTs4cygf8K' }, |
| 32 | + }); |
| 33 | + fireEvent.change(q('nestedAtaAddress'), { |
| 34 | + target: { value: 'FGuZSBhtreqSUsE86xokyjKz2i8VBtJzy6uMXXKyGHug' }, |
| 35 | + }); |
| 36 | + fireEvent.change(q('ownerAtaAddress'), { |
| 37 | + target: { value: 'Zfm98ZpVafydhFTYcsY6bHgubhB4cFgWFvbdEJxYhTA' }, |
| 38 | + }); |
| 39 | + fireEvent.change(q('tokenMintAddress'), { |
| 40 | + target: { value: 'ZBCNpuD7YMXzTHB2fhGkGi78MNsHGLRXUhRewNRm9RU' }, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +describe('NestedATAForm', () => { |
| 45 | + it('renders all required fields', () => { |
| 46 | + const { q } = renderForm(); |
| 47 | + |
| 48 | + expect(q('userKey')).not.toBeNull(); |
| 49 | + expect(q('backupKey')).not.toBeNull(); |
| 50 | + expect(q('bitgoKey')).not.toBeNull(); |
| 51 | + expect(q('walletPassphrase')).not.toBeNull(); |
| 52 | + expect(q('recoveryDestination')).not.toBeNull(); |
| 53 | + expect(q('nestedAtaAddress')).not.toBeNull(); |
| 54 | + expect(q('ownerAtaAddress')).not.toBeNull(); |
| 55 | + expect(q('tokenMintAddress')).not.toBeNull(); |
| 56 | + expect(q('apiKey')).not.toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders submit button with correct label', () => { |
| 60 | + renderForm(); |
| 61 | + expect(screen.getByRole('button', { name: /recover tokens/i })).not.toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('calls onSubmit with correct values when form is valid', async () => { |
| 65 | + const onSubmit = vi.fn(); |
| 66 | + const { q } = renderForm(onSubmit); |
| 67 | + |
| 68 | + fillValidForm(q); |
| 69 | + fireEvent.click(screen.getByRole('button', { name: /recover tokens/i })); |
| 70 | + |
| 71 | + await waitFor(() => { |
| 72 | + expect(onSubmit).toHaveBeenCalledOnce(); |
| 73 | + const [values] = onSubmit.mock.calls[0] as [Record<string, string>][]; |
| 74 | + expect(values.nestedAtaAddress).toBe('FGuZSBhtreqSUsE86xokyjKz2i8VBtJzy6uMXXKyGHug'); |
| 75 | + expect(values.ownerAtaAddress).toBe('Zfm98ZpVafydhFTYcsY6bHgubhB4cFgWFvbdEJxYhTA'); |
| 76 | + expect(values.tokenMintAddress).toBe('ZBCNpuD7YMXzTHB2fhGkGi78MNsHGLRXUhRewNRm9RU'); |
| 77 | + expect(values.recoveryDestination).toBe('7YbcLmVorrH7KCKMj38rFidsruisWi2CmvCTs4cygf8K'); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('does not call onSubmit when required fields are empty', async () => { |
| 82 | + const onSubmit = vi.fn(); |
| 83 | + renderForm(onSubmit); |
| 84 | + |
| 85 | + fireEvent.click(screen.getByRole('button', { name: /recover tokens/i })); |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('rejects API key that looks like a URL', async () => { |
| 93 | + const onSubmit = vi.fn(); |
| 94 | + const { q } = renderForm(onSubmit); |
| 95 | + |
| 96 | + fillValidForm(q); |
| 97 | + fireEvent.change(q('apiKey'), { |
| 98 | + target: { value: 'https://eth-mainnet.g.alchemy.com/v2/somekey' }, |
| 99 | + }); |
| 100 | + fireEvent.click(screen.getByRole('button', { name: /recover tokens/i })); |
| 101 | + |
| 102 | + await waitFor(() => { |
| 103 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 104 | + expect(screen.getByText(/api key should not be a url/i)).not.toBeNull(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('accepts a valid optional API key', async () => { |
| 109 | + const onSubmit = vi.fn(); |
| 110 | + const { q } = renderForm(onSubmit); |
| 111 | + |
| 112 | + fillValidForm(q); |
| 113 | + fireEvent.change(q('apiKey'), { |
| 114 | + target: { value: 'validApiKey123' }, |
| 115 | + }); |
| 116 | + fireEvent.click(screen.getByRole('button', { name: /recover tokens/i })); |
| 117 | + |
| 118 | + await waitFor(() => { |
| 119 | + expect(onSubmit).toHaveBeenCalledOnce(); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments