-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathListView.test.tsx
More file actions
264 lines (218 loc) · 7.73 KB
/
ListView.test.tsx
File metadata and controls
264 lines (218 loc) · 7.73 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { ListView } from '../ListView';
import type { ListViewSchema } from '@object-ui/types';
import { SchemaRendererProvider } from '@object-ui/react';
// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => { store[key] = value; },
clear: () => { store = {}; },
removeItem: (key: string) => { delete store[key]; },
};
})();
const mockDataSource = {
find: vi.fn().mockResolvedValue([]),
findOne: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
};
const renderWithProvider = (component: React.ReactNode) => {
return render(
<SchemaRendererProvider dataSource={mockDataSource}>
{component}
</SchemaRendererProvider>
);
};
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
describe('ListView', () => {
beforeEach(() => {
localStorageMock.clear();
});
it('should be exported', () => {
expect(ListView).toBeDefined();
});
it('should be a function', () => {
expect(typeof ListView).toBe('function');
});
it('should render with basic schema', () => {
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
const { container } = renderWithProvider(<ListView schema={schema} />);
expect(container).toBeTruthy();
});
it('should render search button', () => {
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
renderWithProvider(<ListView schema={schema} />);
const searchButton = screen.getByRole('button', { name: /search/i });
expect(searchButton).toBeInTheDocument();
});
it('should expand search and call onSearchChange when search input changes', () => {
const onSearchChange = vi.fn();
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
renderWithProvider(<ListView schema={schema} onSearchChange={onSearchChange} />);
// Click search button to expand
const searchButton = screen.getByRole('button', { name: /search/i });
fireEvent.click(searchButton);
const searchInput = screen.getByPlaceholderText(/find/i);
fireEvent.change(searchInput, { target: { value: 'test' } });
expect(onSearchChange).toHaveBeenCalledWith('test');
});
it('should persist view preference to localStorage', () => {
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
options: {
kanban: {
groupField: 'status',
},
},
};
renderWithProvider(<ListView schema={schema} showViewSwitcher={true} />);
// Find kanban view button and click it
// ViewSwitcher uses buttons with aria-label
const kanbanButton = screen.getByLabelText('Kanban');
fireEvent.click(kanbanButton);
// localStorage should be set with new view
const storageKey = 'listview-contacts-view';
expect(localStorageMock.getItem(storageKey)).toBe('kanban');
});
it('should call onViewChange when view is changed', () => {
const onViewChange = vi.fn();
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
renderWithProvider(<ListView schema={schema} onViewChange={onViewChange} />);
// Simulate view change by updating the view prop in ViewSwitcher
// Since we can't easily trigger the actual view switcher in tests,
// we verify the callback is properly passed to the component
expect(onViewChange).toBeDefined();
// If we could trigger view change, we would expect:
// expect(onViewChange).toHaveBeenCalledWith('list');
});
it('should toggle filter panel when filter button is clicked', () => {
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
renderWithProvider(<ListView schema={schema} />);
// Find filter button (by icon or aria-label)
const buttons = screen.getAllByRole('button');
const filterButton = buttons.find(btn =>
btn.querySelector('svg') !== null
);
if (filterButton) {
fireEvent.click(filterButton);
// After click, filter panel should be visible
}
});
it('should handle sort order toggle', () => {
const onSortChange = vi.fn();
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
sort: [{ field: 'name', order: 'asc' }],
};
renderWithProvider(<ListView schema={schema} onSortChange={onSortChange} />);
// Find sort button
const buttons = screen.getAllByRole('button');
const sortButton = buttons.find(btn =>
btn.querySelector('svg') !== null
);
if (sortButton) {
fireEvent.click(sortButton);
// onSortChange should be called with new order
}
});
it('should clear search when clear button is clicked', () => {
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
renderWithProvider(<ListView schema={schema} />);
// Click search button to expand search input
const searchButton = screen.getByRole('button', { name: /search/i });
fireEvent.click(searchButton);
const searchInput = screen.getByPlaceholderText(/find/i) as HTMLInputElement;
// Type in search
fireEvent.change(searchInput, { target: { value: 'test' } });
expect(searchInput.value).toBe('test');
// Find and click clear button (the X button inside the expanded search)
const buttons = screen.getAllByRole('button');
const clearButton = buttons.find(btn =>
btn.querySelector('svg') !== null && searchInput.value !== ''
);
if (clearButton) {
fireEvent.click(clearButton);
}
});
it('should show default empty state when no data', async () => {
mockDataSource.find.mockResolvedValue([]);
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
};
renderWithProvider(<ListView schema={schema} />);
// Wait for data fetch to complete
await vi.waitFor(() => {
expect(screen.getByTestId('empty-state')).toBeInTheDocument();
});
expect(screen.getByText('No items found')).toBeInTheDocument();
});
it('should show custom empty state when configured', async () => {
mockDataSource.find.mockResolvedValue([]);
const schema: ListViewSchema = {
type: 'list-view',
objectName: 'contacts',
viewType: 'grid',
fields: ['name', 'email'],
emptyState: {
title: 'No contacts yet',
message: 'Add your first contact to get started.',
},
};
renderWithProvider(<ListView schema={schema} />);
await vi.waitFor(() => {
expect(screen.getByTestId('empty-state')).toBeInTheDocument();
});
expect(screen.getByText('No contacts yet')).toBeInTheDocument();
expect(screen.getByText('Add your first contact to get started.')).toBeInTheDocument();
});
});