-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor notifications unit tests (#11431)
<!-- 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** This PR addresses all comments from [this PR](#11250) related to unit tests best practices. ## **Related issues** Fixes: #11250 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **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 Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.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-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [x] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [x] 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. --------- Co-authored-by: Nico MASSART <[email protected]>
- Loading branch information
1 parent
853cf92
commit 7bc75e9
Showing
8 changed files
with
244 additions
and
357 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
app/components/Views/Settings/NotificationsSettings/__snapshots__/index.test.tsx.snap
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
125 changes: 125 additions & 0 deletions
125
app/components/Views/Settings/NotificationsSettings/useToggleNotifications.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,125 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useToggleNotifications } from './useToggleNotifications'; | ||
import NotificationsService from '../../../../util/notifications/services/NotificationService'; | ||
import Routes from '../../../../constants/navigation/Routes'; | ||
import { NavigationProp, ParamListBase } from '@react-navigation/native'; | ||
|
||
jest.mock( | ||
'../../../../util/notifications/services/NotificationService', | ||
() => ({ | ||
getAllPermissions: jest.fn(), | ||
}), | ||
); | ||
|
||
const mockNavigation = { | ||
navigate: jest.fn(), | ||
} as unknown as NavigationProp<ParamListBase>; | ||
|
||
const mockDisableNotifications = jest.fn(); | ||
const mockEnableNotifications = jest.fn(); | ||
const mockSetUiNotificationStatus = jest.fn(); | ||
|
||
describe('useToggleNotifications', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('navigates to basic functionality screen if basic functionality is disabled', async () => { | ||
const { result } = renderHook(() => | ||
useToggleNotifications({ | ||
navigation: mockNavigation, | ||
basicFunctionalityEnabled: false, | ||
isMetamaskNotificationsEnabled: false, | ||
isProfileSyncingEnabled: false, | ||
disableNotifications: mockDisableNotifications, | ||
enableNotifications: mockEnableNotifications, | ||
setUiNotificationStatus: mockSetUiNotificationStatus, | ||
}), | ||
); | ||
|
||
await act(async () => { | ||
await result.current.toggleNotificationsEnabled(); | ||
}); | ||
|
||
expect(mockNavigation.navigate).toHaveBeenCalledWith( | ||
Routes.MODAL.ROOT_MODAL_FLOW, | ||
{ | ||
screen: Routes.SHEET.BASIC_FUNCTIONALITY, | ||
params: { | ||
caller: Routes.SETTINGS.NOTIFICATIONS, | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
it('switches notifications OFF if notifications previously enabled', async () => { | ||
const { result } = renderHook(() => | ||
useToggleNotifications({ | ||
navigation: mockNavigation, | ||
basicFunctionalityEnabled: true, | ||
isMetamaskNotificationsEnabled: true, | ||
isProfileSyncingEnabled: false, | ||
disableNotifications: mockDisableNotifications, | ||
enableNotifications: mockEnableNotifications, | ||
setUiNotificationStatus: mockSetUiNotificationStatus, | ||
}), | ||
); | ||
|
||
await act(async () => { | ||
await result.current.toggleNotificationsEnabled(); | ||
}); | ||
|
||
expect(mockDisableNotifications).toHaveBeenCalled(); | ||
expect(mockSetUiNotificationStatus).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('switches notifications ON if notifications previously disabled and permission is authorized', async () => { | ||
(NotificationsService.getAllPermissions as jest.Mock).mockResolvedValue({ | ||
permission: 'authorized', | ||
}); | ||
|
||
const { result } = renderHook(() => | ||
useToggleNotifications({ | ||
navigation: mockNavigation, | ||
basicFunctionalityEnabled: true, | ||
isMetamaskNotificationsEnabled: false, | ||
isProfileSyncingEnabled: false, | ||
disableNotifications: mockDisableNotifications, | ||
enableNotifications: mockEnableNotifications, | ||
setUiNotificationStatus: mockSetUiNotificationStatus, | ||
}), | ||
); | ||
|
||
await act(async () => { | ||
await result.current.toggleNotificationsEnabled(); | ||
}); | ||
|
||
expect(mockEnableNotifications).toHaveBeenCalled(); | ||
expect(mockSetUiNotificationStatus).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
it('switches notifications OFF if device permission is not authorized', async () => { | ||
(NotificationsService.getAllPermissions as jest.Mock).mockResolvedValue({ | ||
permission: 'denied', | ||
}); | ||
|
||
const { result } = renderHook(() => | ||
useToggleNotifications({ | ||
navigation: mockNavigation, | ||
basicFunctionalityEnabled: true, | ||
isMetamaskNotificationsEnabled: false, | ||
isProfileSyncingEnabled: false, | ||
disableNotifications: mockDisableNotifications, | ||
enableNotifications: mockEnableNotifications, | ||
setUiNotificationStatus: mockSetUiNotificationStatus, | ||
}), | ||
); | ||
|
||
await act(async () => { | ||
await result.current.toggleNotificationsEnabled(); | ||
}); | ||
|
||
expect(mockEnableNotifications).not.toHaveBeenCalled(); | ||
expect(mockSetUiNotificationStatus).not.toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.