|
| 1 | +import React, { ReactNode } from 'react'; |
| 2 | +import { render, screen, fireEvent, act, waitFor } from '@testing-library/react'; |
| 3 | +import CopyForLLM from './CopyForLLM'; |
| 4 | + |
| 5 | +const mockUseLayoutContext = jest.fn(() => ({ |
| 6 | + activePage: { |
| 7 | + language: 'javascript', |
| 8 | + languages: ['javascript'], |
| 9 | + product: 'pubsub', |
| 10 | + page: { |
| 11 | + name: 'Test Page', |
| 12 | + link: '/docs/test-page', |
| 13 | + }, |
| 14 | + tree: [], |
| 15 | + template: 'mdx' as const, |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('src/contexts/layout-context', () => ({ |
| 20 | + useLayoutContext: () => mockUseLayoutContext(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('@reach/router', () => ({ |
| 24 | + useLocation: () => ({ pathname: '/docs/test-page' }), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('@ably/ui/core/Icon', () => ({ |
| 28 | + __esModule: true, |
| 29 | + default: ({ name }: { name: string }) => <span data-testid={`icon-${name}`}>{name}</span>, |
| 30 | +})); |
| 31 | + |
| 32 | +jest.mock('@ably/ui/core/insights', () => ({ |
| 33 | + track: jest.fn(), |
| 34 | +})); |
| 35 | + |
| 36 | +// Mock Radix DropdownMenu to render content directly |
| 37 | +jest.mock('@radix-ui/react-dropdown-menu', () => ({ |
| 38 | + Root: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 39 | + Trigger: ({ children }: { children: ReactNode; asChild?: boolean }) => <div>{children}</div>, |
| 40 | + Portal: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 41 | + Content: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 42 | + Item: ({ |
| 43 | + children, |
| 44 | + onSelect, |
| 45 | + ...props |
| 46 | + }: { |
| 47 | + children: ReactNode; |
| 48 | + onSelect?: (e: Event) => void; |
| 49 | + asChild?: boolean; |
| 50 | + }) => |
| 51 | + props.asChild ? ( |
| 52 | + <>{children}</> |
| 53 | + ) : ( |
| 54 | + <div onClick={() => onSelect?.({ preventDefault: () => undefined } as unknown as Event)}>{children}</div> |
| 55 | + ), |
| 56 | + Separator: () => <hr />, |
| 57 | +})); |
| 58 | + |
| 59 | +describe('CopyForLLM', () => { |
| 60 | + beforeEach(() => { |
| 61 | + jest.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + jest.useRealTimers(); |
| 66 | + jest.restoreAllMocks(); |
| 67 | + jest.clearAllTimers(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('renders the dropdown trigger button', () => { |
| 71 | + global.fetch = jest.fn(() => Promise.resolve({ ok: false, status: 404 } as Response)); |
| 72 | + |
| 73 | + render(<CopyForLLM />); |
| 74 | + expect(screen.getByText('Copy for LLM')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders LLM links for ChatGPT and Claude', () => { |
| 78 | + global.fetch = jest.fn(() => Promise.resolve({ ok: false, status: 404 } as Response)); |
| 79 | + |
| 80 | + render(<CopyForLLM />); |
| 81 | + expect(screen.getByText('Open in ChatGPT')).toBeInTheDocument(); |
| 82 | + expect(screen.getByText('Open in Claude')).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('shows markdown items when content is available', async () => { |
| 86 | + const mockMarkdown = '# Test content'; |
| 87 | + |
| 88 | + global.fetch = jest.fn(() => |
| 89 | + Promise.resolve({ |
| 90 | + ok: true, |
| 91 | + headers: { |
| 92 | + get: (name: string) => (name === 'Content-Type' ? 'text/markdown' : null), |
| 93 | + }, |
| 94 | + text: () => Promise.resolve(mockMarkdown), |
| 95 | + } as Response), |
| 96 | + ); |
| 97 | + |
| 98 | + const mockWriteText = jest.fn(); |
| 99 | + Object.assign(navigator, { clipboard: { writeText: mockWriteText } }); |
| 100 | + |
| 101 | + render(<CopyForLLM />); |
| 102 | + |
| 103 | + // Wait for the markdown to be fetched and state to update (button becomes enabled) |
| 104 | + const copyButton = await screen.findByText('Copy for LLM'); |
| 105 | + await waitFor(() => { |
| 106 | + expect(copyButton.closest('button')).not.toBeDisabled(); |
| 107 | + }); |
| 108 | + |
| 109 | + jest.useFakeTimers(); |
| 110 | + |
| 111 | + // Click copy via the dropdown item |
| 112 | + const copyItem = screen.getByText('Copy as markdown').closest('div'); |
| 113 | + if (copyItem) { |
| 114 | + act(() => { |
| 115 | + fireEvent.click(copyItem); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + expect(mockWriteText).toHaveBeenCalledWith(mockMarkdown); |
| 120 | + |
| 121 | + act(() => { |
| 122 | + jest.runOnlyPendingTimers(); |
| 123 | + }); |
| 124 | + jest.useRealTimers(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('disables copy button when fetch fails', async () => { |
| 128 | + global.fetch = jest.fn(() => Promise.resolve({ ok: false, status: 404 } as Response)); |
| 129 | + |
| 130 | + render(<CopyForLLM />); |
| 131 | + |
| 132 | + await new Promise<void>((resolve) => setTimeout(resolve, 50)); |
| 133 | + |
| 134 | + const copyButton = screen.getByText('Copy for LLM').closest('button'); |
| 135 | + expect(copyButton).toBeDisabled(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments