|
| 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 } from 'vitest'; |
| 10 | +import { renderHook, act } from '@testing-library/react'; |
| 11 | +import { useCommentSearch } from '../useCommentSearch'; |
| 12 | +import type { CommentEntry } from '@object-ui/types'; |
| 13 | + |
| 14 | +const mockComments: CommentEntry[] = [ |
| 15 | + { |
| 16 | + id: '1', |
| 17 | + text: 'Great progress on the dashboard feature', |
| 18 | + author: 'Alice', |
| 19 | + createdAt: '2026-02-20T10:00:00Z', |
| 20 | + objectName: 'Project', |
| 21 | + recordId: 'p1', |
| 22 | + }, |
| 23 | + { |
| 24 | + id: '2', |
| 25 | + text: 'Need to fix the bug in the login page', |
| 26 | + author: 'Bob', |
| 27 | + createdAt: '2026-02-20T11:00:00Z', |
| 28 | + objectName: 'Task', |
| 29 | + recordId: 't1', |
| 30 | + }, |
| 31 | + { |
| 32 | + id: '3', |
| 33 | + text: 'Dashboard design looks good', |
| 34 | + author: 'Charlie', |
| 35 | + createdAt: '2026-02-20T12:00:00Z', |
| 36 | + objectName: 'Project', |
| 37 | + recordId: 'p2', |
| 38 | + }, |
| 39 | + { |
| 40 | + id: '4', |
| 41 | + text: 'Alice approved the PR', |
| 42 | + author: 'Diana', |
| 43 | + createdAt: '2026-02-20T13:00:00Z', |
| 44 | + objectName: 'Review', |
| 45 | + recordId: 'r1', |
| 46 | + }, |
| 47 | +]; |
| 48 | + |
| 49 | +describe('useCommentSearch', () => { |
| 50 | + it('returns empty results when query is empty', () => { |
| 51 | + const { result } = renderHook(() => |
| 52 | + useCommentSearch({ comments: mockComments }), |
| 53 | + ); |
| 54 | + expect(result.current.results).toEqual([]); |
| 55 | + expect(result.current.isSearching).toBe(false); |
| 56 | + expect(result.current.query).toBe(''); |
| 57 | + }); |
| 58 | + |
| 59 | + it('searches by comment text', () => { |
| 60 | + const { result } = renderHook(() => |
| 61 | + useCommentSearch({ comments: mockComments }), |
| 62 | + ); |
| 63 | + |
| 64 | + act(() => { |
| 65 | + result.current.setQuery('dashboard'); |
| 66 | + }); |
| 67 | + |
| 68 | + expect(result.current.isSearching).toBe(true); |
| 69 | + expect(result.current.results).toHaveLength(2); |
| 70 | + expect(result.current.results.map(r => r.comment.id)).toEqual(['1', '3']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('searches by author name', () => { |
| 74 | + const { result } = renderHook(() => |
| 75 | + useCommentSearch({ comments: mockComments }), |
| 76 | + ); |
| 77 | + |
| 78 | + act(() => { |
| 79 | + result.current.setQuery('Alice'); |
| 80 | + }); |
| 81 | + |
| 82 | + // Comment 4 mentions "Alice" in its text, and Comment 1 has author "Alice" |
| 83 | + expect(result.current.results).toHaveLength(2); |
| 84 | + const ids = result.current.results.map(r => r.comment.id); |
| 85 | + expect(ids).toContain('1'); |
| 86 | + expect(ids).toContain('4'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('is case-insensitive', () => { |
| 90 | + const { result } = renderHook(() => |
| 91 | + useCommentSearch({ comments: mockComments }), |
| 92 | + ); |
| 93 | + |
| 94 | + act(() => { |
| 95 | + result.current.setQuery('BUG'); |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.current.results).toHaveLength(1); |
| 99 | + expect(result.current.results[0].comment.id).toBe('2'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns results with objectName and recordId', () => { |
| 103 | + const { result } = renderHook(() => |
| 104 | + useCommentSearch({ comments: mockComments }), |
| 105 | + ); |
| 106 | + |
| 107 | + act(() => { |
| 108 | + result.current.setQuery('login'); |
| 109 | + }); |
| 110 | + |
| 111 | + expect(result.current.results).toHaveLength(1); |
| 112 | + expect(result.current.results[0].objectName).toBe('Task'); |
| 113 | + expect(result.current.results[0].recordId).toBe('t1'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('provides highlighted text snippets', () => { |
| 117 | + const { result } = renderHook(() => |
| 118 | + useCommentSearch({ comments: mockComments }), |
| 119 | + ); |
| 120 | + |
| 121 | + act(() => { |
| 122 | + result.current.setQuery('bug'); |
| 123 | + }); |
| 124 | + |
| 125 | + expect(result.current.results[0].highlight).toBeDefined(); |
| 126 | + expect(result.current.results[0].highlight).toContain('bug'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('clears search results', () => { |
| 130 | + const { result } = renderHook(() => |
| 131 | + useCommentSearch({ comments: mockComments }), |
| 132 | + ); |
| 133 | + |
| 134 | + act(() => { |
| 135 | + result.current.setQuery('dashboard'); |
| 136 | + }); |
| 137 | + expect(result.current.results).toHaveLength(2); |
| 138 | + |
| 139 | + act(() => { |
| 140 | + result.current.clearSearch(); |
| 141 | + }); |
| 142 | + expect(result.current.results).toEqual([]); |
| 143 | + expect(result.current.query).toBe(''); |
| 144 | + expect(result.current.isSearching).toBe(false); |
| 145 | + }); |
| 146 | + |
| 147 | + it('returns empty results for non-matching query', () => { |
| 148 | + const { result } = renderHook(() => |
| 149 | + useCommentSearch({ comments: mockComments }), |
| 150 | + ); |
| 151 | + |
| 152 | + act(() => { |
| 153 | + result.current.setQuery('zzzznonexistent'); |
| 154 | + }); |
| 155 | + |
| 156 | + expect(result.current.results).toEqual([]); |
| 157 | + expect(result.current.isSearching).toBe(true); |
| 158 | + }); |
| 159 | +}); |
0 commit comments