-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean up profile sync hooks (#28132)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** These changes make it easier for us to extend and add other syncing features. Originally this was part of my local network sync feature, but decoupled parts of it since the change are a lot. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28132?quickstart=1) ## **Related issues** Fixes: N/A ## **Manual testing steps** There should be no changes as this is a code refactor and the interfaces/exports do not change. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
1 parent
3a35385
commit 6eae9bc
Showing
10 changed files
with
331 additions
and
285 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
178 changes: 0 additions & 178 deletions
178
ui/hooks/metamask-notifications/useProfileSyncing.test.tsx
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
ui/hooks/metamask-notifications/useProfileSyncing/accountSyncing.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,70 @@ | ||
import { waitFor } from '@testing-library/react'; | ||
import { act } from '@testing-library/react-hooks'; | ||
import { renderHookWithProviderTyped } from '../../../../test/lib/render-helpers'; | ||
import * as actions from '../../../store/actions'; | ||
import { | ||
useAccountSyncingEffect, | ||
useDeleteAccountSyncingDataFromUserStorage, | ||
} from './accountSyncing'; | ||
import * as ProfileSyncModule from './profileSyncing'; | ||
|
||
describe('useDeleteAccountSyncingDataFromUserStorage()', () => { | ||
it('should dispatch account sync data deletion', async () => { | ||
const mockDeleteAccountSyncAction = jest.spyOn( | ||
actions, | ||
'deleteAccountSyncingDataFromUserStorage', | ||
); | ||
|
||
const { result } = renderHookWithProviderTyped( | ||
() => useDeleteAccountSyncingDataFromUserStorage(), | ||
{}, | ||
); | ||
|
||
await act(async () => { | ||
await result.current.dispatchDeleteAccountData(); | ||
}); | ||
|
||
expect(mockDeleteAccountSyncAction).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('useAccountSyncingEffect', () => { | ||
const arrangeMocks = () => { | ||
const mockUseShouldProfileSync = jest.spyOn( | ||
ProfileSyncModule, | ||
'useShouldDispatchProfileSyncing', | ||
); | ||
const mockSyncAccountsAction = jest.spyOn( | ||
actions, | ||
'syncInternalAccountsWithUserStorage', | ||
); | ||
return { | ||
mockUseShouldProfileSync, | ||
mockSyncAccountsAction, | ||
}; | ||
}; | ||
|
||
const arrangeAndAct = (props: { profileSyncConditionsMet: boolean }) => { | ||
const mocks = arrangeMocks(); | ||
mocks.mockUseShouldProfileSync.mockReturnValue( | ||
props.profileSyncConditionsMet, | ||
); | ||
|
||
renderHookWithProviderTyped(() => useAccountSyncingEffect(), {}); | ||
return mocks; | ||
}; | ||
|
||
it('should run effect if profile sync conditions are met', async () => { | ||
const mocks = arrangeAndAct({ profileSyncConditionsMet: true }); | ||
await waitFor(() => { | ||
expect(mocks.mockSyncAccountsAction).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should not run effect if profile sync conditions are not met', async () => { | ||
const mocks = arrangeAndAct({ profileSyncConditionsMet: false }); | ||
await waitFor(() => { | ||
expect(mocks.mockSyncAccountsAction).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
ui/hooks/metamask-notifications/useProfileSyncing/accountSyncing.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,66 @@ | ||
import log from 'loglevel'; | ||
import { useCallback, useEffect } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { | ||
deleteAccountSyncingDataFromUserStorage, | ||
syncInternalAccountsWithUserStorage, | ||
} from '../../../store/actions'; | ||
import { useShouldDispatchProfileSyncing } from './profileSyncing'; | ||
|
||
/** | ||
* Custom hook to dispatch account syncing. | ||
* | ||
* @returns An object containing the `dispatchAccountSyncing` function, boolean `shouldDispatchAccountSyncing`, | ||
* and error state. | ||
*/ | ||
const useAccountSyncing = () => { | ||
const dispatch = useDispatch(); | ||
|
||
const shouldDispatchAccountSyncing = useShouldDispatchProfileSyncing(); | ||
|
||
const dispatchAccountSyncing = useCallback(() => { | ||
try { | ||
if (!shouldDispatchAccountSyncing) { | ||
return; | ||
} | ||
dispatch(syncInternalAccountsWithUserStorage()); | ||
} catch (e) { | ||
log.error(e); | ||
} | ||
}, [dispatch, shouldDispatchAccountSyncing]); | ||
|
||
return { | ||
dispatchAccountSyncing, | ||
shouldDispatchAccountSyncing, | ||
}; | ||
}; | ||
|
||
/** | ||
* Custom hook to apply account syncing effect. | ||
*/ | ||
export const useAccountSyncingEffect = () => { | ||
const shouldSync = useShouldDispatchProfileSyncing(); | ||
const { dispatchAccountSyncing } = useAccountSyncing(); | ||
|
||
useEffect(() => { | ||
if (shouldSync) { | ||
dispatchAccountSyncing(); | ||
} | ||
}, [shouldSync, dispatchAccountSyncing]); | ||
}; | ||
|
||
/** | ||
* Custom hook to delete a user's account syncing data from user storage | ||
*/ | ||
export const useDeleteAccountSyncingDataFromUserStorage = () => { | ||
const dispatch = useDispatch(); | ||
const dispatchDeleteAccountData = useCallback(async () => { | ||
try { | ||
await dispatch(deleteAccountSyncingDataFromUserStorage()); | ||
} catch { | ||
// Do Nothing | ||
} | ||
}, []); | ||
|
||
return { dispatchDeleteAccountData }; | ||
}; |
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,9 @@ | ||
export { | ||
useDisableProfileSyncing, | ||
useEnableProfileSyncing, | ||
useSetIsProfileSyncingEnabled, | ||
} from './profileSyncing'; | ||
export { | ||
useAccountSyncingEffect, | ||
useDeleteAccountSyncingDataFromUserStorage, | ||
} from './accountSyncing'; |
Oops, something went wrong.