File size: 6,727 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { jest } from '@jest/globals';
import VirtualizedAgentGrid from '../VirtualizedAgentGrid';
import type t from 'librechat-data-provider';

// Mock react-virtualized
jest.mock('react-virtualized', () => ({
  AutoSizer: ({
    children,
    disableHeight,
  }: {
    children: (props: { width: number; height?: number }) => React.ReactNode;
    disableHeight?: boolean;
  }) => {
    if (disableHeight) {
      return children({ width: 800 });
    }
    return children({ width: 800, height: 600 });
  },
  List: ({
    rowRenderer,
    rowCount,
    width,
    style,
    'aria-rowcount': ariaRowCount,
    'data-testid': dataTestId,
    'data-total-rows': dataTotalRows,
  }: {
    rowRenderer: any;
    rowCount: number;
    autoHeight?: boolean;
    height?: number;
    width?: number;
    rowHeight?: number;
    overscanRowCount?: number;
    scrollTop?: number;
    isScrolling?: boolean;
    onScroll?: any;
    style?: any;
    'aria-rowcount'?: number;
    'data-testid'?: string;
    'data-total-rows'?: number;
  }) => (
    <div
      data-testid={dataTestId || 'virtual-list'}
      aria-rowcount={ariaRowCount}
      data-total-rows={dataTotalRows}
      style={style}
    >
      {Array.from({ length: Math.min(rowCount, 5) }, (_, index) =>
        rowRenderer({
          index,
          key: `row-${index}`,
          style: {},
          parent: { props: { width: width || 800 } },
        }),
      )}
    </div>
  ),
  WindowScroller: ({
    children,
  }: {
    children: (props: any) => React.ReactNode;
    scrollElement?: HTMLElement | null;
  }) => {
    return children({
      height: 600,
      isScrolling: false,
      registerChild: (_ref: any) => {},
      onChildScroll: () => {},
      scrollTop: 0,
    });
  },
}));

// Mock the data provider
const mockInfiniteQuery = {
  data: {
    pages: [
      {
        data: [
          {
            id: '1',
            name: 'Test Agent 1',
            description: 'A test agent for virtual scrolling',
            category: 'productivity',
          },
          {
            id: '2',
            name: 'Test Agent 2',
            description: 'Another test agent',
            category: 'development',
          },
        ],
      },
    ],
  },
  isLoading: false,
  error: null,
  isFetching: false,
  fetchNextPage: jest.fn(),
  hasNextPage: true,
  refetch: jest.fn(),
  isFetchingNextPage: false,
};

jest.mock('~/data-provider/Agents', () => ({
  useMarketplaceAgentsInfiniteQuery: jest.fn(() => mockInfiniteQuery),
}));

// Mock other hooks
jest.mock('~/hooks', () => ({
  useAgentCategories: () => ({
    categories: [
      { value: 'productivity', label: 'Productivity' },
      { value: 'development', label: 'Development' },
    ],
  }),
  useLocalize: () => (key: string, params?: any) => {
    if (key === 'com_agents_grid_announcement') {
      return `Found ${params?.count || 0} agents in ${params?.category || 'category'}`;
    }
    return key;
  },
}));

jest.mock('../SmartLoader', () => ({
  useHasData: () => true,
}));

jest.mock('../AgentCard', () => {
  return function MockAgentCard({ agent, onClick }: { agent: t.Agent; onClick: () => void }) {
    return (
      <div data-testid={`agent-card-${agent.id}`} onClick={onClick}>
        <h3>{agent.name}</h3>
        <p>{agent.description}</p>
      </div>
    );
  };
});

describe('VirtualizedAgentGrid', () => {
  let queryClient: QueryClient;

  beforeEach(() => {
    queryClient = new QueryClient({
      defaultOptions: {
        queries: { retry: false },
        mutations: { retry: false },
      },
    });
  });

  const renderComponent = (props = {}) => {
    const defaultProps = {
      category: 'all',
      searchQuery: '',
      onSelectAgent: jest.fn(),
    };

    return render(
      <QueryClientProvider client={queryClient}>
        <VirtualizedAgentGrid {...defaultProps} {...props} />
      </QueryClientProvider>,
    );
  };

  it('renders virtual list container', async () => {
    renderComponent();

    await waitFor(() => {
      expect(screen.getByTestId('virtual-list')).toBeInTheDocument();
    });
  });

  it('displays agent cards in virtual rows', async () => {
    renderComponent();

    await waitFor(() => {
      expect(screen.getByTestId('agent-card-1')).toBeInTheDocument();
      expect(screen.getByTestId('agent-card-2')).toBeInTheDocument();
    });

    expect(screen.getByText('Test Agent 1')).toBeInTheDocument();
    expect(screen.getByText('Test Agent 2')).toBeInTheDocument();
  });

  it('calls onSelectAgent when agent card is clicked', async () => {
    const onSelectAgent = jest.fn();
    renderComponent({ onSelectAgent });

    await waitFor(() => {
      expect(screen.getByTestId('agent-card-1')).toBeInTheDocument();
    });

    screen.getByTestId('agent-card-1').click();

    expect(onSelectAgent).toHaveBeenCalledWith({
      id: '1',
      name: 'Test Agent 1',
      description: 'A test agent for virtual scrolling',
      category: 'productivity',
    });
  });

  it('shows loading spinner when loading', async () => {
    const mockQuery = jest.fn(() => ({
      ...mockInfiniteQuery,
      isLoading: true,
      data: undefined,
    }));

    const useMarketplaceAgentsInfiniteQuery =
      jest.requireMock('~/data-provider/Agents').useMarketplaceAgentsInfiniteQuery;
    useMarketplaceAgentsInfiniteQuery.mockImplementation(mockQuery);

    renderComponent();

    // Should show loading spinner
    const spinner = document.querySelector('.spinner');
    expect(spinner).toBeInTheDocument();
    expect(spinner).toHaveClass('h-8 w-8 text-primary');
  });

  it('has proper accessibility attributes', async () => {
    // Reset the mock to ensure we have data
    const useMarketplaceAgentsInfiniteQuery =
      jest.requireMock('~/data-provider/Agents').useMarketplaceAgentsInfiniteQuery;
    useMarketplaceAgentsInfiniteQuery.mockImplementation(() => mockInfiniteQuery);

    renderComponent({ category: 'productivity' });

    await waitFor(() => {
      expect(screen.getByTestId('virtual-list')).toBeInTheDocument();
    });

    const gridContainer = screen.getByRole('grid');
    expect(gridContainer).toHaveAttribute('aria-label');
    expect(gridContainer.getAttribute('aria-label')).toContain('2');
    expect(gridContainer.getAttribute('aria-label')).toContain('Productivity');

    const tabpanel = screen.getByRole('tabpanel');
    expect(tabpanel).toHaveAttribute('id', 'category-panel-productivity');
    expect(tabpanel).toHaveAttribute('aria-labelledby', 'category-tab-productivity');
  });
});