Skip to content

Commit 69e7147

Browse files
committed
chore(View All > useSearch): Fix unit tests
1 parent e9ebfa8 commit 69e7147

File tree

8 files changed

+34
-22
lines changed

8 files changed

+34
-22
lines changed

app/global.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ html {
1111
--gridPx: 4px;
1212
--grid2Px: calc(2 * 4px);
1313
}
14+
15+
.mapboxgl-control-container {
16+
display: none;
17+
}

src/components/Album/AlbumClient.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ function AlbumClient({ items = [], meta, indexedKeywords, clusteredMarkers }: Al
5454
if (idx >= 0 && refImageGallery.current?.getCurrentIndex?.() !== idx) {
5555
refImageGallery.current?.slideToIndex(idx)
5656
setMemoryIndex(idx)
57+
setViewed(idx)
5758
}
58-
}, [selectId, itemsToShow, refImageGallery, setMemoryIndex])
59+
}, [selectId, itemsToShow, refImageGallery, setMemoryIndex, setViewed])
5960

6061
return (
6162
<div>

src/components/All/AllClient.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export default function AllClient({ items, indexedKeywords, clusteredMarkers }:
4949
if (idx >= 0 && refImageGallery.current?.getCurrentIndex?.() !== idx) {
5050
refImageGallery.current?.slideToIndex(idx)
5151
setMemoryIndex(idx)
52+
setViewed(idx)
5253
}
53-
}, [selectId, itemsToShow, refImageGallery, setMemoryIndex])
54+
}, [selectId, itemsToShow, refImageGallery, setMemoryIndex, setViewed])
5455

5556
return (
5657
<div>

src/components/Persons/PersonsClient.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ export default function PersonsClient({
5555
if (idx >= 0 && refImageGallery.current?.getCurrentIndex?.() !== idx) {
5656
refImageGallery.current?.slideToIndex(idx)
5757
setMemoryIndex(idx)
58+
setViewed(idx)
5859
}
59-
}, [selectId, ageFiltered, refImageGallery, setMemoryIndex])
60+
}, [selectId, ageFiltered, refImageGallery, setMemoryIndex, setViewed])
6061

6162
// Replace controls age list if override available
6263
const finalControls = overrideAgeSummary ?? controls

