|
| 1 | +/** |
| 2 | + * Unit tests for Feedback search functionality in ActivityComments component |
| 3 | + * Tests search by reviewer name and feedback text with case-insensitive partial matching |
| 4 | + */ |
| 5 | + |
| 6 | +describe('ActivityComments - Feedback Search Functionality', () => { |
| 7 | + const mockFeedbacks = [ |
| 8 | + { |
| 9 | + id: 1, |
| 10 | + name: 'Sarah Johnson', |
| 11 | + text: 'This was an absolutely fantastic event!', |
| 12 | + rating: 5, |
| 13 | + createdAt: new Date('2024-01-01'), |
| 14 | + }, |
| 15 | + { |
| 16 | + id: 2, |
| 17 | + name: 'Anonymous User', |
| 18 | + text: 'Really enjoyed the event overall.', |
| 19 | + rating: 4, |
| 20 | + createdAt: new Date('2024-01-02'), |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 3, |
| 24 | + name: 'Mike Chen', |
| 25 | + text: 'The event was okay. Some parts were interesting.', |
| 26 | + rating: 3, |
| 27 | + createdAt: new Date('2024-01-03'), |
| 28 | + }, |
| 29 | + ]; |
| 30 | + |
| 31 | + // Helper function to simulate the search filter logic |
| 32 | + const filterFeedbacks = (feedbacks, searchTerm, filter = 'All') => { |
| 33 | + return feedbacks.filter(feedback => { |
| 34 | + const trimmedSearch = searchTerm.trim().toLowerCase(); |
| 35 | + let matchesSearch = true; |
| 36 | + |
| 37 | + if (trimmedSearch) { |
| 38 | + const reviewerName = (feedback.name || '').toLowerCase(); |
| 39 | + const feedbackText = (feedback.text || '').toLowerCase(); |
| 40 | + matchesSearch = |
| 41 | + reviewerName.includes(trimmedSearch) || feedbackText.includes(trimmedSearch); |
| 42 | + } |
| 43 | + |
| 44 | + const matchesFilter = filter === 'All' || feedback.rating.toString() === filter; |
| 45 | + return matchesSearch && matchesFilter; |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + // Helper function for common assertions |
| 50 | + const expectSingleResult = (results, expectedName, expectedText) => { |
| 51 | + expect(results).toHaveLength(1); |
| 52 | + if (expectedName) expect(results[0].name).toBe(expectedName); |
| 53 | + if (expectedText) expect(results[0].text).toContain(expectedText); |
| 54 | + }; |
| 55 | + |
| 56 | + describe('Search by reviewer name', () => { |
| 57 | + test.each([ |
| 58 | + ['Sarah Johnson', 'Sarah Johnson', null], |
| 59 | + ['Sarah', 'Sarah Johnson', null], |
| 60 | + ['sarah', 'Sarah Johnson', null], |
| 61 | + ['Mike', 'Mike Chen', null], |
| 62 | + ])('should find feedback for search term "%s"', (searchTerm, expectedName) => { |
| 63 | + const results = filterFeedbacks(mockFeedbacks, searchTerm); |
| 64 | + expectSingleResult(results, expectedName, null); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Search by feedback text', () => { |
| 69 | + test.each([ |
| 70 | + ['fantastic event', null, 'fantastic'], |
| 71 | + ['enjoyed', null, 'enjoyed'], |
| 72 | + ['FANTASTIC', null, 'fantastic'], |
| 73 | + ])('should find feedback for text search "%s"', (searchTerm, expectedName, expectedText) => { |
| 74 | + const results = filterFeedbacks(mockFeedbacks, searchTerm); |
| 75 | + expectSingleResult(results, expectedName, expectedText); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('Search edge cases', () => { |
| 80 | + test.each([ |
| 81 | + ['', 3], |
| 82 | + [' ', 3], |
| 83 | + ['nonexistent', 0], |
| 84 | + ])('should return %d results for search term "%s"', (searchTerm, expectedCount) => { |
| 85 | + const results = filterFeedbacks(mockFeedbacks, searchTerm); |
| 86 | + expect(results).toHaveLength(expectedCount); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should handle null or undefined name gracefully', () => { |
| 90 | + const feedbackWithNullName = { |
| 91 | + id: 4, |
| 92 | + name: null, |
| 93 | + text: 'Some feedback text', |
| 94 | + rating: 5, |
| 95 | + createdAt: new Date('2024-01-04'), |
| 96 | + }; |
| 97 | + const results = filterFeedbacks([feedbackWithNullName], 'text'); |
| 98 | + expect(results).toHaveLength(1); |
| 99 | + }); |
| 100 | + |
| 101 | + test('should handle null or undefined text gracefully', () => { |
| 102 | + const feedbackWithNullText = { |
| 103 | + id: 5, |
| 104 | + name: 'John Doe', |
| 105 | + text: null, |
| 106 | + rating: 5, |
| 107 | + createdAt: new Date('2024-01-05'), |
| 108 | + }; |
| 109 | + const results = filterFeedbacks([feedbackWithNullText], 'John'); |
| 110 | + expect(results).toHaveLength(1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('Search with rating filter', () => { |
| 115 | + test.each([ |
| 116 | + ['event', '5', 1, 5], |
| 117 | + ['Sarah', '1', 0, null], |
| 118 | + ])( |
| 119 | + 'should filter by rating %s and search "%s" to return %d results', |
| 120 | + (searchTerm, rating, expectedCount, expectedRating) => { |
| 121 | + const results = filterFeedbacks(mockFeedbacks, searchTerm, rating); |
| 122 | + expect(results).toHaveLength(expectedCount); |
| 123 | + if (expectedRating) expect(results[0].rating).toBe(expectedRating); |
| 124 | + }, |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('Partial matching', () => { |
| 129 | + test.each([ |
| 130 | + ['John', 'name', 'Johnson'], |
| 131 | + ['absolutely', 'text', 'absolutely'], |
| 132 | + ])('should match partial words in %s', (searchTerm, field, expectedContent) => { |
| 133 | + const results = filterFeedbacks(mockFeedbacks, searchTerm); |
| 134 | + expect(results).toHaveLength(1); |
| 135 | + expect(results[0][field]).toContain(expectedContent); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments