Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update internal caching #2085

Merged
merged 10 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/identity/hooks/useAddress.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,69 @@ describe('useAddress', () => {
});
expect(getChainPublicClient).toHaveBeenCalledWith(baseSepolia);
});

it('respects the enabled option in queryOptions', async () => {
const testEnsName = 'test.ens';
const testEnsAddress = '0x1234';

// Mock the getEnsAddress method of the publicClient
mockGetEnsAddress.mockResolvedValue(testEnsAddress);

// Use the renderHook function to create a test harness for the useAddress hook with enabled: false
const { result } = renderHook(
() => useAddress({ name: testEnsName }, { enabled: false }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// The query should not be executed
expect(result.current.isLoading).toBe(false);
expect(result.current.isFetched).toBe(false);
expect(mockGetEnsAddress).not.toHaveBeenCalled();
});

it('uses the default query options when no queryOptions are provided', async () => {
const testEnsName = 'test.ens';
const testEnsAddress = '0x1234';

// Mock the getEnsAddress method of the publicClient
mockGetEnsAddress.mockResolvedValue(testEnsAddress);

// Use the renderHook function to create a test harness for the useAddress hook
renderHook(() => useAddress({ name: testEnsName }), {
wrapper: getNewReactQueryTestProvider(),
});

// Wait for the hook to finish fetching the ENS address
await waitFor(() => {
// Check that the default query options were used
expect(mockGetEnsAddress).toHaveBeenCalled();
});
});

it('merges custom queryOptions with default options', async () => {
const testEnsName = 'test.ens';
const testEnsAddress = '0x1234';
const customStaleTime = 60000; // 1 minute

// Mock the getEnsAddress method of the publicClient
mockGetEnsAddress.mockResolvedValue(testEnsAddress);

// Use the renderHook function to create a test harness for the useAddress hook with custom staleTime
const { result } = renderHook(
() => useAddress({ name: testEnsName }, { staleTime: customStaleTime }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the ENS address
await waitFor(() => {
expect(result.current.data).toBe(testEnsAddress);
});

// The query should be executed with the custom staleTime
expect(mockGetEnsAddress).toHaveBeenCalled();
});
});
65 changes: 65 additions & 0 deletions src/identity/hooks/useAvatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,69 @@ describe('useAvatar', () => {
);
});
});

it('respects the enabled option in queryOptions', async () => {
const testEnsName = 'test.ens';
const testEnsAvatar = 'avatarUrl';

// Mock the getEnsAvatar method of the publicClient
mockGetEnsAvatar.mockResolvedValue(testEnsAvatar);

// Use the renderHook function to create a test harness for the useAvatar hook with enabled: false
const { result } = renderHook(
() => useAvatar({ ensName: testEnsName }, { enabled: false }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// The query should not be executed
expect(result.current.isLoading).toBe(false);
expect(result.current.isFetched).toBe(false);
expect(mockGetEnsAvatar).not.toHaveBeenCalled();
});

it('uses the default query options when no queryOptions are provided', async () => {
const testEnsName = 'test.ens';
const testEnsAvatar = 'avatarUrl';

// Mock the getEnsAvatar method of the publicClient
mockGetEnsAvatar.mockResolvedValue(testEnsAvatar);

// Use the renderHook function to create a test harness for the useAvatar hook
renderHook(() => useAvatar({ ensName: testEnsName }), {
wrapper: getNewReactQueryTestProvider(),
});

// Wait for the hook to finish fetching the ENS avatar
await waitFor(() => {
// Check that the default query options were used
expect(mockGetEnsAvatar).toHaveBeenCalled();
});
});

it('merges custom queryOptions with default options', async () => {
const testEnsName = 'test.ens';
const testEnsAvatar = 'avatarUrl';
const customStaleTime = 60000; // 1 minute

// Mock the getEnsAvatar method of the publicClient
mockGetEnsAvatar.mockResolvedValue(testEnsAvatar);

// Use the renderHook function to create a test harness for the useAvatar hook with custom staleTime
const { result } = renderHook(
() => useAvatar({ ensName: testEnsName }, { staleTime: customStaleTime }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the ENS avatar
await waitFor(() => {
expect(result.current.data).toBe(testEnsAvatar);
});

// The query should be executed with the custom staleTime
expect(mockGetEnsAvatar).toHaveBeenCalled();
});
});
18 changes: 12 additions & 6 deletions src/identity/hooks/useAvatar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getAvatar } from '@/identity/utils/getAvatar';
import { DEFAULT_QUERY_OPTIONS } from '@/internal/constants';
import { useQuery } from '@tanstack/react-query';
import { mainnet } from 'viem/chains';
import type {
Expand All @@ -14,14 +15,19 @@ export const useAvatar = (
{ ensName, chain = mainnet }: UseAvatarOptions,
queryOptions?: UseQueryOptions,
) => {
const { enabled = true, cacheTime } = queryOptions ?? {};
const { enabled, cacheTime, staleTime, refetchOnWindowFocus } = {
...DEFAULT_QUERY_OPTIONS,
...queryOptions,
};

const queryKey = ['useAvatar', ensName, chain.id];

return useQuery<GetAvatarReturnType>({
queryKey: ['useAvatar', ensName, chain.id],
queryFn: async () => {
return getAvatar({ ensName, chain });
},
queryKey,
queryFn: () => getAvatar({ ensName, chain }),
gcTime: cacheTime,
staleTime: staleTime,
enabled,
refetchOnWindowFocus: false,
refetchOnWindowFocus,
});
};
62 changes: 62 additions & 0 deletions src/identity/hooks/useName.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,66 @@ describe('useName', () => {
);
});
});

