File size: 6,581 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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import { renderWithProviders } from '../../../test-utils/render.js';
import { TabHeader, type Tab } from './TabHeader.js';

const MOCK_TABS: Tab[] = [
  { key: '0', header: 'Tab 1' },
  { key: '1', header: 'Tab 2' },
  { key: '2', header: 'Tab 3' },
];

describe('TabHeader', () => {
  describe('rendering', () => {
    it('renders null for single tab', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader
          tabs={[{ key: '0', header: 'Only Tab' }]}
          currentIndex={0}
        />,
      );
      expect(lastFrame({ allowEmpty: true })).toBe('');
      unmount();
    });

    it('renders all tab headers', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={MOCK_TABS} currentIndex={0} />,
      );
      const frame = lastFrame();
      expect(frame).toContain('Tab 1');
      expect(frame).toContain('Tab 2');
      expect(frame).toContain('Tab 3');
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('renders separators between tabs', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={MOCK_TABS} currentIndex={0} />,
      );
      const frame = lastFrame();
      // Should have 2 separators for 3 tabs
      const separatorCount = (frame?.match(/│/g) || []).length;
      expect(separatorCount).toBe(2);
      expect(frame).toMatchSnapshot();
      unmount();
    });
  });

  describe('arrows', () => {
    it('shows arrows by default', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={MOCK_TABS} currentIndex={0} />,
      );
      const frame = lastFrame();
      expect(frame).toContain('←');
      expect(frame).toContain('→');
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('hides arrows when showArrows is false', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={MOCK_TABS} currentIndex={0} showArrows={false} />,
      );
      const frame = lastFrame();
      expect(frame).not.toContain('←');
      expect(frame).not.toContain('→');
      expect(frame).toMatchSnapshot();
      unmount();
    });
  });

  describe('status icons', () => {
    it('shows status icons by default', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={MOCK_TABS} currentIndex={0} />,
      );
      const frame = lastFrame();
      // Default uncompleted icon is □
      expect(frame).toContain('□');
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('hides status icons when showStatusIcons is false', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={MOCK_TABS} currentIndex={0} showStatusIcons={false} />,
      );
      const frame = lastFrame();
      expect(frame).not.toContain('□');
      expect(frame).not.toContain('✓');
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('shows checkmark for completed tabs', async () => {
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader
          tabs={MOCK_TABS}
          currentIndex={0}
          completedIndices={new Set([0, 2])}
        />,
      );
      const frame = lastFrame();
      // Should have 2 checkmarks and 1 box
      const checkmarkCount = (frame?.match(/✓/g) || []).length;
      const boxCount = (frame?.match(/□/g) || []).length;
      expect(checkmarkCount).toBe(2);
      expect(boxCount).toBe(1);
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('shows special icon for special tabs', async () => {
      const tabsWithSpecial: Tab[] = [
        { key: '0', header: 'Tab 1' },
        { key: '1', header: 'Review', isSpecial: true },
      ];
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={tabsWithSpecial} currentIndex={0} />,
      );
      const frame = lastFrame();
      // Special tab shows ≡ icon
      expect(frame).toContain('≡');
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('uses tab statusIcon when provided', async () => {
      const tabsWithCustomIcon: Tab[] = [
        { key: '0', header: 'Tab 1', statusIcon: '★' },
        { key: '1', header: 'Tab 2' },
      ];
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={tabsWithCustomIcon} currentIndex={0} />,
      );
      const frame = lastFrame();
      expect(frame).toContain('★');
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('uses custom renderStatusIcon when provided', async () => {
      const renderStatusIcon = () => '•';
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader
          tabs={MOCK_TABS}
          currentIndex={0}
          renderStatusIcon={renderStatusIcon}
        />,
      );
      const frame = lastFrame();
      const bulletCount = (frame?.match(/•/g) || []).length;
      expect(bulletCount).toBe(3);
      expect(frame).toMatchSnapshot();
      unmount();
    });

    it('truncates long headers when not selected', async () => {
      const longTabs: Tab[] = [
        { key: '0', header: 'ThisIsAVeryLongHeaderThatShouldBeTruncated' },
        { key: '1', header: 'AnotherVeryLongHeader' },
      ];
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader tabs={longTabs} currentIndex={0} />,
      );
      const frame = lastFrame();

      // Current tab (index 0) should NOT be truncated
      expect(frame).toContain('ThisIsAVeryLongHeaderThatShouldBeTruncated');

      // Inactive tab (index 1) SHOULD be truncated to 16 chars (15 chars + …)
      const expectedTruncated = 'AnotherVeryLong…';
      expect(frame).toContain(expectedTruncated);
      expect(frame).not.toContain('AnotherVeryLongHeader');

      unmount();
    });

    it('falls back to default when renderStatusIcon returns undefined', async () => {
      const renderStatusIcon = () => undefined;
      const { lastFrame, unmount } = await renderWithProviders(
        <TabHeader
          tabs={MOCK_TABS}
          currentIndex={0}
          renderStatusIcon={renderStatusIcon}
        />,
      );
      const frame = lastFrame();
      expect(frame).toContain('□');
      expect(frame).toMatchSnapshot();
      unmount();
    });
  });
});