|
| 1 | +import { describe, it, vi, expect, beforeEach } from 'vitest' |
| 2 | +import { screen, waitFor } from '@testing-library/react' |
| 3 | +import { customRender as render } from '../../test/test-utils-helpers' |
| 4 | +import { MyTemplates } from '../MyTemplates' |
| 5 | +import * as wagmi from 'wagmi' |
| 6 | +import * as api from '../../services/api' |
| 7 | +import type { Mock } from 'vitest' |
| 8 | + |
| 9 | +// Mock wagmi hooks |
| 10 | +vi.mock('wagmi', () => ({ |
| 11 | + useAccount: vi.fn(), |
| 12 | + useSignMessage: vi.fn(), |
| 13 | + WagmiProvider: ({ children }: { children: React.ReactNode }) => children, |
| 14 | +})) |
| 15 | + |
| 16 | +// Mock API services |
| 17 | +vi.mock('../../services/api', () => ({ |
| 18 | + checkUserRegistration: vi.fn(), |
| 19 | + getMyTemplates: vi.fn(), |
| 20 | + deleteTemplate: vi.fn(), |
| 21 | + createTemplate: vi.fn(), |
| 22 | +})) |
| 23 | + |
| 24 | +describe('MyTemplates Component', () => { |
| 25 | + const mockAddress = '0x123...' |
| 26 | + const mockSignMessage = vi.fn() |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + |
| 31 | + // Default mock implementations |
| 32 | + ;(wagmi.useAccount as Mock).mockReturnValue({ |
| 33 | + address: mockAddress, |
| 34 | + isConnected: true, |
| 35 | + }) |
| 36 | + ;(wagmi.useSignMessage as Mock).mockReturnValue({ |
| 37 | + signMessageAsync: mockSignMessage, |
| 38 | + }) |
| 39 | + ;(api.checkUserRegistration as Mock).mockResolvedValue(true) |
| 40 | + ;(api.getMyTemplates as Mock).mockResolvedValue([]) |
| 41 | + }) |
| 42 | + |
| 43 | + it('displays templates when loaded', async () => { |
| 44 | + const mockTemplates = [ |
| 45 | + { |
| 46 | + id: 1, |
| 47 | + title: 'Test Template', |
| 48 | + description: 'Test Description', |
| 49 | + url: 'https://example.com', |
| 50 | + data: '{}', |
| 51 | + created_at: new Date().toISOString(), |
| 52 | + owner_address: mockAddress, |
| 53 | + updated_at: new Date().toISOString(), |
| 54 | + }, |
| 55 | + ] |
| 56 | + |
| 57 | + ;(api.getMyTemplates as Mock).mockResolvedValue(mockTemplates) |
| 58 | + |
| 59 | + render(<MyTemplates />) |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + expect(screen.getByText('Test Template')).toBeInTheDocument() |
| 63 | + expect(screen.getByText('Test Description')).toBeInTheDocument() |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('disables New Template button when user is not registered', async () => { |
| 68 | + // Mock user as not registered |
| 69 | + ;(api.checkUserRegistration as Mock).mockResolvedValue(false) |
| 70 | + |
| 71 | + render(<MyTemplates />) |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + const newTemplateButton = screen.getByRole('button', { name: /New Template/i }) |
| 75 | + expect(newTemplateButton).toBeDisabled() |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + it('enables New Template button when user is registered', async () => { |
| 80 | + // Mock user as registered |
| 81 | + ;(api.checkUserRegistration as Mock).mockResolvedValue(true) |
| 82 | + |
| 83 | + render(<MyTemplates />) |
| 84 | + |
| 85 | + await waitFor(() => { |
| 86 | + const newTemplateButton = screen.getByRole('button', { name: /New Template/i }) |
| 87 | + expect(newTemplateButton).not.toBeDisabled() |
| 88 | + }) |
| 89 | + }) |
| 90 | +}) |
0 commit comments