Skip to content

Commit 1c4fe9d

Browse files
committed
Refactor tests to improve button role queries and update schema fields in ObjectForm and ListView
1 parent 3d6d9ca commit 1c4fe9d

File tree

6 files changed

+55
-27
lines changed

6 files changed

+55
-27
lines changed

apps/console/src/__tests__/BrowserSimulation.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ describe('Console Application Simulation', () => {
145145
});
146146

147147
// Verify "New" Button exists
148-
const newButton = screen.getByRole('button', { name: /New Kitchen Sink/i });
148+
const newButton = screen.getByRole('button', { name: /New/i });
149149
expect(newButton).toBeInTheDocument();
150150

151151
// Verify Grid rendered
@@ -169,7 +169,7 @@ describe('Console Application Simulation', () => {
169169
});
170170

171171
// 2. Click "New Kitchen Sink"
172-
const newButton = screen.getByRole('button', { name: /New Kitchen Sink/i });
172+
const newButton = screen.getByRole('button', { name: /New/i });
173173
fireEvent.click(newButton);
174174

175175
// 3. Verify Dialog Opens
@@ -217,7 +217,7 @@ describe('Console Application Simulation', () => {
217217

218218
// Verify Data Grid Headers
219219
expect(screen.getByText('Region')).toBeInTheDocument();
220-
expect(screen.getByText('Sales')).toBeInTheDocument();
220+
expect(screen.getAllByText('Sales').length).toBeGreaterThan(0);
221221
expect(screen.getByText('Target')).toBeInTheDocument();
222222

223223
// Verify Data Rows
@@ -243,7 +243,7 @@ describe('Console Application Simulation', () => {
243243
});
244244

245245
// Verify the form can be opened (showing metadata was loaded)
246-
const newButton = screen.getByRole('button', { name: /New Kitchen Sink/i });
246+
const newButton = screen.getByRole('button', { name: /New/i });
247247
fireEvent.click(newButton);
248248

249249
// Verify form loaded with schema-based fields
@@ -267,7 +267,7 @@ describe('Console Application Simulation', () => {
267267
expect(screen.getByRole('heading', { name: /Kitchen Sink/i })).toBeInTheDocument();
268268
});
269269

270-
const newButton = screen.getByRole('button', { name: /New Kitchen Sink/i });
270+
const newButton = screen.getByRole('button', { name: /New/i });
271271
expect(newButton).toBeInTheDocument();
272272
});
273273

apps/console/src/__tests__/ObjectForm.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe('ObjectForm with MSW Integration', () => {
211211

212212
const createdContact = onSuccess.mock.calls[0][0];
213213
// Check default values from schema
214-
expect(createdContact.priority).toBe(5);
214+
expect(createdContact.priority).toBe('Medium');
215215
expect(createdContact.is_active).toBe(true);
216216
});
217217
});
@@ -362,19 +362,19 @@ describe('ObjectForm with MSW Integration', () => {
362362
<ObjectForm
363363
schema={{
364364
type: 'object-form',
365-
objectName: 'contact',
365+
objectName: 'kitchen_sink',
366366
mode: 'create',
367-
fields: ['name', 'email', 'priority'],
367+
fields: ['name', 'amount'],
368368
}}
369369
dataSource={dataSource}
370370
/>
371371
);
372372

373373
await waitFor(() => {
374-
expect(screen.getByLabelText(/^Name/i)).toBeInTheDocument();
374+
expect(screen.getByLabelText(/Text/i)).toBeInTheDocument();
375375
});
376376

377-
const numberInput = screen.getByLabelText(/Priority/i) as HTMLInputElement;
377+
const numberInput = screen.getByLabelText(/Number/i) as HTMLInputElement;
378378
expect(numberInput.type).toBe('number');
379379
});
380380

examples/crm/src/objects/contact.object.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const ContactObject = ObjectSchema.create({
66
icon: 'user',
77
fields: {
88
name: Field.text({ label: 'Name', required: true, searchable: true }),
9+
company: Field.text({ label: 'Company' }),
910
email: Field.email({ label: 'Email', searchable: true }),
1011
phone: Field.text({ label: 'Phone' }),
1112
title: Field.text({ label: 'Title' }),

packages/fields/src/widgets/SelectField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function SelectField({ value, onChange, field, readonly, ...props }: Fiel
2525
onValueChange={onChange}
2626
disabled={readonly || props.disabled}
2727
>
28-
<SelectTrigger className={props.className}>
28+
<SelectTrigger className={props.className} id={props.id}>
2929
<SelectValue placeholder={config?.placeholder || "Select an option"} />
3030
</SelectTrigger>
3131
<SelectContent position="popper">

packages/plugin-detail/src/__tests__/DetailView.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ describe('DetailView', () => {
117117
data: { name: 'John Doe' },
118118
fields: [{ name: 'name', label: 'Name' }],
119119
showEdit: true,
120+
// Disable back button to ensure it's not the first button found if using generic search
121+
showBack: false
120122
};
121123

122124
render(<DetailView schema={schema} onEdit={onEdit} />);
123125

124-
const buttons = screen.getAllByRole('button');
125-
const editButton = buttons.find(btn =>
126-
btn.querySelector('svg') !== null
127-
);
126+
// Find button with text "Edit"
127+
const editButton = screen.getByRole('button', { name: /edit/i });
128128

129129
if (editButton) {
130130
fireEvent.click(editButton);
@@ -241,8 +241,9 @@ describe('DetailView', () => {
241241

242242
const { container } = render(<DetailView schema={schema} />);
243243

244-
// Check for skeleton elements (they typically have specific classes)
245-
const skeletons = container.querySelectorAll('[class*="skeleton"]');
244+
// Check for skeleton elements (they typically have animate-pulse class)
245+
// DetailedView uses Skeleton component which has animate-pulse class
246+
const skeletons = container.querySelectorAll('.animate-pulse');
246247
expect(skeletons.length).toBeGreaterThan(0);
247248
});
248249
});

packages/plugin-list/src/__tests__/ListView.test.tsx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
1010
import { render, screen, fireEvent } from '@testing-library/react';
1111
import { ListView } from '../ListView';
1212
import type { ListViewSchema } from '@object-ui/types';
13+
import { SchemaRendererProvider } from '@object-ui/react';
1314

1415
// Mock localStorage
1516
const localStorageMock = (() => {
@@ -22,6 +23,22 @@ const localStorageMock = (() => {
2223
};
2324
})();
2425

26+
const mockDataSource = {
27+
find: vi.fn().mockResolvedValue([]),
28+
findOne: vi.fn(),
29+
create: vi.fn(),
30+
update: vi.fn(),
31+
delete: vi.fn(),
32+
};
33+
34+
const renderWithProvider = (component: React.ReactNode) => {
35+
return render(
36+
<SchemaRendererProvider dataSource={mockDataSource}>
37+
{component}
38+
</SchemaRendererProvider>
39+
);
40+
};
41+
2542
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
2643

2744
describe('ListView', () => {
@@ -45,7 +62,7 @@ describe('ListView', () => {
4562
fields: ['name', 'email'],
4663
};
4764

48-
const { container } = render(<ListView schema={schema} />);
65+
const { container } = renderWithProvider(<ListView schema={schema} />);
4966
expect(container).toBeTruthy();
5067
});
5168

@@ -57,7 +74,7 @@ describe('ListView', () => {
5774
fields: ['name', 'email'],
5875
};
5976

60-
render(<ListView schema={schema} />);
77+
renderWithProvider(<ListView schema={schema} />);
6178
const searchInput = screen.getByPlaceholderText(/search/i);
6279
expect(searchInput).toBeInTheDocument();
6380
});
@@ -71,7 +88,7 @@ describe('ListView', () => {
7188
fields: ['name', 'email'],
7289
};
7390

74-
render(<ListView schema={schema} onSearchChange={onSearchChange} />);
91+
renderWithProvider(<ListView schema={schema} onSearchChange={onSearchChange} />);
7592
const searchInput = screen.getByPlaceholderText(/search/i);
7693

7794
fireEvent.change(searchInput, { target: { value: 'test' } });
@@ -86,11 +103,20 @@ describe('ListView', () => {
86103
fields: ['name', 'email'],
87104
};
88105

89-
render(<ListView schema={schema} />);
106+
renderWithProvider(<ListView schema={schema} />);
107+
108+
// Find list view button and click it
109+
// Using getAllByRole because there might be multiple buttons
110+
const buttons = screen.getAllByRole('radio'); // ToggleGroup usually uses radio role if type="single"
111+
// However, if it's implemented as buttons using ToggleGroup which is roving tabindex...
112+
// Let's try finding by aria-label which ViewSwitcher sets
113+
const listButton = screen.getByLabelText('List');
114+
115+
fireEvent.click(listButton);
90116

91-
// localStorage should be set with initial view
117+
// localStorage should be set with new view
92118
const storageKey = 'listview-contacts-view';
93-
expect(localStorageMock.getItem(storageKey)).toBeTruthy();
119+
expect(localStorageMock.getItem(storageKey)).toBe('list');
94120
});
95121

96122
it('should call onViewChange when view is changed', () => {
@@ -102,7 +128,7 @@ describe('ListView', () => {
102128
fields: ['name', 'email'],
103129
};
104130

105-
const { rerender } = render(<ListView schema={schema} onViewChange={onViewChange} />);
131+
renderWithProvider(<ListView schema={schema} onViewChange={onViewChange} />);
106132

107133
// Simulate view change by updating the view prop in ViewSwitcher
108134
// Since we can't easily trigger the actual view switcher in tests,
@@ -121,7 +147,7 @@ describe('ListView', () => {
121147
fields: ['name', 'email'],
122148
};
123149

124-
render(<ListView schema={schema} />);
150+
renderWithProvider(<ListView schema={schema} />);
125151

126152
// Find filter button (by icon or aria-label)
127153
const buttons = screen.getAllByRole('button');
@@ -145,7 +171,7 @@ describe('ListView', () => {
145171
sort: [{ field: 'name', order: 'asc' }],
146172
};
147173

148-
render(<ListView schema={schema} onSortChange={onSortChange} />);
174+
renderWithProvider(<ListView schema={schema} onSortChange={onSortChange} />);
149175

150176
// Find sort button
151177
const buttons = screen.getAllByRole('button');
@@ -167,7 +193,7 @@ describe('ListView', () => {
167193
fields: ['name', 'email'],
168194
};
169195

170-
render(<ListView schema={schema} />);
196+
renderWithProvider(<ListView schema={schema} />);
171197
const searchInput = screen.getByPlaceholderText(/search/i) as HTMLInputElement;
172198

173199
// Type in search

0 commit comments

Comments
 (0)