File size: 6,664 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { renderWithProviders } from '../../../test-utils/render.js';
import { Scrollable } from './Scrollable.js';
import { Text, Box } from 'ink';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import * as ScrollProviderModule from '../../contexts/ScrollProvider.js';
import { act } from 'react';
import { waitFor } from '../../../test-utils/async.js';

vi.mock('../../hooks/useAnimatedScrollbar.js', () => ({
  useAnimatedScrollbar: (
    hasFocus: boolean,
    scrollBy: (delta: number) => void,
  ) => ({
    scrollbarColor: 'white',
    flashScrollbar: vi.fn(),
    scrollByWithAnimation: scrollBy,
  }),
}));

describe('<Scrollable />', () => {
  beforeEach(() => {
    vi.restoreAllMocks();
  });

  it('renders children', async () => {
    const { lastFrame, unmount } = await renderWithProviders(
      <Scrollable hasFocus={false} height={5}>
        <Text>Hello World</Text>
      </Scrollable>,
    );
    expect(lastFrame()).toContain('Hello World');
    unmount();
  });

  it('renders multiple children', async () => {
    const { lastFrame, unmount } = await renderWithProviders(
      <Scrollable hasFocus={false} height={5}>
        <Text>Line 1</Text>
        <Text>Line 2</Text>
        <Text>Line 3</Text>
      </Scrollable>,
    );
    expect(lastFrame()).toContain('Line 1');
    expect(lastFrame()).toContain('Line 2');
    expect(lastFrame()).toContain('Line 3');
    unmount();
  });

  it('matches snapshot', async () => {
    const { lastFrame, unmount } = await renderWithProviders(
      <Scrollable hasFocus={false} height={5}>
        <Text>Line 1</Text>
        <Text>Line 2</Text>
        <Text>Line 3</Text>
      </Scrollable>,
    );
    expect(lastFrame()).toMatchSnapshot();
    unmount();
  });

  it('updates scroll position correctly when scrollBy is called multiple times in the same tick', async () => {
    let capturedEntry: ScrollProviderModule.ScrollableEntry | undefined;
    vi.spyOn(ScrollProviderModule, 'useScrollable').mockImplementation(
      async (entry, isActive) => {
        if (isActive) {
          capturedEntry = entry as ScrollProviderModule.ScrollableEntry;
        }
      },
    );

    const { unmount } = await renderWithProviders(
      <Scrollable hasFocus={true} height={5}>
        <Text>Line 1</Text>
        <Text>Line 2</Text>
        <Text>Line 3</Text>
        <Text>Line 4</Text>
        <Text>Line 5</Text>
        <Text>Line 6</Text>
        <Text>Line 7</Text>
        <Text>Line 8</Text>
        <Text>Line 9</Text>
        <Text>Line 10</Text>
      </Scrollable>,
    );

    expect(capturedEntry).toBeDefined();

    if (!capturedEntry) {
      throw new Error('capturedEntry is undefined');
    }

    // Initial state (starts at top by default)
    expect(capturedEntry.getScrollState().scrollTop).toBe(0);

    // Initial state with scrollToBottom={true}
    unmount();
    const { unmount: unmount2 } = await renderWithProviders(
      <Scrollable hasFocus={true} height={5} scrollToBottom={true}>
        <Text>Line 1</Text>
        <Text>Line 2</Text>
        <Text>Line 3</Text>
        <Text>Line 4</Text>
        <Text>Line 5</Text>
        <Text>Line 6</Text>
        <Text>Line 7</Text>
        <Text>Line 8</Text>
        <Text>Line 9</Text>
        <Text>Line 10</Text>
      </Scrollable>,
    );
    await waitFor(() => {
      expect(capturedEntry?.getScrollState().scrollTop).toBe(5);
    });

    // Call scrollBy multiple times (upwards) in the same tick
    await act(async () => {
      capturedEntry?.scrollBy(-1);
      capturedEntry?.scrollBy(-1);
    });
    // Should have moved up by 2 (5 -> 3)
    await waitFor(() => {
      expect(capturedEntry?.getScrollState().scrollTop).toBe(3);
    });

    await act(async () => {
      capturedEntry?.scrollBy(-2);
    });
    await waitFor(() => {
      expect(capturedEntry?.getScrollState().scrollTop).toBe(1);
    });
    unmount2();
  });

  describe('keypress handling', () => {
    it.each([
      {
        name: 'scrolls down when overflow exists and not at bottom',
        initialScrollTop: 0,
        scrollHeight: 10,
        keySequence: '\u001B[1;2B', // Shift+Down
        expectedScrollTop: 1,
      },
      {
        name: 'scrolls up when overflow exists and not at top',
        initialScrollTop: 2,
        scrollHeight: 10,
        keySequence: '\u001B[1;2A', // Shift+Up
        expectedScrollTop: 1,
      },
      {
        name: 'does not scroll up when at top (allows event to bubble)',
        initialScrollTop: 0,
        scrollHeight: 10,
        keySequence: '\u001B[1;2A', // Shift+Up
        expectedScrollTop: 0,
      },
      {
        name: 'does not scroll down when at bottom (allows event to bubble)',
        initialScrollTop: 5, // maxScroll = 10 - 5 = 5
        scrollHeight: 10,
        keySequence: '\u001B[1;2B', // Shift+Down
        expectedScrollTop: 5,
      },
      {
        name: 'does not scroll when content fits (allows event to bubble)',
        initialScrollTop: 0,
        scrollHeight: 5, // Same as innerHeight (5)
        keySequence: '\u001B[1;2B', // Shift+Down
        expectedScrollTop: 0,
      },
    ])(
      '$name',
      async ({
        initialScrollTop,
        scrollHeight,
        keySequence,
        expectedScrollTop,
      }) => {
        let capturedEntry: ScrollProviderModule.ScrollableEntry | undefined;
        vi.spyOn(ScrollProviderModule, 'useScrollable').mockImplementation(
          async (entry, isActive) => {
            if (isActive) {
              capturedEntry = entry as ScrollProviderModule.ScrollableEntry;
            }
          },
        );

        const { stdin, unmount, waitUntilReady } = await renderWithProviders(
          <Scrollable hasFocus={true} height={5}>
            <Box height={scrollHeight}>
              <Text>Content</Text>
            </Box>
          </Scrollable>,
        );

        // Ensure initial state using existing scrollBy method
        await act(async () => {
          // Reset to top first, then scroll to desired start position
          capturedEntry!.scrollBy(-100);
          if (initialScrollTop > 0) {
            capturedEntry!.scrollBy(initialScrollTop);
          }
        });
        expect(capturedEntry!.getScrollState().scrollTop).toBe(
          initialScrollTop,
        );

        await act(async () => {
          stdin.write(keySequence);
        });
        await waitUntilReady();

        expect(capturedEntry!.getScrollState().scrollTop).toBe(
          expectedScrollTop,
        );
        unmount();
      },
    );
  });
});