it('respects the enabled option in queryOptions', async () => {
const testEnsName = 'test.ens';

// Mock the getEnsName method of the publicClient
mockGetEnsName.mockResolvedValue(testEnsName);

// Use the renderHook function to create a test harness for the useName hook with enabled: false
const { result } = renderHook(
() => useName({ address: testAddress }, { enabled: false }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// The query should not be executed
expect(result.current.isLoading).toBe(false);
expect(result.current.isFetched).toBe(false);
expect(mockGetEnsName).not.toHaveBeenCalled();
});

it('uses the default query options when no queryOptions are provided', async () => {
const testEnsName = 'test.ens';

// Mock the getEnsName method of the publicClient
mockGetEnsName.mockResolvedValue(testEnsName);

// Use the renderHook function to create a test harness for the useName hook
renderHook(() => useName({ address: testAddress }), {
wrapper: getNewReactQueryTestProvider(),
});

// Wait for the hook to finish fetching the ENS name
await waitFor(() => {
// Check that the default query options were used
expect(mockGetEnsName).toHaveBeenCalled();
});
});

it('merges custom queryOptions with default options', async () => {
const testEnsName = 'test.ens';
const customCacheTime = 120000; // 2 minutes

// Mock the getEnsName method of the publicClient
mockGetEnsName.mockResolvedValue(testEnsName);

// Use the renderHook function to create a test harness for the useName hook with custom cacheTime
const { result } = renderHook(
() => useName({ address: testAddress }, { cacheTime: customCacheTime }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the ENS name
await waitFor(() => {
expect(result.current.data).toBe(testEnsName);
});

// The query should be executed with the custom cacheTime
expect(mockGetEnsName).toHaveBeenCalled();
});
});
18 changes: 12 additions & 6 deletions src/identity/hooks/useName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getName } from '@/identity/utils/getName';
import { DEFAULT_QUERY_OPTIONS } from '@/internal/constants';
import { useQuery } from '@tanstack/react-query';
import { mainnet } from 'viem/chains';
import type {
Expand All @@ -17,14 +18,19 @@ export const useName = (
{ address, chain = mainnet }: UseNameOptions,
queryOptions?: UseQueryOptions,
) => {
const { enabled = true, cacheTime } = queryOptions ?? {};
const { enabled, cacheTime, staleTime, refetchOnWindowFocus } = {
...DEFAULT_QUERY_OPTIONS,
...queryOptions,
};

const queryKey = ['useName', address, chain.id];

return useQuery<GetNameReturnType>({
queryKey: ['useName', address, chain.id],
queryFn: async () => {
return await getName({ address, chain });
},
queryKey,
queryFn: () => getName({ address, chain }),
gcTime: cacheTime,
staleTime: staleTime,
enabled,
refetchOnWindowFocus: false,
refetchOnWindowFocus,
});
};
69 changes: 68 additions & 1 deletion src/identity/hooks/useSocials.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSocials } from '@/identity/utils/getSocials';
import { renderHook } from '@testing-library/react';
import { renderHook, waitFor } from '@testing-library/react';
import { base, mainnet } from 'viem/chains';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Mock } from 'vitest';
Expand Down Expand Up @@ -46,4 +46,71 @@ describe('useSocials', () => {
chain: base,
});
});

