Skip to content

Commit 84bd933

Browse files
committed
Add comprehensive unit tests for feedback search functionality
1 parent c88b21c commit 84bd933

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
describe('Search by reviewer name', () => {
50+
test('should find feedback by exact reviewer name match', () => {
51+
const results = filterFeedbacks(mockFeedbacks, 'Sarah Johnson');
52+
expect(results).toHaveLength(1);
53+
expect(results[0].name).toBe('Sarah Johnson');
54+
});
55+
56+
test('should find feedback by partial reviewer name match', () => {
57+
const results = filterFeedbacks(mockFeedbacks, 'Sarah');
58+
expect(results).toHaveLength(1);
59+
expect(results[0].name).toBe('Sarah Johnson');
60+
});
61+
62+
test('should be case-insensitive when searching by name', () => {
63+
const results = filterFeedbacks(mockFeedbacks, 'sarah');
64+
expect(results).toHaveLength(1);
65+
expect(results[0].name).toBe('Sarah Johnson');
66+
});
67+
68+
test('should find multiple results for partial name match', () => {
69+
const results = filterFeedbacks(mockFeedbacks, 'Mike');
70+
expect(results).toHaveLength(1);
71+
expect(results[0].name).toBe('Mike Chen');
72+
});
73+
});
74+
75+
describe('Search by feedback text', () => {
76+
test('should find feedback by exact text match', () => {
77+
const results = filterFeedbacks(mockFeedbacks, 'fantastic event');
78+
expect(results).toHaveLength(1);
79+
expect(results[0].text).toContain('fantastic');
80+
});
81+
82+
test('should find feedback by partial text match', () => {
83+
const results = filterFeedbacks(mockFeedbacks, 'enjoyed');
84+
expect(results).toHaveLength(1);
85+
expect(results[0].text).toContain('enjoyed');
86+
});
87+
88+
test('should be case-insensitive when searching by text', () => {
89+
const results = filterFeedbacks(mockFeedbacks, 'FANTASTIC');
90+
expect(results).toHaveLength(1);
91+
expect(results[0].text).toContain('fantastic');
92+
});
93+
});
94+
95+
describe('Search edge cases', () => {
96+
test('should return all feedbacks when search term is empty', () => {
97+
const results = filterFeedbacks(mockFeedbacks, '');
98+
expect(results).toHaveLength(3);
99+
});
100+
101+
test('should return all feedbacks when search term is only whitespace', () => {
102+
const results = filterFeedbacks(mockFeedbacks, ' ');
103+
expect(results).toHaveLength(3);
104+
});
105+
106+
test('should handle null or undefined name gracefully', () => {
107+
const feedbackWithNullName = {
108+
id: 4,
109+
name: null,
110+
text: 'Some feedback text',
111+
rating: 5,
112+
createdAt: new Date('2024-01-04'),
113+
};
114+
const results = filterFeedbacks([feedbackWithNullName], 'text');
115+
expect(results).toHaveLength(1);
116+
});
117+
118+
test('should handle null or undefined text gracefully', () => {
119+
const feedbackWithNullText = {
120+
id: 5,
121+
name: 'John Doe',
122+
text: null,
123+
rating: 5,
124+
createdAt: new Date('2024-01-05'),
125+
};
126+
const results = filterFeedbacks([feedbackWithNullText], 'John');
127+
expect(results).toHaveLength(1);
128+
});
129+
130+
test('should return empty array when no matches found', () => {
131+
const results = filterFeedbacks(mockFeedbacks, 'nonexistent');
132+
expect(results).toHaveLength(0);
133+
});
134+
});
135+
136+
describe('Search with rating filter', () => {
137+
test('should filter by rating and search term together', () => {
138+
const results = filterFeedbacks(mockFeedbacks, 'event', '5');
139+
expect(results).toHaveLength(1);
140+
expect(results[0].rating).toBe(5);
141+
});
142+
143+
test('should return empty when search matches but rating filter does not', () => {
144+
const results = filterFeedbacks(mockFeedbacks, 'Sarah', '1');
145+
expect(results).toHaveLength(0);
146+
});
147+
});
148+
149+
describe('Partial matching', () => {
150+
test('should match partial words in reviewer name', () => {
151+
const results = filterFeedbacks(mockFeedbacks, 'John');
152+
expect(results).toHaveLength(1);
153+
expect(results[0].name).toContain('Johnson');
154+
});
155+
156+
test('should match partial words in feedback text', () => {
157+
const results = filterFeedbacks(mockFeedbacks, 'absolutely');
158+
expect(results).toHaveLength(1);
159+
expect(results[0].text).toContain('absolutely');
160+
});
161+
});
162+
});

0 commit comments

Comments
 (0)