From bb0084a052d1895516237f06b0b5cf50617bcc95 Mon Sep 17 00:00:00 2001 From: Francesco Ceccon Date: Fri, 4 Feb 2022 18:36:27 +0000 Subject: [PATCH] feat: useStarknet hook returns errors --- .changeset/neat-actors-play.md | 5 +++++ packages/core/src/providers/starknet/manager.ts | 17 +++++++++++++---- packages/core/src/providers/starknet/model.ts | 1 + packages/core/test/providers/block.test.tsx | 3 ++- packages/core/test/providers/starknet.test.tsx | 13 +++++++++++++ 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 .changeset/neat-actors-play.md create mode 100644 packages/core/test/providers/starknet.test.tsx diff --git a/.changeset/neat-actors-play.md b/.changeset/neat-actors-play.md new file mode 100644 index 00000000..428921dc --- /dev/null +++ b/.changeset/neat-actors-play.md @@ -0,0 +1,5 @@ +--- +'@starknet-react/core': minor +--- + +useStarknet hook now returns an error on activation diff --git a/packages/core/src/providers/starknet/manager.ts b/packages/core/src/providers/starknet/manager.ts index c7470d27..fd4e47cb 100644 --- a/packages/core/src/providers/starknet/manager.ts +++ b/packages/core/src/providers/starknet/manager.ts @@ -7,6 +7,7 @@ import { StarknetState } from './model' interface StarknetManagerState { account?: string library: ProviderInterface + error?: string } interface SetAccount { @@ -19,7 +20,12 @@ interface SetProvider { provider: ProviderInterface } -type Action = SetAccount | SetProvider +interface SetError { + type: 'set_error' + error: string +} + +type Action = SetAccount | SetProvider | SetError function reducer(state: StarknetManagerState, action: Action): StarknetManagerState { switch (action.type) { @@ -29,6 +35,9 @@ function reducer(state: StarknetManagerState, action: Action): StarknetManagerSt case 'set_provider': { return { ...state, library: action.provider } } + case 'set_error': { + return { ...state, error: action.error } + } default: { return state } @@ -41,7 +50,7 @@ export function useStarknetManager(): StarknetState { library: defaultProvider, }) - const { account, library } = state + const { account, library, error } = state useEffect(() => { if (typeof window !== undefined) { @@ -63,9 +72,9 @@ export function useStarknetManager(): StarknetState { } } catch (err) { console.error(err) - // TODO: display error message to user + dispatch({ type: 'set_error', error: 'could not activate StarkNet' }) } }, []) - return { account, hasStarknet, connectBrowserWallet, library } + return { account, hasStarknet, connectBrowserWallet, library, error } } diff --git a/packages/core/src/providers/starknet/model.ts b/packages/core/src/providers/starknet/model.ts index 2663a73c..80f8d74f 100644 --- a/packages/core/src/providers/starknet/model.ts +++ b/packages/core/src/providers/starknet/model.ts @@ -5,6 +5,7 @@ export interface StarknetState { hasStarknet: boolean connectBrowserWallet: () => void library: ProviderInterface + error?: string } export const STARKNET_INITIAL_STATE: StarknetState = { diff --git a/packages/core/test/providers/block.test.tsx b/packages/core/test/providers/block.test.tsx index b72ecbec..3ce27218 100644 --- a/packages/core/test/providers/block.test.tsx +++ b/packages/core/test/providers/block.test.tsx @@ -11,7 +11,8 @@ describe('useStarknetBlock', () => { ) const { result, waitForNextUpdate } = renderHook(() => useStarknetBlock(), { wrapper }) expect(result.current).toBeUndefined() - await waitForNextUpdate() + // wait up to one minute for a block + await waitForNextUpdate({ timeout: 60000 }) expect(result.current.timestamp).toBeGreaterThan(0) expect(result.current.block_hash).not.toBeUndefined() }) diff --git a/packages/core/test/providers/starknet.test.tsx b/packages/core/test/providers/starknet.test.tsx new file mode 100644 index 00000000..e66e54d2 --- /dev/null +++ b/packages/core/test/providers/starknet.test.tsx @@ -0,0 +1,13 @@ +import React from 'react' +import { renderHook } from '@testing-library/react-hooks' +import { useStarknet, StarknetProvider } from '../../src' + +describe('useStarknetBlock', () => { + it('returns the current block', async () => { + const wrapper = ({ children }) => {children} + const { result } = renderHook(() => useStarknet(), { wrapper }) + const { account, hasStarknet } = result.current + expect(account).toBeUndefined() + expect(hasStarknet).toBeFalsy() + }) +})