it('respects the enabled option in queryOptions', async () => {
// Use the renderHook function to create a test harness for the useSocials hook with enabled: false
const { result } = renderHook(
() => useSocials({ ensName: testEnsName }, { enabled: false }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// The query should not be executed
expect(result.current.isLoading).toBe(false);
expect(result.current.isFetched).toBe(false);
expect(mockGetSocials).not.toHaveBeenCalled();
});

it('uses the default query options when no queryOptions are provided', async () => {
// Mock the getSocials function to return some data
mockGetSocials.mockResolvedValue({
twitter: 'twitterHandle',
github: 'githubUsername',
farcaster: 'farcasterUsername',
website: 'https://example.com',
});

// Use the renderHook function to create a test harness for the useSocials hook
const { result } = renderHook(() => useSocials({ ensName: testEnsName }), {
wrapper: getNewReactQueryTestProvider(),
});

// Wait for the hook to finish fetching the socials
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});

// The query should be executed with the default options
expect(mockGetSocials).toHaveBeenCalled();
});

it('merges custom queryOptions with default options', async () => {
const customStaleTime = 60000; // 1 minute

// Mock the getSocials function to return some data
mockGetSocials.mockResolvedValue({
twitter: 'twitterHandle',
github: 'githubUsername',
farcaster: 'farcasterUsername',
website: 'https://example.com',
});

// Use the renderHook function to create a test harness for the useSocials hook with custom staleTime
const { result } = renderHook(
() =>
useSocials({ ensName: testEnsName }, { staleTime: customStaleTime }),
{
wrapper: getNewReactQueryTestProvider(),
},
);

// Wait for the hook to finish fetching the socials
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});

// The query should be executed with the custom staleTime
expect(mockGetSocials).toHaveBeenCalled();
});
});
18 changes: 12 additions & 6 deletions src/identity/hooks/useSocials.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GetSocialsReturnType, UseQueryOptions } from '@/identity/types';
import { getSocials } from '@/identity/utils/getSocials';
import { DEFAULT_QUERY_OPTIONS } from '@/internal/constants';
import { useQuery } from '@tanstack/react-query';
import type { Chain } from 'viem';
import { mainnet } from 'viem/chains';
Expand All @@ -13,14 +14,19 @@ export const useSocials = (
{ ensName, chain = mainnet }: UseSocialsOptions,
queryOptions?: UseQueryOptions,
) => {
const { enabled = true, cacheTime } = queryOptions ?? {};
const { enabled, cacheTime, staleTime, refetchOnWindowFocus } = {
...DEFAULT_QUERY_OPTIONS,
...queryOptions,
};

const queryKey = ['useSocials', ensName, chain.id];

return useQuery<GetSocialsReturnType>({
queryKey: ['useSocials', ensName, chain.id],
queryFn: async () => {
return getSocials({ ensName, chain });
},
queryKey,
queryFn: () => getSocials({ ensName, chain }),
gcTime: cacheTime,
staleTime: staleTime,
enabled,
refetchOnWindowFocus: false,
refetchOnWindowFocus,
});
};
2 changes: 2 additions & 0 deletions src/identity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ export type UseQueryOptions = {
enabled?: boolean;
/** Cache time in milliseconds */
cacheTime?: number;
/** Stale time in milliseconds */
staleTime?: number;
};

/**
Expand Down
Loading