|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { ViewSwitcher, findKanbanField } from '@/components/objectui/ViewSwitcher'; |
| 4 | +import type { ObjectDefinition } from '@/types/metadata'; |
| 5 | +import { vi } from 'vitest'; |
| 6 | + |
| 7 | +const taskObjectDef: ObjectDefinition = { |
| 8 | + name: 'task', |
| 9 | + label: 'Task', |
| 10 | + fields: { |
| 11 | + id: { type: 'text', label: 'ID', readonly: true }, |
| 12 | + title: { type: 'text', label: 'Title' }, |
| 13 | + status: { |
| 14 | + type: 'select', |
| 15 | + label: 'Status', |
| 16 | + options: [ |
| 17 | + { label: 'To Do', value: 'todo' }, |
| 18 | + { label: 'In Progress', value: 'in_progress' }, |
| 19 | + { label: 'Done', value: 'done' }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + due_date: { type: 'datetime', label: 'Due Date' }, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +const noSelectObjectDef: ObjectDefinition = { |
| 27 | + name: 'note', |
| 28 | + label: 'Note', |
| 29 | + fields: { |
| 30 | + id: { type: 'text', label: 'ID', readonly: true }, |
| 31 | + content: { type: 'textarea', label: 'Content' }, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +describe('findKanbanField', () => { |
| 36 | + it('returns status field for object with select fields', () => { |
| 37 | + expect(findKanbanField(taskObjectDef)).toBe('status'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns undefined for object without select fields', () => { |
| 41 | + expect(findKanbanField(noSelectObjectDef)).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('prefers status/stage/state/priority names', () => { |
| 45 | + const objectDef: ObjectDefinition = { |
| 46 | + name: 'test', |
| 47 | + label: 'Test', |
| 48 | + fields: { |
| 49 | + category: { |
| 50 | + type: 'select', |
| 51 | + label: 'Category', |
| 52 | + options: [{ label: 'A', value: 'a' }, { label: 'B', value: 'b' }], |
| 53 | + }, |
| 54 | + priority: { |
| 55 | + type: 'select', |
| 56 | + label: 'Priority', |
| 57 | + options: [{ label: 'Low', value: 'low' }, { label: 'High', value: 'high' }], |
| 58 | + }, |
| 59 | + }, |
| 60 | + }; |
| 61 | + expect(findKanbanField(objectDef)).toBe('priority'); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('ViewSwitcher', () => { |
| 66 | + it('renders all three view buttons', () => { |
| 67 | + render(<ViewSwitcher currentView="table" onViewChange={() => {}} />); |
| 68 | + // Three buttons should exist |
| 69 | + const buttons = screen.getAllByRole('button'); |
| 70 | + expect(buttons.length).toBe(3); |
| 71 | + }); |
| 72 | + |
| 73 | + it('calls onViewChange when a button is clicked', () => { |
| 74 | + const onViewChange = vi.fn(); |
| 75 | + render( |
| 76 | + <ViewSwitcher |
| 77 | + currentView="table" |
| 78 | + onViewChange={onViewChange} |
| 79 | + objectDef={taskObjectDef} |
| 80 | + />, |
| 81 | + ); |
| 82 | + // Click the kanban button (second button) |
| 83 | + const buttons = screen.getAllByRole('button'); |
| 84 | + fireEvent.click(buttons[1]); // Kanban |
| 85 | + expect(onViewChange).toHaveBeenCalledWith('kanban'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('disables kanban when no select field available', () => { |
| 89 | + render( |
| 90 | + <ViewSwitcher |
| 91 | + currentView="table" |
| 92 | + onViewChange={() => {}} |
| 93 | + objectDef={noSelectObjectDef} |
| 94 | + />, |
| 95 | + ); |
| 96 | + const buttons = screen.getAllByRole('button'); |
| 97 | + // Kanban button should be disabled |
| 98 | + expect(buttons[1].hasAttribute('disabled')).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('enables kanban when select field is available', () => { |
| 102 | + render( |
| 103 | + <ViewSwitcher |
| 104 | + currentView="table" |
| 105 | + onViewChange={() => {}} |
| 106 | + objectDef={taskObjectDef} |
| 107 | + />, |
| 108 | + ); |
| 109 | + const buttons = screen.getAllByRole('button'); |
| 110 | + // Kanban button should NOT be disabled |
| 111 | + expect(buttons[1].hasAttribute('disabled')).toBe(false); |
| 112 | + }); |
| 113 | +}); |
0 commit comments