|
| 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 { ObjectView } from '../components/ObjectView'; |
| 12 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 13 | +import '@object-ui/plugin-grid'; |
| 14 | +import '@object-ui/plugin-kanban'; |
| 15 | +import '@object-ui/plugin-calendar'; |
| 16 | +import '@object-ui/plugin-list'; |
| 17 | + |
| 18 | +vi.mock('@object-ui/components', async () => { |
| 19 | + const actual = await vi.importActual('@object-ui/components'); |
| 20 | + return { ...actual }; |
| 21 | +}); |
| 22 | + |
| 23 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 24 | + const actual = await importOriginal<any>(); |
| 25 | + return { |
| 26 | + ...actual, |
| 27 | + useDataScope: () => undefined, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +const mockSetSearchParams = vi.fn(); |
| 32 | +let mockSearchParams = new URLSearchParams(); |
| 33 | +let mockViewId = 'calendar'; |
| 34 | + |
| 35 | +vi.mock('react-router-dom', () => ({ |
| 36 | + useParams: () => ({ objectName: 'todo_task', viewId: mockViewId }), |
| 37 | + useSearchParams: () => [mockSearchParams, mockSetSearchParams], |
| 38 | + useNavigate: () => vi.fn(), |
| 39 | +})); |
| 40 | + |
| 41 | +// Use dates in current month so events show on the visible calendar |
| 42 | +const now = new Date(); |
| 43 | +const year = now.getFullYear(); |
| 44 | +const month = String(now.getMonth() + 1).padStart(2, '0'); |
| 45 | + |
| 46 | +const records = [ |
| 47 | + { id: 't1', name: 'Set up CI/CD pipeline', due_date: `${year}-${month}-16T09:00:00`, status: 'todo' }, |
| 48 | + { id: 't2', name: 'Design dashboard', due_date: `${year}-${month}-18T10:00:00`, status: 'in_progress' }, |
| 49 | + { id: 't3', name: 'Fix mobile responsive', due_date: `${year}-${month}-20T14:00:00`, status: 'done' }, |
| 50 | +]; |
| 51 | + |
| 52 | +let mockDataSource: any; |
| 53 | + |
| 54 | +const mockObjects = [ |
| 55 | + { |
| 56 | + name: 'todo_task', |
| 57 | + label: 'Task', |
| 58 | + fields: { |
| 59 | + name: { label: 'Name', type: 'text' }, |
| 60 | + due_date: { label: 'Due Date', type: 'datetime' }, |
| 61 | + status: { |
| 62 | + label: 'Status', type: 'select', |
| 63 | + options: [ |
| 64 | + { value: 'todo', label: 'To Do' }, |
| 65 | + { value: 'in_progress', label: 'In Progress' }, |
| 66 | + { value: 'done', label: 'Done' }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }, |
| 70 | + listViews: { |
| 71 | + all: { |
| 72 | + name: 'all', |
| 73 | + label: 'All Tasks', |
| 74 | + type: 'grid', |
| 75 | + columns: ['name', 'due_date', 'status'], |
| 76 | + }, |
| 77 | + calendar: { |
| 78 | + name: 'calendar', |
| 79 | + label: 'Calendar', |
| 80 | + type: 'calendar', |
| 81 | + columns: ['name', 'due_date', 'status'], |
| 82 | + calendar: { |
| 83 | + startDateField: 'due_date', |
| 84 | + titleField: 'name', |
| 85 | + }, |
| 86 | + }, |
| 87 | + board: { |
| 88 | + name: 'board', |
| 89 | + label: 'Board', |
| 90 | + type: 'kanban', |
| 91 | + columns: ['name', 'due_date', 'status'], |
| 92 | + kanban: { |
| 93 | + groupField: 'status', |
| 94 | + titleField: 'name', |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | +]; |
| 100 | + |
| 101 | +describe('Duplicate Data Prevention', () => { |
| 102 | + beforeEach(() => { |
| 103 | + vi.clearAllMocks(); |
| 104 | + mockSearchParams = new URLSearchParams(); |
| 105 | + mockDataSource = { |
| 106 | + find: vi.fn().mockResolvedValue(records), |
| 107 | + findOne: vi.fn().mockResolvedValue({}), |
| 108 | + create: vi.fn().mockResolvedValue({}), |
| 109 | + update: vi.fn().mockResolvedValue({}), |
| 110 | + delete: vi.fn().mockResolvedValue(true), |
| 111 | + getObjectSchema: vi.fn().mockResolvedValue({ |
| 112 | + name: 'todo_task', |
| 113 | + label: 'Task', |
| 114 | + fields: mockObjects[0].fields, |
| 115 | + }), |
| 116 | + }; |
| 117 | + }); |
| 118 | + |
| 119 | + describe('Calendar view', () => { |
| 120 | + beforeEach(() => { |
| 121 | + mockViewId = 'calendar'; |
| 122 | + }); |
| 123 | + |
| 124 | + it('should not show duplicate events in calendar view', async () => { |
| 125 | + render( |
| 126 | + <SchemaRendererProvider dataSource={mockDataSource}> |
| 127 | + <ObjectView dataSource={mockDataSource} objects={mockObjects} onEdit={vi.fn()} /> |
| 128 | + </SchemaRendererProvider> |
| 129 | + ); |
| 130 | + |
| 131 | + // Calendar region should render |
| 132 | + await waitFor(() => { |
| 133 | + const calendarRegion = document.querySelector('[aria-label="Calendar"]'); |
| 134 | + expect(calendarRegion).toBeInTheDocument(); |
| 135 | + }, { timeout: 5000 }); |
| 136 | + |
| 137 | + // Wait for events to appear |
| 138 | + await waitFor(() => { |
| 139 | + expect(screen.getByText('Set up CI/CD pipeline')).toBeInTheDocument(); |
| 140 | + }, { timeout: 5000 }); |
| 141 | + |
| 142 | + // Each event should appear exactly ONCE (no duplicates) |
| 143 | + expect(screen.getAllByText('Set up CI/CD pipeline')).toHaveLength(1); |
| 144 | + expect(screen.getAllByText('Design dashboard')).toHaveLength(1); |
| 145 | + expect(screen.getAllByText('Fix mobile responsive')).toHaveLength(1); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not show duplicate events with wrapped response format', async () => { |
| 149 | + // Test with ObjectStack response format: { data: [...], total, page, pageSize } |
| 150 | + mockDataSource.find.mockResolvedValue({ |
| 151 | + data: records, |
| 152 | + total: records.length, |
| 153 | + page: 1, |
| 154 | + pageSize: 100, |
| 155 | + hasMore: false, |
| 156 | + }); |
| 157 | + |
| 158 | + render( |
| 159 | + <SchemaRendererProvider dataSource={mockDataSource}> |
| 160 | + <ObjectView dataSource={mockDataSource} objects={mockObjects} onEdit={vi.fn()} /> |
| 161 | + </SchemaRendererProvider> |
| 162 | + ); |
| 163 | + |
| 164 | + await waitFor(() => { |
| 165 | + expect(screen.getByText('Set up CI/CD pipeline')).toBeInTheDocument(); |
| 166 | + }, { timeout: 5000 }); |
| 167 | + |
| 168 | + expect(screen.getAllByText('Set up CI/CD pipeline')).toHaveLength(1); |
| 169 | + expect(screen.getAllByText('Design dashboard')).toHaveLength(1); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('Kanban view', () => { |
| 174 | + beforeEach(() => { |
| 175 | + mockViewId = 'board'; |
| 176 | + }); |
| 177 | + |
| 178 | + it('should not show duplicate cards in kanban view', async () => { |
| 179 | + render( |
| 180 | + <SchemaRendererProvider dataSource={mockDataSource}> |
| 181 | + <ObjectView dataSource={mockDataSource} objects={mockObjects} onEdit={vi.fn()} /> |
| 182 | + </SchemaRendererProvider> |
| 183 | + ); |
| 184 | + |
| 185 | + // Wait for kanban cards to render |
| 186 | + await waitFor(() => { |
| 187 | + expect(screen.getByText('Set up CI/CD pipeline')).toBeInTheDocument(); |
| 188 | + }, { timeout: 5000 }); |
| 189 | + |
| 190 | + // Each card should appear exactly ONCE (no duplicates) |
| 191 | + expect(screen.getAllByText('Set up CI/CD pipeline')).toHaveLength(1); |
| 192 | + expect(screen.getAllByText('Design dashboard')).toHaveLength(1); |
| 193 | + expect(screen.getAllByText('Fix mobile responsive')).toHaveLength(1); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments