|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 10 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 11 | +import { ObjectGantt } from '../ObjectGantt'; |
| 12 | +import type { ObjectGridSchema, DataSource } from '@object-ui/types'; |
| 13 | + |
| 14 | +describe('ObjectGantt', () => { |
| 15 | + let mockDataSource: DataSource; |
| 16 | + let mockSchema: ObjectGridSchema; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Mock data source |
| 20 | + mockDataSource = { |
| 21 | + query: vi.fn().mockResolvedValue([ |
| 22 | + { |
| 23 | + _id: '1', |
| 24 | + title: 'Task 1', |
| 25 | + start_date: '2024-01-01T00:00:00Z', |
| 26 | + end_date: '2024-01-15T00:00:00Z', |
| 27 | + progress: 50, |
| 28 | + }, |
| 29 | + { |
| 30 | + _id: '2', |
| 31 | + title: 'Task 2', |
| 32 | + start_date: '2024-01-10T00:00:00Z', |
| 33 | + end_date: '2024-01-25T00:00:00Z', |
| 34 | + progress: 75, |
| 35 | + }, |
| 36 | + ]), |
| 37 | + getObjectSchema: vi.fn().mockResolvedValue({ |
| 38 | + name: 'tasks', |
| 39 | + label: 'Tasks', |
| 40 | + fields: { |
| 41 | + title: { type: 'text', label: 'Title' }, |
| 42 | + start_date: { type: 'date', label: 'Start Date' }, |
| 43 | + end_date: { type: 'date', label: 'End Date' }, |
| 44 | + progress: { type: 'number', label: 'Progress' }, |
| 45 | + }, |
| 46 | + }), |
| 47 | + } as any; |
| 48 | + |
| 49 | + // Mock schema with gantt config |
| 50 | + mockSchema = { |
| 51 | + type: 'object-grid', |
| 52 | + objectName: 'tasks', |
| 53 | + data: { |
| 54 | + provider: 'object', |
| 55 | + object: 'tasks', |
| 56 | + }, |
| 57 | + filter: { |
| 58 | + gantt: { |
| 59 | + startDateField: 'start_date', |
| 60 | + endDateField: 'end_date', |
| 61 | + titleField: 'title', |
| 62 | + progressField: 'progress', |
| 63 | + }, |
| 64 | + }, |
| 65 | + } as any; |
| 66 | + }); |
| 67 | + |
| 68 | + it('should render loading state initially', () => { |
| 69 | + render(<ObjectGantt schema={mockSchema} dataSource={mockDataSource} />); |
| 70 | + expect(screen.getByText(/Loading Gantt chart/i)).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should fetch and display gantt data', async () => { |
| 74 | + render(<ObjectGantt schema={mockSchema} dataSource={mockDataSource} />); |
| 75 | + |
| 76 | + await waitFor(() => { |
| 77 | + expect(mockDataSource.query).toHaveBeenCalledWith('tasks', expect.any(Object)); |
| 78 | + }); |
| 79 | + |
| 80 | + await waitFor(() => { |
| 81 | + expect(screen.getByText('Task 1')).toBeInTheDocument(); |
| 82 | + expect(screen.getByText('Task 2')).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should display task progress', async () => { |
| 87 | + render(<ObjectGantt schema={mockSchema} dataSource={mockDataSource} />); |
| 88 | + |
| 89 | + await waitFor(() => { |
| 90 | + expect(screen.getByText('50%')).toBeInTheDocument(); |
| 91 | + expect(screen.getByText('75%')).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should render with inline data', async () => { |
| 96 | + const schemaWithInlineData = { |
| 97 | + ...mockSchema, |
| 98 | + data: { |
| 99 | + provider: 'value', |
| 100 | + items: [ |
| 101 | + { |
| 102 | + id: '1', |
| 103 | + title: 'Project A', |
| 104 | + start_date: '2024-01-01', |
| 105 | + end_date: '2024-01-31', |
| 106 | + progress: 30, |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, |
| 110 | + } as any; |
| 111 | + |
| 112 | + render(<ObjectGantt schema={schemaWithInlineData} />); |
| 113 | + |
| 114 | + await waitFor(() => { |
| 115 | + expect(screen.getByText('Project A')).toBeInTheDocument(); |
| 116 | + expect(screen.getByText('30%')).toBeInTheDocument(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should show error when gantt config is missing', async () => { |
| 121 | + const schemaWithoutGantt = { |
| 122 | + ...mockSchema, |
| 123 | + filter: {}, |
| 124 | + } as any; |
| 125 | + |
| 126 | + render(<ObjectGantt schema={schemaWithoutGantt} dataSource={mockDataSource} />); |
| 127 | + |
| 128 | + await waitFor(() => { |
| 129 | + expect(screen.getByText(/Gantt configuration required/i)).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle task click', async () => { |
| 134 | + const onTaskClick = vi.fn(); |
| 135 | + render( |
| 136 | + <ObjectGantt |
| 137 | + schema={mockSchema} |
| 138 | + dataSource={mockDataSource} |
| 139 | + onTaskClick={onTaskClick} |
| 140 | + /> |
| 141 | + ); |
| 142 | + |
| 143 | + await waitFor(() => { |
| 144 | + expect(screen.getByText('Task 1')).toBeInTheDocument(); |
| 145 | + }); |
| 146 | + |
| 147 | + // Click on task in the list |
| 148 | + const task = screen.getAllByText('Task 1')[0].closest('div'); |
| 149 | + if (task) { |
| 150 | + task.click(); |
| 151 | + expect(onTaskClick).toHaveBeenCalledWith( |
| 152 | + expect.objectContaining({ title: 'Task 1' }) |
| 153 | + ); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + it('should display task list section', async () => { |
| 158 | + render(<ObjectGantt schema={mockSchema} dataSource={mockDataSource} />); |
| 159 | + |
| 160 | + await waitFor(() => { |
| 161 | + expect(screen.getByText('Tasks')).toBeInTheDocument(); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should display date ranges for tasks', async () => { |
| 166 | + render(<ObjectGantt schema={mockSchema} dataSource={mockDataSource} />); |
| 167 | + |
| 168 | + await waitFor(() => { |
| 169 | + // Should display formatted dates |
| 170 | + const dateElements = screen.getAllByText(/-/); |
| 171 | + expect(dateElements.length).toBeGreaterThan(0); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments