-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: the
useWallet()
hook now causes a re-render when any wallet's…
… change event fires
- Loading branch information
1 parent
8069d6c
commit 5e4f381
Showing
6 changed files
with
234 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@wallet-standard/react-core': patch | ||
--- | ||
|
||
The `useWallets()` hook will now cause a re-render any time a wallet's `'change'` event fires |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,107 @@ | ||
import { getWallets } from '@wallet-standard/app'; | ||
import type { Wallet, WalletVersion } from '@wallet-standard/base'; | ||
import type { StandardEventsFeature, StandardEventsListeners } from '@wallet-standard/features'; | ||
import { act } from 'react-test-renderer'; | ||
|
||
import { renderHook } from '../test-renderer.js'; | ||
import { useWallets } from '../useWallets.js'; | ||
|
||
jest.mock('@wallet-standard/app'); | ||
|
||
describe('useWallets', () => { | ||
it.todo('Add a test'); | ||
let mockGet: jest.MockedFn<ReturnType<typeof getWallets>['get']>; | ||
let mockOn: jest.MockedFn<ReturnType<typeof getWallets>['on']>; | ||
let mockRegister: jest.MockedFn<ReturnType<typeof getWallets>['register']>; | ||
beforeEach(() => { | ||
mockGet = jest.fn().mockReturnValue([] as readonly Wallet[]); | ||
mockOn = jest.fn(); | ||
mockRegister = jest.fn(); | ||
jest.mocked(getWallets).mockReturnValue({ | ||
get: mockGet, | ||
on: mockOn, | ||
register: mockRegister, | ||
}); | ||
// Suppresses console output when an `ErrorBoundary` is hit. | ||
// See https://stackoverflow.com/a/72632884/802047 | ||
}); | ||
it('returns a list of registered wallets', () => { | ||
const expectedWallets = [] as readonly Wallet[]; | ||
mockGet.mockReturnValue(expectedWallets); | ||
const { result } = renderHook(() => useWallets()); | ||
expect(result.current).toBe(expectedWallets); | ||
}); | ||
describe.each(['register', 'unregister'])('when the %s event fires', (expectedEvent) => { | ||
let initialWallets: readonly Wallet[]; | ||
let listeners: (((...wallets: Wallet[]) => void) | ((...wallets: Wallet[]) => void))[] = []; | ||
beforeEach(() => { | ||
initialWallets = [] as readonly Wallet[]; | ||
listeners = []; | ||
mockGet.mockReturnValue(initialWallets); | ||
mockOn.mockImplementation((event, listener) => { | ||
if (event === expectedEvent) { | ||
listeners.push(listener); | ||
} | ||
return () => { | ||
/* unsubscribe */ | ||
}; | ||
}); | ||
mockGet.mockReturnValue(initialWallets); | ||
}); | ||
it('updates if the wallet array has changed', () => { | ||
const { result } = renderHook(() => useWallets()); | ||
const expectedWalletsAfterUpdate = ['new' as unknown as Wallet] as readonly Wallet[]; | ||
mockGet.mockReturnValue(expectedWalletsAfterUpdate); | ||
act(() => { | ||
listeners.forEach((l) => { | ||
l(/* doesn't really matter what the listener receives */); | ||
}); | ||
}); | ||
expect(result.current).toBe(expectedWalletsAfterUpdate); | ||
}); | ||
it('does not update if the wallet array has not changed', () => { | ||
const { result } = renderHook(() => useWallets()); | ||
act(() => { | ||
listeners.forEach((l) => { | ||
l(/* doesn't really matter what the listener receives */); | ||
}); | ||
}); | ||
expect(result.current).toBe(initialWallets); | ||
}); | ||
}); | ||
it('recycles the wallets array when the `change` event fires on a wallet', () => { | ||
const listeners: StandardEventsListeners['change'][] = []; | ||
const mockWallets = [ | ||
{ | ||
accounts: [], | ||
chains: ['solana:mainnet'] as const, | ||
features: { | ||
'standard:events': { | ||
on(event, listener) { | ||
if (event === 'change') { | ||
listeners.push(listener); | ||
} | ||
return () => { | ||
/* unsubscribe */ | ||
}; | ||
}, | ||
version: '1.0.0' as const, | ||
}, | ||
} as StandardEventsFeature, | ||
icon: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIBAAA=', | ||
name: 'Mock Wallet', | ||
version: '1.0.0' as WalletVersion, | ||
} as const, | ||
]; | ||
mockGet.mockReturnValue(mockWallets); | ||
const { result } = renderHook(() => useWallets()); | ||
act(() => { | ||
listeners.forEach((l) => { | ||
l({ | ||
/* doesn't really matter what the listener receives */ | ||
}); | ||
}); | ||
}); | ||
expect(result.current).toStrictEqual(mockWallets); | ||
expect(result.current).not.toBe(mockWallets); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { ErrorBoundary } from 'react-error-boundary'; | ||
import { act, create, type ReactTestRenderer } from 'react-test-renderer'; | ||
|
||
type Result<T> = | ||
| { | ||
__type: 'error'; | ||
current: Error; | ||
reset: () => void; | ||
} | ||
| { | ||
__type: 'result'; | ||
current?: T; | ||
}; | ||
|
||
type TestComponentProps<THookReturn> = { | ||
executor(): THookReturn; | ||
resultRef: Result<THookReturn>; | ||
}; | ||
|
||
function TestComponentHookRenderer<THookFn extends (...args: never) => unknown>({ | ||
executor, | ||
resultRef, | ||
}: TestComponentProps<ReturnType<THookFn>>) { | ||
resultRef.current = executor(); | ||
return null; | ||
} | ||
|
||
function TestComponent<THookFn extends (...args: never) => unknown>({ | ||
executor, | ||
resultRef, | ||
}: TestComponentProps<ReturnType<THookFn>>) { | ||
return ( | ||
<ErrorBoundary | ||
fallbackRender={({ error, resetErrorBoundary }) => { | ||
resultRef.__type = 'error'; | ||
resultRef.current = error; | ||
(resultRef as Extract<typeof resultRef, { __type: 'error' }>).reset = resetErrorBoundary; | ||
return null; | ||
}} | ||
onReset={() => { | ||
resultRef.__type = 'result'; | ||
delete resultRef.current; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (resultRef as any).reset; | ||
}} | ||
> | ||
<TestComponentHookRenderer executor={executor} resultRef={resultRef} /> | ||
</ErrorBoundary> | ||
); | ||
} | ||
|
||
export function renderHook<THookReturn>(executor: () => THookReturn): { | ||
rerenderHook(nextExecutor: () => THookReturn): void; | ||
result: Readonly<Result<THookReturn>>; | ||
} { | ||
const result = { __type: 'result' } as Result<THookReturn>; | ||
let testRenderer: ReactTestRenderer; | ||
act(() => { | ||
testRenderer = create(<TestComponent executor={executor} resultRef={result} />); | ||
}); | ||
return { | ||
rerenderHook(nextExecutor) { | ||
act(() => { | ||
testRenderer.update(<TestComponent executor={nextExecutor} resultRef={result} />); | ||
}); | ||
}, | ||
result, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.