src/components/SlippyMap/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ export default function SlippyMap({
179179

180180
return (
181181
<>
182-
<style global jsx>{'.mapboxgl-control-container{display:none;}'}</style>
183182
<div style={{ position: 'relative', width: '100%', height: '100%', minHeight: 300 }}>
184183
<button
185184
type="button"

src/hooks/__tests__/useMemoryAndMapFilter.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jest.mock('../useSearch', () => ({
1010
keyword: '',
1111
searchBox: <div data-testid="search-box" />,
1212
setVisibleCount: jest.fn(),
13+
setDisplayedItems: jest.fn(),
1314
}),
1415
}))
1516

src/hooks/__tests__/useSearch.vitest.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ describe('Clear button functionality', () => {
269269

270270
vi.mocked(usePathname).mockReturnValue('/search')
271271

272-
const items = [{ corpus: 'apple' }, { corpus: 'banana' }, { corpus: 'cherry' }]
272+
const items = [
273+
{ corpus: 'apple', filename: 'apple.jpg' },
274+
{ corpus: 'banana', filename: 'banana.jpg' },
275+
{ corpus: 'cherry', filename: 'cherry.jpg' },
276+
]
273277

274278
// Use a wrapper component to access the hook
275279
function TestComponent() {
@@ -283,7 +287,7 @@ describe('Clear button functionality', () => {
283287
expect(container.textContent).toMatch(/for "apple"/)
284288

285289
// Find the Clear button by title attribute
286-
const clearButton = container.querySelector('button[title="Clear search"]') as HTMLButtonElement
290+
const clearButton = container.querySelector('button[title="Clear search and view adjacent photos"]') as HTMLButtonElement
287291

288292
// Mock the search params to return empty after clearing
289293
vi.mocked(useSearchParams).mockReturnValue({
@@ -293,7 +297,7 @@ describe('Clear button functionality', () => {
293297
fireEvent.click(clearButton)
294298

295299
// Verify router.replace was called
296-
expect(mockReplace).toHaveBeenCalledWith('/search')
300+
expect(mockReplace).toHaveBeenCalledWith('/search?select=apple.jpg')
297301

298302
// Check that the input field value is cleared
299303
await waitFor(() => {
@@ -323,21 +327,22 @@ describe('Clear button functionality', () => {
323327
vi.mocked(usePathname).mockReturnValue('/gallery/photos')
324328

325329
const items = [
326-
{ corpus: 'apple' },
327-
{ corpus: 'banana' },
328-
{ corpus: 'cherry' },
329-
{ corpus: 'banana split' },
330+
{ corpus: 'apple', filename: 'apple.jpg' },
331+
{ corpus: 'banana', filename: 'banana.jpg' },
332+
{ corpus: 'cherry', filename: 'cherry.jpg' },
333+
{ corpus: 'banana split', filename: 'banana-split.jpg' },
330334
]
331335

332336
// Use a wrapper component to access the hook and set visible count
333337
function TestComponent() {
334338
const search = useSearch({ items, indexedKeywords: [] })
335339

336-
// Simulate setting visible count based on filtered results
337-
const { filtered, setVisibleCount } = search
340+
// Simulate setting visible count and displayed items based on filtered results
341+
const { filtered, setVisibleCount, setDisplayedItems } = search
338342
React.useEffect(() => {
339343
setVisibleCount(filtered.length)
340-
}, [filtered, setVisibleCount])
344+
setDisplayedItems(filtered)
345+
}, [filtered, setVisibleCount, setDisplayedItems])
341346

342347
return <div>{search.searchBox}</div>
343348
}
@@ -353,7 +358,7 @@ describe('Clear button functionality', () => {
353358
expect(container.textContent).toMatch(/for "ban"/)
354359

355360
// Find the Clear button
356-
const clearButton = container.querySelector('button[title="Clear search"]') as HTMLButtonElement
361+
const clearButton = container.querySelector('button[title="Clear search and view adjacent photos"]') as HTMLButtonElement
357362

358363
// Mock the search params to return empty after clearing
359364
vi.mocked(useSearchParams).mockReturnValue({
@@ -363,7 +368,7 @@ describe('Clear button functionality', () => {
363368
fireEvent.click(clearButton)
364369

365370
// Verify URL was cleared (router.replace called with path only, no keyword)
366-
expect(mockReplace).toHaveBeenCalledWith('/gallery/photos')
371+
expect(mockReplace).toHaveBeenCalledWith('/gallery/photos?select=banana.jpg')
367372

368373
// Re-render to reflect the cleared state
369374
rerender(<TestComponent />)

src/hooks/useSearch.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ export default function useSearch<ItemType extends ServerSideItem>({
4848
const [inputValue, setInputValue] = useState<string>(initialKeyword)
4949
const [displayedItems, setDisplayedItems] = useState<ItemType[]>(items)
5050

51+
const filtered = useMemo(() => {
52+
if (!keyword) return items
53+
return items.filter((item) => matchCorpus(item.corpus, keyword))
54+
}, [items, keyword])
55+
5156
// Count of currently visible thumbnails (consumer can override this if needed)
52-
const [visibleCount, setVisibleCount] = useState<number>(items.length)
57+
const [visibleCount, setVisibleCount] = useState<number>(filtered.length)
5358

5459
// Make setVisibleCount stable to prevent useEffect loops
5560
const setVisibleCountStable = useCallback((count: number) => {
5661
setVisibleCount((prev) => (prev === count ? prev : count))
5762
}, [])
5863

59-
const filtered = useMemo(() => {
60-
if (!keyword) return items
61-
return items.filter((item) => matchCorpus(item.corpus, keyword))
62-
}, [items, keyword])
63-
6464
// Adjust visibleCount when filtered items change during render
6565
// This avoids a double render caused by useEffect synchronization
6666
const [prevFiltered, setPrevFiltered] = useState(filtered)

0 commit comments

Comments
 (0)