Skip to content

Commit

Permalink
fix: useVechainDomain
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini committed Nov 4, 2024
1 parent baf26f6 commit 254bebd
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('getAddress', () => {

it('should return null if domain is null', async () => {
const result = await getAddress({ domain: null, connex: mockConnex });
expect(result).toBeNull();
expect(result).toBeUndefined();
});

it('should use main resolver for mainnet', async () => {
Expand Down Expand Up @@ -70,6 +70,6 @@ describe('getAddress', () => {
connex: mockConnex,
});

expect(result).toBeNull();
expect(result).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const getAddress = async ({
}: {
domain: string | null;
connex: DAppKitContext['connex'];
}): Promise<string | null> => {
if (!domain) return null;
}): Promise<string | undefined> => {
if (!domain) return undefined;

const resolver =
connex.thor.genesis.id === genesisBlocks.test.id
Expand All @@ -48,5 +48,5 @@ export const getAddress = async ({
decoded: { addresses },
} = res;

return (addresses?.[0] as string) || null;
return (addresses?.[0] as string) || undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('getDomain', () => {

it('should return null if address is null', async () => {
const result = await getDomain({ address: null, connex: mockConnex });
expect(result).toBeNull();
expect(result).toBeUndefined();
});

it('should use main resolver for mainnet', async () => {
Expand Down Expand Up @@ -76,6 +76,6 @@ describe('getDomain', () => {
connex: mockConnex,
});

expect(result).toBeNull();
expect(result).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export const getDomain = async ({
address,
connex,
}: {
address: string | null;
address?: string | null;
connex: DAppKitContext['connex'];
}): Promise<string | null> => {
if (!address) return null;
}): Promise<string | undefined> => {
if (!address) return undefined;

const resolver =
connex.thor.genesis.id === genesisBlocks.test.id
Expand All @@ -48,5 +48,5 @@ export const getDomain = async ({
decoded: { names },
} = res;

return (names?.[0] as string) || null;
return (names?.[0] as string) || undefined;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { renderHook, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { useVechainDomain } from './useVechainDomain';
import { wrapper } from '../../../test';

vi.mock('./api/getDomain', () => ({
getDomain: vi.fn().mockImplementation(({ address }) => {
if (address === '0x1234567890123456789012345678901234567890') {
return Promise.resolve('test.vet');
}
if (address === '0xERROR') {
return Promise.reject(new Error('Network error'));
}
if (address === '0x0000000000000000000000000000000000000000') {
return Promise.reject(new Error('Network error'));
}
return Promise.resolve(null);
}),
}));

vi.mock('./api/getAddress', () => ({
getAddress: vi.fn().mockImplementation(({ domain }) => {
if (domain === 'test.vet') {
return Promise.resolve(
'0x1234567890123456789012345678901234567890',
);
}
if (domain === 'invalid.vet') {
return Promise.resolve(
'0x0000000000000000000000000000000000000000',
);
}
if (domain === 'error.vet' || domain === '0xERROR') {
return Promise.reject(new Error('Network error'));
}
return Promise.resolve(null);
}),
}));

describe('useVechainDomain', () => {
it('should return initial state', () => {
const { result } = renderHook(
() => useVechainDomain({ addressOrDomain: null }),
{
wrapper,
},
);

expect(result.current).toEqual({
address: undefined,
domain: undefined,
isLoading: false,
isValidAddressOrDomain: false,
});
});

it('should handle valid address input', async () => {
const { result } = renderHook(
() =>
useVechainDomain({
addressOrDomain:
'0x1234567890123456789012345678901234567890',
}),
{ wrapper },
);

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current).toEqual({
address: '0x1234567890123456789012345678901234567890',
domain: 'test.vet',
isLoading: false,
isValidAddressOrDomain: true,
});
});
});

it('should handle valid domain input', async () => {
const { result } = renderHook(
() => useVechainDomain({ addressOrDomain: 'test.vet' }),
{
wrapper,
},
);

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current).toEqual({
address: '0x1234567890123456789012345678901234567890',
domain: 'test.vet',
isLoading: false,
isValidAddressOrDomain: true,
});
});
});

it('should handle invalid domain input', async () => {
const { result } = renderHook(
() => useVechainDomain({ addressOrDomain: 'invalid.vet' }),
{
wrapper,
},
);

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current).toEqual({
address: undefined,
domain: undefined,
isLoading: false,
isValidAddressOrDomain: false,
});
});
});

it('should handle error when getting domain', async () => {
const { result } = renderHook(
() => useVechainDomain({ addressOrDomain: '0xERROR' }),
{
wrapper,
},
);

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current).toEqual({
address: undefined,
domain: undefined,
isLoading: false,
isValidAddressOrDomain: false,
});
});
});

it('should handle error when getting address', async () => {
const { result } = renderHook(
() => useVechainDomain({ addressOrDomain: 'error.vet' }),
{
wrapper,
},
);

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current).toEqual({
address: undefined,
domain: undefined,
isLoading: false,
isValidAddressOrDomain: false,
});
});
});
it('should handle error when getting zero address', async () => {
const { result } = renderHook(
() =>
useVechainDomain({
addressOrDomain:
'0x0000000000000000000000000000000000000000',
}),
{
wrapper,
},
);

expect(result.current.isLoading).toBe(true);

await waitFor(() => {
expect(result.current).toEqual({
address: '0x0000000000000000000000000000000000000000',
domain: undefined,
isLoading: false,
isValidAddressOrDomain: true,
});
});
});
});
Loading

0 comments on commit 254bebd

Please sign in to comment.