-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improves the react connected hook when using extension & emit t…
…erminate when using extension (#1186) * chore: improves the react connected hook when using extension * chore: fixes linting and unit tests * chore: fixes unit test to meet the new event emit of terminate * chore: fixes fetching selectedAddress to ensure refreshes dont break the flow * chore: adds unit test for useHandleTerminateEvent
- Loading branch information
1 parent
29f6598
commit 801c9ef
Showing
6 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.test.tsx
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,41 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useHandleTerminateEvent } from './useHandleTerminateEvent'; | ||
import { EventHandlerProps } from '../MetaMaskProvider'; | ||
import * as loggerModule from '../utils/logger'; | ||
|
||
describe('useHandleTerminateEvent', () => { | ||
const spyLogger = jest.spyOn(loggerModule, 'logger'); | ||
|
||
const eventHandlerProps = { | ||
setConnecting: jest.fn(), | ||
setConnected: jest.fn(), | ||
setError: jest.fn(), | ||
debug: true, | ||
} as unknown as EventHandlerProps; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
eventHandlerProps.setConnecting = jest.fn(); | ||
eventHandlerProps.setConnected = jest.fn(); | ||
eventHandlerProps.setError = jest.fn(); | ||
}); | ||
|
||
it('should handle the terminate event correctly', () => { | ||
const mockReason = { message: 'Terminated due to xyz', code: -32000 }; | ||
|
||
const { result } = renderHook(() => | ||
useHandleTerminateEvent(eventHandlerProps), | ||
); | ||
result.current(mockReason); | ||
|
||
expect(spyLogger).toHaveBeenCalledWith( | ||
"[MetaMaskProvider: useHandleTerminateEvent()] on 'terminate' event.", | ||
mockReason, | ||
); | ||
|
||
expect(eventHandlerProps.setConnecting).toHaveBeenCalledWith(false); | ||
expect(eventHandlerProps.setConnected).toHaveBeenCalledWith(false); | ||
expect(eventHandlerProps.setError).toHaveBeenCalledWith(mockReason); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/sdk-react/src/EventsHandlers/useHandleTerminateEvent.ts
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,25 @@ | ||
import { EthereumRpcError } from 'eth-rpc-errors'; | ||
import { useCallback } from 'react'; | ||
import { EventHandlerProps } from '../MetaMaskProvider'; | ||
import { logger } from '../utils/logger'; | ||
|
||
export const useHandleTerminateEvent = ({ | ||
debug, | ||
setConnecting, | ||
setConnected, | ||
setError, | ||
}: EventHandlerProps) => { | ||
return useCallback( | ||
(reason: unknown) => { | ||
logger( | ||
`[MetaMaskProvider: useHandleTerminateEvent()] on 'terminate' event.`, | ||
reason, | ||
); | ||
|
||
setConnecting(false); | ||
setConnected(false); | ||
setError(reason as EthereumRpcError<unknown>); | ||
}, | ||
[debug, setConnecting, setConnected, setError], | ||
); | ||
}; |
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
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