Skip to content

Commit

Permalink
[#144] Try to cover set notification handler fn
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Aug 25, 2023
1 parent 30992cf commit d7a088a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
29 changes: 21 additions & 8 deletions app/src/navigation/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import { render, act } from '@testing-library/react-native';
import { render, act, waitFor } from '@testing-library/react-native';
import { NavigationContainer } from '@react-navigation/native';
import { AuthState, UIState } from '../../store';
import { BackHandler } from 'react-native';
import Navigation from '../index';
import Navigation, { setNotificationHandler } from '../index';
import { backgroundTask, notification } from '../../lib';
import * as Notifications from 'expo-notifications';

Expand All @@ -17,12 +17,11 @@ jest.mock('expo-background-fetch', () => ({
},
}));

jest.mock('expo-notifications', () => ({
...jest.requireActual('expo-notifications'),
addNotificationReceivedListener: jest.fn(),
addNotificationResponseReceivedListener: jest.fn(),
removeNotificationSubscription: jest.fn(),
}));
jest.mock('expo-notifications');

Notifications.addNotificationReceivedListener.mockImplementation(() => jest.fn());
Notifications.addNotificationResponseReceivedListener.mockImplementation(() => jest.fn());
Notifications.removeNotificationSubscription.mockImplementation(() => jest.fn());

jest.mock('../..//lib/background-task', () => ({
syncFormVersion: jest.fn(),
Expand All @@ -49,6 +48,20 @@ describe('Navigation Component', () => {
jest.clearAllMocks();
});

it('should call set notification handler', async () => {
const mockHandleNotification = jest.fn().mockResolvedValue({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
});
Notifications.setNotificationHandler.mockImplementation(({ handleNotification }) => {
handleNotification(mockHandleNotification);
});
await setNotificationHandler();

expect(Notifications.setNotificationHandler).toHaveBeenCalledTimes(1);
});

it('should call set up hardware back press function listener', () => {
const { unmount } = render(
<NavigationContainer>
Expand Down
16 changes: 9 additions & 7 deletions app/src/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import { backgroundTask, notification } from '../lib';
const SYNC_FORM_VERSION_TASK_NAME = 'sync-form-version';
const SYNC_FORM_SUBMISSION_TASK_NAME = 'sync-form-submission';

Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
export const setNotificationHandler = () =>
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
setNotificationHandler();

TaskManager.defineTask(SYNC_FORM_VERSION_TASK_NAME, async () => {
try {
Expand Down

0 comments on commit d7a088a

Please sign in to comment.