Skip to content

Commit a3dcc4d

Browse files
authored
test(useRecentSearches): verify clearSearches removes the localStorage key (JhaSourav07#802)
## Description Fixes JhaSourav07#717 This PR improves test coverage for the useRecentSearches hook by verifying that clearSearches correctly removes the persisted localStorage entry. ● Changes Made Added a spy for localStorage.removeItem Verified clearSearches() calls: removeItem('recentSearches') Preserved all existing hook test behavior ## Pillar Testing / Quality Assurance ## Visual Preview <img width="1920" height="1080" alt="Screenshot 2026-05-28 004327" src="https://github.com/user-attachments/assets/3018699d-abb8-4466-aefa-041ce22fe584" /> <img width="1920" height="1080" alt="Screenshot 2026-05-28 004343" src="https://github.com/user-attachments/assets/cf412082-6cd3-45c0-8b29-d3daf4d18844" /> <img width="1920" height="1080" alt="Screenshot 2026-05-28 004419" src="https://github.com/user-attachments/assets/c465f5db-5fab-4c93-a144-f0af325c0893" /> ● Why This Matters This ensures: localStorage cleanup works correctly stale recent search data is removed properly future regressions in persistence behavior are caught automatically ## Checklist before requesting a review: - I have read the `CONTRIBUTING.md` file. - I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - I have updated `README.md` if I added a new theme or URL parameter. - I have started the repo. - I have made sure that i have only one commit to merge in this PR. - The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
1 parent d881686 commit a3dcc4d

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

app/(root)/dashboard/[username]/page.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('DashboardPage', () => {
129129
expect(screen.getByTestId('heatmap')).toBeDefined();
130130
expect(screen.getByTestId('ai-insights')).toBeDefined();
131131
expect(screen.getByTestId('achievements')).toBeDefined();
132-
132+
expect(screen.getAllByTestId('stats-card')).toHaveLength(3);
133133
expect(screen.getByText('Current Streak: 5')).toBeDefined();
134134
expect(screen.getByText('Peak Streak: 15')).toBeDefined();
135135
expect(screen.getByText('Contributions: 500')).toBeDefined();

hooks/useRecentSearches.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, it, expect, beforeEach } from 'vitest';
1+
import { describe, it, expect, beforeEach, vi } from 'vitest';
22
import { renderHook, act } from '@testing-library/react';
3-
import { useRecentSearches, MAX_SEARCHES } from './useRecentSearches';
3+
import { useRecentSearches, MAX_SEARCHES, STORAGE_KEY } from './useRecentSearches';
44

55
const store: Record<string, string> = {};
66

@@ -62,15 +62,21 @@ describe('useRecentSearches', () => {
6262
expect(result.current.searches.length).toBe(MAX_SEARCHES);
6363
});
6464

65-
it('clears all searches', () => {
65+
it('clears all searches and removes localStorage key', () => {
66+
const removeItemSpy = vi.spyOn(window.localStorage, 'removeItem');
67+
6668
const { result } = renderHook(() => useRecentSearches());
69+
6770
act(() => {
6871
result.current.addSearch('torvalds');
6972
});
73+
7074
act(() => {
7175
result.current.clearSearches();
7276
});
77+
7378
expect(result.current.searches).toEqual([]);
79+
expect(removeItemSpy).toHaveBeenCalledWith(STORAGE_KEY);
7480
});
7581

7682
it('persists searches across remounts', () => {

hooks/useRecentSearches.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ export function useRecentSearches() {
7474
* Clears all recent searches from state and localStorage.
7575
*/
7676
const clearSearches = () => {
77-
setState((prev) => ({ ...prev, searches: [] }));
7877
writeStorage(null);
78+
79+
setState((prev) => ({
80+
...prev,
81+
searches: [],
82+
}));
7983
};
8084

8185
// Return empty searches until after hydration to prevent SSR/client mismatch.

0 commit comments

Comments
 (0)