diff --git a/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.test.tsx b/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.test.tsx index 7a168377df..32d0d6b1b6 100644 --- a/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.test.tsx +++ b/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.test.tsx @@ -8,9 +8,9 @@ import { useSWRConfig } from 'swr'; const server = testServerSetup(); const TestComponent: FC<{ params: { project: string } }> = ({ params }) => { - const { loading, error, features, total } = useFeatureSearch(params); + const { loading, error, features, total, refetch } = + useFeatureSearch(params); const { cache } = useSWRConfig(); - if (loading) { return
Loading...
; } @@ -21,6 +21,9 @@ const TestComponent: FC<{ params: { project: string } }> = ({ params }) => { return (
+
Features: {features.map((f) => f.name).join(', ')}
Total: {total}
Cache: {[...cache.keys()]}
@@ -48,8 +51,9 @@ describe('useFeatureSearch', () => { features: [{ name: 'Feature1' }], total: 1, }); + render(); - await screen.findByText(/Features:/); + await screen.findByText(/Features: Feature1/); await screen.findByText( 'Cache: api/admin/search/features?project=project1', ); @@ -59,9 +63,34 @@ describe('useFeatureSearch', () => { total: 1, }); render(); - await screen.findByText(/Features:/); + await screen.findByText(/Features: Feature2/); await screen.findByText( 'Cache: api/admin/search/features?project=project1api/admin/search/features?project=project2', ); }); + + test('should overwrite cache total with 0 if the next result has 0 values', async () => { + const project = 'project3'; + const url = `/api/admin/search/features?project=${project}`; + testServerRoute(server, url, { + features: [{ name: 'Feature1' }], + total: 1, + }); + + const { rerender } = render(); + + await screen.findByText(/Total: 1/); + + testServerRoute(server, url, { + features: [], + total: 0, + }); + + // force fetch + const button = await screen.findByRole('button', { name: 'refetch' }); + button.click(); + + rerender(); + await screen.findByText(/Total: 0/); + }); }); diff --git a/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.ts b/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.ts index bbfff2338e..58a5f7e19e 100644 --- a/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.ts +++ b/frontend/src/hooks/api/getters/useFeatureSearch/useFeatureSearch.ts @@ -78,7 +78,7 @@ const createFeatureSearch = () => { const cacheValues = get(cacheId); - if (data?.total) { + if (data?.total !== undefined) { set(cacheId, 'total', data.total); }