|
| 1 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 2 | +import Playground from '@/components/playground/index'; |
| 3 | +import { executeJavaScript } from '@/lib/playground/javascript-executor'; |
| 4 | + |
| 5 | +// Mocks |
| 6 | +jest.mock('@/components/playground/code-editor-wrapper', () => ({ |
| 7 | + CodeEditorWrapper: ({ value, onChange, onRun }: any) => ( |
| 8 | + <div data-testid="code-editor"> |
| 9 | + <textarea |
| 10 | + data-testid="mock-editor-textarea" |
| 11 | + value={value} |
| 12 | + onChange={(e) => onChange(e.target.value)} |
| 13 | + /> |
| 14 | + <button data-testid="editor-run-shortcut" onClick={onRun}>Run Shortcut</button> |
| 15 | + </div> |
| 16 | + ), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('@/lib/playground/javascript-executor', () => ({ |
| 20 | + executeJavaScript: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('@/hooks/use-media-query', () => ({ |
| 24 | + useMediaQuery: () => false, |
| 25 | +})); |
| 26 | + |
| 27 | +// Mock ResizeObserver for react-resizable-panels |
| 28 | +global.ResizeObserver = jest.fn().mockImplementation(() => ({ |
| 29 | + observe: jest.fn(), |
| 30 | + unobserve: jest.fn(), |
| 31 | + disconnect: jest.fn(), |
| 32 | +})); |
| 33 | + |
| 34 | +describe('Playground Flow E2E (Simulated)', () => { |
| 35 | + beforeEach(() => { |
| 36 | + jest.clearAllMocks(); |
| 37 | + (executeJavaScript as jest.Mock).mockResolvedValue({ |
| 38 | + success: true, |
| 39 | + output: 'Hello Test Flow', |
| 40 | + executionTime: 50, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should run code and display output when Run button is clicked', async () => { |
| 45 | + render(<Playground />); |
| 46 | + |
| 47 | + // Check if key elements are present |
| 48 | + expect(screen.getByText(/Run/i)).toBeInTheDocument(); |
| 49 | + expect(screen.getByTestId('code-editor')).toBeInTheDocument(); |
| 50 | + |
| 51 | + // Find Run button (it might be in nav) |
| 52 | + // Assuming the button text is "Run" inside PlaygroundNav |
| 53 | + const runBtn = screen.getByRole('button', { name: /run/i }); |
| 54 | + |
| 55 | + // Click Run |
| 56 | + fireEvent.click(runBtn); |
| 57 | + |
| 58 | + // Verify loading state (optional, might be too fast) |
| 59 | + |
| 60 | + // Verify execution was called |
| 61 | + await waitFor(() => { |
| 62 | + expect(executeJavaScript).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + // Verify output is displayed |
| 66 | + // The OutputPanel likely displays the text. |
| 67 | + await waitFor(() => { |
| 68 | + expect(screen.getByText('Hello Test Flow')).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle runtime errors', async () => { |
| 73 | + (executeJavaScript as jest.Mock).mockResolvedValue({ |
| 74 | + success: false, |
| 75 | + error: 'Runtime Error: Boom', |
| 76 | + executionTime: 10, |
| 77 | + }); |
| 78 | + |
| 79 | + render(<Playground />); |
| 80 | + |
| 81 | + const runBtn = screen.getByRole('button', { name: /run/i }); |
| 82 | + fireEvent.click(runBtn); |
| 83 | + |
| 84 | + await waitFor(() => { |
| 85 | + expect(screen.getByText('Runtime Error: Boom')).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments