1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

fix: update cache, even when the total is 0 (#7582)

This PR fixes a bug where we wouldn't update the `useFeatureSearch`
hook's cached `total` value if the new total was `0`. The reason this
failed is that we would only update it if `data?.total`. Because `0` is
a falsy value, the check would fail.
This commit is contained in:
Thomas Heartman 2024-07-12 14:47:50 +02:00 committed by GitHub
parent e7627becec
commit 9d7eec5951
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 5 deletions

View File

@ -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 <div>Loading...</div>;
}
@ -21,6 +21,9 @@ const TestComponent: FC<{ params: { project: string } }> = ({ params }) => {
return (
<div>
<button type='button' onClick={refetch}>
refetch
</button>
<div>Features: {features.map((f) => f.name).join(', ')}</div>
<div>Total: {total}</div>
<div>Cache: {[...cache.keys()]}</div>
@ -48,8 +51,9 @@ describe('useFeatureSearch', () => {
features: [{ name: 'Feature1' }],
total: 1,
});
render(<TestComponent params={{ project: 'project1' }} />);
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(<TestComponent params={{ project: 'project2' }} />);
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(<TestComponent params={{ project }} />);
await screen.findByText(/Total: 1/);
testServerRoute(server, url, {
features: [],
total: 0,
});
// force fetch
const button = await screen.findByRole('button', { name: 'refetch' });
button.click();
rerender(<TestComponent params={{ project }} />);
await screen.findByText(/Total: 0/);
});
});

View File

@ -78,7 +78,7 @@ const createFeatureSearch = () => {
const cacheValues = get(cacheId);
if (data?.total) {
if (data?.total !== undefined) {
set(cacheId, 'total', data.total);
}