Skip to content

Commit

Permalink
2438: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenkleinle committed Sep 30, 2023
1 parent 10f8e7d commit 39b9974
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions native/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ global.fetch = require('jest-fetch-mock')
console.error = () => undefined

jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage)
jest.mock('react-native-permissions', () => require('react-native-permissions/mock'))

// react-navigation jest setup
// https://reactnavigation.org/docs/testing#mocking-native-modules
Expand Down
1 change: 1 addition & 0 deletions native/src/__tests__/Navigator.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ jest.mock('../utils/PushNotificationsManager', () => ({
pushNotificationsSupported: jest.fn(() => true),
quitAppStatePushNotificationListener: jest.fn(),
useForegroundPushNotificationListener: jest.fn(),
androidPushNotificationPermissionFix: jest.fn(async () => undefined),
}))
jest.mock('../utils/FetcherModule')

Expand Down
1 change: 0 additions & 1 deletion native/src/components/__tests__/NearbyCities.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jest.mock('../../utils/LocationPermissionManager', () => ({
checkLocationPermission: jest.fn(),
requestLocationPermission: jest.fn(),
}))
jest.mock('react-native-permissions', () => require('react-native-permissions/mock'))
jest.mock('@react-native-community/geolocation')
jest.mock('react-i18next')

Expand Down
22 changes: 7 additions & 15 deletions native/src/utils/__tests__/PushNotificationsManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import messaging, { FirebaseMessagingTypes } from '@react-native-firebase/messaging'
import { mocked } from 'jest-mock'
import { requestNotifications } from 'react-native-permissions'

import buildConfig from '../../constants/buildConfig'
import appSettings from '../AppSettings'
import * as PushNotificationsManager from '../PushNotificationsManager'

jest.mock('@react-native-firebase/messaging', () => jest.fn(() => ({})))
Expand Down Expand Up @@ -72,30 +74,20 @@ describe('PushNotificationsManager', () => {
expect(mockRequestPermission).not.toHaveBeenCalled()
})

it('should request permissions and return false if not granted', async () => {
it('should request permissions and return false and disable push notifications in settings if not granted', async () => {
mockBuildConfig(true, false)
const mockRequestPermission = jest.fn(async () => 0)
mockedFirebaseMessaging.mockImplementation(() => {
const previous = previousFirebaseMessaging
previous.requestPermission = mockRequestPermission
return previous
})
mocked(requestNotifications).mockImplementationOnce(async () => ({ status: 'blocked', settings: {} }))

expect(await PushNotificationsManager.requestPushNotificationPermission()).toBeFalsy()
expect(mockRequestPermission).toHaveBeenCalledTimes(1)
expect((await appSettings.loadSettings()).allowPushNotifications).toBe(false)
})

it('should request permissions and return true if granted', async () => {
mockBuildConfig(true, false)
const mockRequestPermission = jest.fn(async () => 1)
mockedFirebaseMessaging.mockImplementation(() => {
const previous = previousFirebaseMessaging
previous.requestPermission = mockRequestPermission
return previous
})
mocked(requestNotifications).mockImplementationOnce(async () => ({ status: 'granted', settings: {} }))

expect(await PushNotificationsManager.requestPushNotificationPermission()).toBeTruthy()
expect(mockRequestPermission).toHaveBeenCalledTimes(1)
expect((await appSettings.loadSettings()).allowPushNotifications).toBe(true)
})
})

Expand Down
1 change: 0 additions & 1 deletion native/src/utils/__tests__/createSettingsSections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jest.mock('../../utils/PushNotificationsManager', () => ({
subscribeNews: jest.fn(),
unsubscribeNews: jest.fn(),
}))
jest.mock('react-native-permissions', () => require('react-native-permissions/mock'))
jest.mock('@react-native-community/geolocation')

const mockRequestPushNotificationPermission = mocked(requestPushNotificationPermission)
Expand Down

0 comments on commit 39b9974

Please sign in to comment.