|
| 1 | +import { act, render, screen } from '@testing-library/react'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { largeDiscOutput } from '../../test/fixtures/generateLargeDiscOutput'; |
| 5 | +import { type Value, ValueType } from '../alloy-syntax-js/types'; |
| 6 | +import AsyncStringifiedValue from './AsyncStringifiedValue'; |
| 7 | + |
| 8 | +describe('AsyncStringifiedValue', () => { |
| 9 | + describe('simple values render synchronously', () => { |
| 10 | + it('renders number values immediately', () => { |
| 11 | + const numberValue: Value = { type: ValueType.NUMBER, value: 42 }; |
| 12 | + |
| 13 | + render(<AsyncStringifiedValue value={numberValue} />); |
| 14 | + |
| 15 | + expect(screen.getByText('42')).toBeInTheDocument(); |
| 16 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('renders boolean values immediately', () => { |
| 20 | + const boolValue: Value = { type: ValueType.BOOL, value: true }; |
| 21 | + |
| 22 | + render(<AsyncStringifiedValue value={boolValue} />); |
| 23 | + |
| 24 | + expect(screen.getByText('true')).toBeInTheDocument(); |
| 25 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('renders null values immediately', () => { |
| 29 | + const nullValue: Value = { type: ValueType.NULL }; |
| 30 | + |
| 31 | + render(<AsyncStringifiedValue value={nullValue} />); |
| 32 | + |
| 33 | + expect(screen.getByText('null')).toBeInTheDocument(); |
| 34 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('complex values render asynchronously with size check', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.useFakeTimers(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + vi.useRealTimers(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('shows nothing initially for strings (delayed loading indicator)', () => { |
| 48 | + const stringValue: Value = { type: ValueType.STRING, value: 'hello world' }; |
| 49 | + |
| 50 | + const { container } = render(<AsyncStringifiedValue value={stringValue} />); |
| 51 | + |
| 52 | + // During the delay threshold, nothing is shown (avoids flickering for fast ops) |
| 53 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 54 | + expect(container.textContent).toBe(''); |
| 55 | + }); |
| 56 | + |
| 57 | + it('skips loading state entirely when processing completes before delay threshold', async () => { |
| 58 | + const stringValue: Value = { type: ValueType.STRING, value: 'hello world' }; |
| 59 | + |
| 60 | + render(<AsyncStringifiedValue value={stringValue} />); |
| 61 | + |
| 62 | + // Run all timers - for fast operations, loading state is never shown |
| 63 | + await act(async () => { |
| 64 | + vi.runAllTimers(); |
| 65 | + }); |
| 66 | + |
| 67 | + // The result should appear without ever showing "Loading..." |
| 68 | + expect(screen.getByText('"hello world"')).toBeInTheDocument(); |
| 69 | + // Verify loading was never shown (it was skipped because processing was fast) |
| 70 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('renders small strings after processing', async () => { |
| 74 | + const stringValue: Value = { type: ValueType.STRING, value: 'hello world' }; |
| 75 | + |
| 76 | + render(<AsyncStringifiedValue value={stringValue} />); |
| 77 | + |
| 78 | + await act(async () => { |
| 79 | + vi.runAllTimers(); |
| 80 | + }); |
| 81 | + |
| 82 | + expect(screen.getByText('"hello world"')).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('shows download link for large strings', async () => { |
| 86 | + const largeString = 'x'.repeat(3000); |
| 87 | + const stringValue: Value = { type: ValueType.STRING, value: largeString }; |
| 88 | + |
| 89 | + render(<AsyncStringifiedValue value={stringValue} maxLength={100} />); |
| 90 | + |
| 91 | + await act(async () => { |
| 92 | + vi.runAllTimers(); |
| 93 | + }); |
| 94 | + |
| 95 | + const downloadButton = screen.getByRole('button', { name: 'Download the value contents' }); |
| 96 | + expect(downloadButton).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('shows nothing initially for arrays (delayed loading indicator)', () => { |
| 100 | + const arrayValue: Value = { |
| 101 | + type: ValueType.ARRAY, |
| 102 | + value: [{ type: ValueType.NUMBER, value: 1 }], |
| 103 | + }; |
| 104 | + |
| 105 | + const { container } = render(<AsyncStringifiedValue value={arrayValue} />); |
| 106 | + |
| 107 | + // During the delay threshold, nothing is shown (avoids flickering for fast ops) |
| 108 | + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); |
| 109 | + expect(container.textContent).toBe(''); |
| 110 | + }); |
| 111 | + |
| 112 | + it('renders small arrays after processing', async () => { |
| 113 | + const arrayValue: Value = { |
| 114 | + type: ValueType.ARRAY, |
| 115 | + value: [ |
| 116 | + { type: ValueType.NUMBER, value: 1 }, |
| 117 | + { type: ValueType.NUMBER, value: 2 }, |
| 118 | + ], |
| 119 | + }; |
| 120 | + |
| 121 | + render(<AsyncStringifiedValue value={arrayValue} />); |
| 122 | + |
| 123 | + // Run all timers and frames |
| 124 | + await act(async () => { |
| 125 | + vi.runAllTimers(); |
| 126 | + }); |
| 127 | + |
| 128 | + // Small array should render successfully |
| 129 | + expect(screen.getByText('[1, 2]')).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('shows download link for values exceeding maxLength', async () => { |
| 133 | + // The large value is in exports[0].value (the targets array) |
| 134 | + const largeValue = largeDiscOutput.exports[0].value as Value; |
| 135 | + |
| 136 | + // Use a small maxLength to ensure it exceeds |
| 137 | + render(<AsyncStringifiedValue value={largeValue} maxLength={100} />); |
| 138 | + |
| 139 | + await act(async () => { |
| 140 | + vi.runAllTimers(); |
| 141 | + }); |
| 142 | + |
| 143 | + // Should show download link instead of error message |
| 144 | + const downloadButton = screen.getByRole('button', { name: 'Download the value contents' }); |
| 145 | + expect(downloadButton).toBeInTheDocument(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('shows download link with default 50000 char limit', async () => { |
| 149 | + const largeValue = largeDiscOutput.exports[0].value as Value; |
| 150 | + |
| 151 | + render(<AsyncStringifiedValue value={largeValue} />); |
| 152 | + |
| 153 | + await act(async () => { |
| 154 | + vi.runAllTimers(); |
| 155 | + }); |
| 156 | + |
| 157 | + // Should show download link (fixture is much larger than 50000 chars) |
| 158 | + const downloadButton = screen.getByRole('button', { name: 'Download the value contents' }); |
| 159 | + expect(downloadButton).toBeInTheDocument(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('cleans up properly when unmounted during processing', async () => { |
| 163 | + const largeValue = largeDiscOutput.exports[0].value as Value; |
| 164 | + |
| 165 | + const { unmount } = render(<AsyncStringifiedValue value={largeValue} />); |
| 166 | + |
| 167 | + // Unmount while still processing (before loading or result appears) |
| 168 | + unmount(); |
| 169 | + |
| 170 | + // Run all timers - should not throw any errors |
| 171 | + await act(async () => { |
| 172 | + vi.runAllTimers(); |
| 173 | + }); |
| 174 | + |
| 175 | + expect(true).toBe(true); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments