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

chore: Update missing address error handling #1430

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/flat-forks-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@coinbase/onchainkit': patch
---

-**chore**: Update missing address error handling in the `Identity` components. By @cpcramer #1430
22 changes: 8 additions & 14 deletions src/identity/components/Address.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ function mock<T>(func: T) {
return func as Mock;
}

const silenceError = () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
return () => consoleErrorMock.mockRestore();
};

vi.mock('../utils/getSlicedAddress', () => ({
getSlicedAddress: vi.fn(),
}));
Expand All @@ -38,15 +31,16 @@ describe('Address component', () => {
vi.clearAllMocks();
});

it('should throw an error when no address is provided', () => {
const restore = silenceError();
useIdentityContextMock.mockReturnValue({});
expect(() => {
render(<Address />);
}).toThrow(
it('should console.error and return null when no address is provided', () => {
vi.mocked(useIdentityContext).mockReturnValue({});
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const { container } = render(<Address />);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Address: an Ethereum address must be provided to the Identity or Address component.',
);
restore();
expect(container.firstChild).toBeNull();
});

it('renders the sliced address when address supplied to Identity', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/identity/components/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export function Address({
}: AddressReact) {
const { address: contextAddress } = useIdentityContext();
if (!contextAddress && !address) {
throw new Error(
console.error(
'Address: an Ethereum address must be provided to the Identity or Address component.',
);
return null;
}

const accountAddress = address ?? contextAddress;
Expand Down
24 changes: 11 additions & 13 deletions src/identity/components/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ function mock<T>(func: T) {
return func as Mock;
}

const silenceError = () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
return () => consoleErrorMock.mockRestore();
};
vi.mock('../../useOnchainKit', () => ({
useOnchainKit: vi.fn(),
}));
Expand Down Expand Up @@ -54,15 +48,19 @@ describe('Avatar Component', () => {
vi.clearAllMocks();
});

it('should throw an error when no address is provided', () => {
useIdentityContextMock.mockReturnValue({ address: null });
const restore = silenceError();
expect(() => {
render(<Avatar />);
}).toThrow(
it('should console.error and return null when no address is provided', () => {
vi.mocked(useIdentityContext).mockReturnValue({
address: undefined,
chain: undefined,
});
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const { container } = render(<Avatar />);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Avatar: an Ethereum address must be provided to the Identity or Avatar component.',
);
restore();
expect(container.firstChild).toBeNull();
});

it('should display loading indicator when loading', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/identity/components/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export function Avatar({
const accountChain = chain ?? contextChain;

if (!accountAddress) {
throw new Error(
console.error(
'Avatar: an Ethereum address must be provided to the Identity or Avatar component.',
);
return null;
}

// The component first attempts to retrieve the ENS name and avatar for the given Ethereum address.
Expand Down
25 changes: 10 additions & 15 deletions src/identity/components/EthBalance.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ function mock<T>(func: T) {
return func as Mock;
}

const silenceError = () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
return () => consoleErrorMock.mockRestore();
};

vi.mock('./IdentityProvider', () => ({
useIdentityContext: vi.fn(),
}));
Expand All @@ -35,16 +28,18 @@ const useGetEthBalanceMock = mock(useGetETHBalance);
describe('EthBalance', () => {
const testIdentityProviderAddress = '0xIdentityAddress';
const testEthBalanceComponentAddress = '0xEthBalanceComponentAddress';
it('should throw an error if no address is provided', () => {
useIdentityContextMock.mockReturnValue({ address: null });

const restore = silenceError();
expect(() =>
render(<EthBalance address={undefined} className="" />),
).toThrow(
it('should console.error and return null when no address is provided', () => {
vi.mocked(useIdentityContext).mockReturnValue({
address: undefined,
});
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const { container } = render(<EthBalance />);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Address: an Ethereum address must be provided to the Identity or EthBalance component.',
);
restore();
expect(container.firstChild).toBeNull();
});

it('should display the balance if provided', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/identity/components/EthBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { useIdentityContext } from './IdentityProvider';
export function EthBalance({ address, className }: EthBalanceReact) {
const { address: contextAddress } = useIdentityContext();
if (!contextAddress && !address) {
throw new Error(
console.error(
'Address: an Ethereum address must be provided to the Identity or EthBalance component.',
);
return null;
}

const { convertedBalance: balance, error } = useGetETHBalance(
Expand Down
25 changes: 10 additions & 15 deletions src/identity/components/Name.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import { Badge } from './Badge';
import { useIdentityContext } from './IdentityProvider';
import { Name } from './Name';

const silenceError = () => {
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
return () => consoleErrorMock.mockRestore();
};

vi.mock('../hooks/useAttestations', () => ({
useAttestations: vi.fn(),
}));
Expand Down Expand Up @@ -44,17 +37,19 @@ describe('Name', () => {
vi.spyOn(console, 'error').mockImplementation(vi.fn());
});

it('should throw an error when no address is provided', () => {
(useIdentityContext as Mock).mockReturnValue({
schemaId: '0x123',
it('should console.error and return null when no address is provided', () => {
vi.mocked(useIdentityContext).mockReturnValue({
address: undefined,
chain: undefined,
});
const restore = silenceError();
expect(() => {
render(<Name />);
}).toThrow(
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const { container } = render(<Name />);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Name: an Ethereum address must be provided to the Identity or Name component.',
);
restore();
expect(container.firstChild).toBeNull();
});

it('displays ENS name when available', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/identity/components/Name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export function Name({
}: NameReact) {
const { address: contextAddress, chain: contextChain } = useIdentityContext();
if (!contextAddress && !address) {
throw new Error(
console.error(
'Name: an Ethereum address must be provided to the Identity or Name component.',
);
return null;
}

const accountAddress = address ?? contextAddress;
Expand Down