Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Airship, {iOS} from '@ua/react-native-airship';
import shouldShowPushNotification from '@libs/Notification/PushNotification/shouldShowPushNotification';
import {getIsMuted} from '@libs/Sound/BaseSound';
import type ForegroundNotificationsModule from './types';

function configureForegroundNotifications() {
Expand All @@ -14,7 +15,15 @@ function configureForegroundNotifications() {

// Set a callback to override our foreground presentation per notification depending on the app's current state.
// Returning null keeps the default presentation. Returning [] uses no presentation (hides the notification).
Airship.push.iOS.setForegroundPresentationOptionsCallback((pushPayload) => Promise.resolve(shouldShowPushNotification(pushPayload) ? null : []));
Airship.push.iOS.setForegroundPresentationOptionsCallback((pushPayload) => {
if (!shouldShowPushNotification(pushPayload)) {
return Promise.resolve([]);
}
if (getIsMuted()) {
return Promise.resolve([iOS.ForegroundPresentationOption.List, iOS.ForegroundPresentationOption.Banner, iOS.ForegroundPresentationOption.Badge]);
}
return Promise.resolve(null);
});
}

function disableForegroundNotifications() {
Expand Down
84 changes: 84 additions & 0 deletions tests/unit/ForegroundNotificationsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type {PushPayload} from '@ua/react-native-airship';
import {iOS} from '@ua/react-native-airship';

const mockShouldShowPushNotification = jest.fn<boolean, [PushPayload]>();
jest.mock('@libs/Notification/PushNotification/shouldShowPushNotification', () => mockShouldShowPushNotification);

const mockGetIsMuted = jest.fn<boolean, []>();
jest.mock('@libs/Sound/BaseSound', () => ({
getIsMuted: mockGetIsMuted,
}));

const fakePushPayload: PushPayload = {
alert: 'test notification',
extras: {payload: '{}'},
notificationId: 'test-id',
title: 'Test',
subtitle: '',
};

type ForegroundCallback = (push: PushPayload) => Promise<iOS.ForegroundPresentationOption[] | null>;

function setupAndGetCallback(method: 'configureForegroundNotifications' | 'disableForegroundNotifications'): ForegroundCallback {
let callback: ForegroundCallback | undefined;

jest.isolateModules(() => {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const AirshipModule = require('@ua/react-native-airship') as {default: {push: {iOS: {setForegroundPresentationOptionsCallback: jest.Mock}}}};
const {setForegroundPresentationOptionsCallback} = AirshipModule.default.push.iOS;
setForegroundPresentationOptionsCallback.mockClear();

// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const ForegroundNotifications = require('@libs/Notification/PushNotification/ForegroundNotifications/index.ios') as {
default: {configureForegroundNotifications: () => void; disableForegroundNotifications: () => void};
};
ForegroundNotifications.default[method]();

const lastCall = setForegroundPresentationOptionsCallback.mock.calls.at(-1) as ForegroundCallback[] | undefined;
callback = lastCall?.[0];
});

if (!callback) {
throw new Error('Failed to capture foreground presentation options callback');
}
return callback;
}

describe('ForegroundNotifications iOS', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('returns empty array when notification should not be shown', async () => {
mockShouldShowPushNotification.mockReturnValue(false);
const callback = setupAndGetCallback('configureForegroundNotifications');

const result = await callback(fakePushPayload);
expect(result).toEqual([]);
});

it('returns null (default presentation with sound) when not muted', async () => {
mockShouldShowPushNotification.mockReturnValue(true);
mockGetIsMuted.mockReturnValue(false);
const callback = setupAndGetCallback('configureForegroundNotifications');

const result = await callback(fakePushPayload);
expect(result).toBeNull();
});

it('returns List, Banner, Badge without Sound when muted', async () => {
mockShouldShowPushNotification.mockReturnValue(true);
mockGetIsMuted.mockReturnValue(true);
const callback = setupAndGetCallback('configureForegroundNotifications');

const result = await callback(fakePushPayload);
expect(result).toEqual([iOS.ForegroundPresentationOption.List, iOS.ForegroundPresentationOption.Banner, iOS.ForegroundPresentationOption.Badge]);
});

it('disableForegroundNotifications sets callback that returns empty array', async () => {
const callback = setupAndGetCallback('disableForegroundNotifications');

const result = await callback(fakePushPayload);
expect(result).toEqual([]);
});
});
Loading