|
| 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 } from 'vitest'; |
| 10 | +import { renderHook } from '@testing-library/react'; |
| 11 | +import { useMemo } from 'react'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests for the title resolution fallback chain in ObjectKanban. |
| 15 | + * |
| 16 | + * The effectiveData logic tries fields in this order: |
| 17 | + * 1. Explicit cardTitle / titleField from schema |
| 18 | + * 2. objectDef.titleFormat (e.g. "{subject}") |
| 19 | + * 3. objectDef.NAME_FIELD_KEY |
| 20 | + * 4. Fallback chain: name → title → subject → label → display_name |
| 21 | + * 5. 'Untitled' |
| 22 | + */ |
| 23 | + |
| 24 | +// Extract the title resolution logic from ObjectKanban to test it in isolation |
| 25 | +const TITLE_FALLBACK_FIELDS = ['name', 'title', 'subject', 'label', 'display_name']; |
| 26 | + |
| 27 | +function resolveTitle(item: Record<string, any>, titleField?: string): string { |
| 28 | + let resolvedTitle = titleField ? item[titleField] : undefined; |
| 29 | + |
| 30 | + if (!resolvedTitle) { |
| 31 | + for (const field of TITLE_FALLBACK_FIELDS) { |
| 32 | + if (item[field]) { |
| 33 | + resolvedTitle = item[field]; |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return resolvedTitle || 'Untitled'; |
| 40 | +} |
| 41 | + |
| 42 | +describe('ObjectKanban title resolution', () => { |
| 43 | + it('uses explicit titleField when value exists', () => { |
| 44 | + const item = { id: '1', custom_title: 'My Custom Title', name: 'Fallback Name' }; |
| 45 | + expect(resolveTitle(item, 'custom_title')).toBe('My Custom Title'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('falls back to common fields when titleField value is empty', () => { |
| 49 | + const item = { id: '1', custom_title: '', name: 'Name Field' }; |
| 50 | + expect(resolveTitle(item, 'custom_title')).toBe('Name Field'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('resolves name field first in fallback chain', () => { |
| 54 | + const item = { id: '1', name: 'Name Value', title: 'Title Value', subject: 'Subject Value' }; |
| 55 | + expect(resolveTitle(item)).toBe('Name Value'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('resolves title field second in fallback chain', () => { |
| 59 | + const item = { id: '1', title: 'Title Value', subject: 'Subject Value' }; |
| 60 | + expect(resolveTitle(item)).toBe('Title Value'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('resolves subject field third in fallback chain', () => { |
| 64 | + const item = { id: '1', subject: 'Subject Value', label: 'Label Value' }; |
| 65 | + expect(resolveTitle(item)).toBe('Subject Value'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('resolves label field fourth in fallback chain', () => { |
| 69 | + const item = { id: '1', label: 'Label Value', display_name: 'Display Name' }; |
| 70 | + expect(resolveTitle(item)).toBe('Label Value'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('resolves display_name field fifth in fallback chain', () => { |
| 74 | + const item = { id: '1', display_name: 'Display Name' }; |
| 75 | + expect(resolveTitle(item)).toBe('Display Name'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('falls back to Untitled when no common fields exist', () => { |
| 79 | + const item = { id: '1', status: 'open', priority: 'high' }; |
| 80 | + expect(resolveTitle(item)).toBe('Untitled'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('skips falsy field values in fallback chain', () => { |
| 84 | + const item = { id: '1', name: '', title: null, subject: 'Bug Report' }; |
| 85 | + expect(resolveTitle(item)).toBe('Bug Report'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('handles todo_task objects with subject field', () => { |
| 89 | + // This is the exact scenario from the bug report |
| 90 | + const todoTask = { id: '1', status: 'in_progress', subject: 'Fix login bug', priority: 'high' }; |
| 91 | + expect(resolveTitle(todoTask)).toBe('Fix login bug'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments