summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/components/LoadingIndicator.test.tsx
blob: 6a8880d48884a24a098ad092047192b2c324cc05 (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { render } from 'ink-testing-library';
import { Text } from 'ink'; // Import Text directly from ink
import { LoadingIndicator } from './LoadingIndicator.js';

vi.mock('ink-spinner', () => ({
  default: function MockSpinner() {
    return <Text>MockSpinner</Text>;
  },
}));

describe('<LoadingIndicator />', () => {
  it('should not render when isLoading is false', () => {
    const { lastFrame } = render(
      <LoadingIndicator
        isLoading={false}
        showSpinner={true}
        currentLoadingPhrase="Loading..."
        elapsedTime={0}
      />,
    );
    expect(lastFrame()).toBe('');
  });

  it('should render spinner, phrase, and time when isLoading is true and showSpinner is true', () => {
    const phrase = 'Processing data...';
    const time = 5;
    const { lastFrame } = render(
      <LoadingIndicator
        isLoading={true}
        showSpinner={true}
        currentLoadingPhrase={phrase}
        elapsedTime={time}
      />,
    );

    const output = lastFrame();
    expect(output).toContain(phrase);
    expect(output).toContain(`(esc to cancel, ${time}s)`);
    // Check for spinner presence by looking for its characteristic characters or structure
    // This is a bit fragile as it depends on Spinner's output.
    // A more robust way would be to mock Spinner and check if it was rendered.
    expect(output).toContain('MockSpinner'); // Check for the mocked spinner text
  });

  it('should render phrase and time but no spinner when isLoading is true and showSpinner is false', () => {
    const phrase = 'Waiting for input...';
    const time = 10;
    const { lastFrame } = render(
      <LoadingIndicator
        isLoading={true}
        showSpinner={false}
        currentLoadingPhrase={phrase}
        elapsedTime={time}
      />,
    );
    const output = lastFrame();
    expect(output).toContain(phrase);
    expect(output).toContain(`(esc to cancel, ${time}s)`);
    // Ensure spinner characters are NOT present
    expect(output).not.toContain('MockSpinner');
  });

  it('should display the currentLoadingPhrase correctly', () => {
    const specificPhrase = 'Almost there!';
    const { lastFrame } = render(
      <LoadingIndicator
        isLoading={true}
        showSpinner={true}
        currentLoadingPhrase={specificPhrase}
        elapsedTime={3}
      />,
    );
    expect(lastFrame()).toContain(specificPhrase);
  });

  it('should display the elapsedTime correctly', () => {
    const specificTime = 7;
    const { lastFrame } = render(
      <LoadingIndicator
        isLoading={true}
        showSpinner={true}
        currentLoadingPhrase="Working..."
        elapsedTime={specificTime}
      />,
    );
    expect(lastFrame()).toContain(`(esc to cancel, ${specificTime}s)`);
  });
});