mattermost-mobile/app/push_notifications/push_notifications.ios.test.js
Miguel Alatzar 5e5d3abd79 [MM-17145] [MM-18947] [MM-17110] [MM-14926] [MM-18646] Use patched v2.0.6 of react-native-notifications and fix Android badge number (#3382)
* Refactor custom push notification code

* Use react-native-notifications 2.0.6 and patch for scheduled notifs

* Fix patch

* iOS changes

* Fix delete

* Fix setting of badge number on Android

* Undo Reflect removal

* Undo removal of didReceiveRemoteNotification

* Use min importance for push notifs received while app is active

* Correctly set badge number after push notificaiton reply

* Fix tests

* Localize reply action text

* Add getDeliveredNotifications

* Fix identifier check and failing test

* Fix local push notif test for Android > 9
2019-10-22 21:18:59 +03:00

93 lines
3.5 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import NotificationsIOS from 'react-native-notifications';
import PushNotification from './push_notifications.ios';
jest.mock('react-native-notifications', () => {
let badgesCount = 0;
let deliveredNotifications = {};
return {
getBadgesCount: jest.fn((callback) => callback(badgesCount)),
setBadgesCount: jest.fn((count) => {
badgesCount = count;
}),
addEventListener: jest.fn(),
setDeliveredNotifications: jest.fn((notifications) => {
deliveredNotifications = notifications;
}),
getDeliveredNotifications: jest.fn(async (callback) => {
await callback(deliveredNotifications);
}),
removeDeliveredNotifications: jest.fn((ids) => {
deliveredNotifications = deliveredNotifications.filter((n) => !ids.includes(n.identifier));
}),
cancelAllLocalNotifications: jest.fn(),
NotificationAction: jest.fn(),
NotificationCategory: jest.fn(),
};
});
describe('PushNotification', () => {
const channel1ID = 'channel-1-id';
const channel2ID = 'channel-2-id';
it('should clear channel notifications', async () => {
const deliveredNotifications = [
// Three channel1 delivered notifications
{
identifier: 'channel1-1',
channel_id: channel1ID,
},
{
identifier: 'channel1-2',
channel_id: channel1ID,
},
{
identifier: 'channel1-3',
channel_id: channel1ID,
},
// Two channel2 delivered notifications
{
identifier: 'channel2-1',
channel_id: channel2ID,
},
{
identifier: 'channel2-2',
channel_id: channel2ID,
},
];
NotificationsIOS.setDeliveredNotifications(deliveredNotifications);
const notificationCount = deliveredNotifications.length;
expect(notificationCount).toBe(5);
NotificationsIOS.setBadgesCount(notificationCount);
NotificationsIOS.getBadgesCount((count) => expect(count).toBe(notificationCount));
// Clear channel1 notifications
await PushNotification.clearChannelNotifications(channel1ID);
await NotificationsIOS.getDeliveredNotifications(async (deliveredNotifs) => {
expect(deliveredNotifs.length).toBe(2);
const channel1DeliveredNotifications = deliveredNotifs.filter((n) => n.channel_id === channel1ID);
const channel2DeliveredNotifications = deliveredNotifs.filter((n) => n.channel_id === channel2ID);
expect(channel1DeliveredNotifications.length).toBe(0);
expect(channel2DeliveredNotifications.length).toBe(2);
});
});
it('should clear all notifications', () => {
const setApplicationIconBadgeNumber = jest.spyOn(PushNotification, 'setApplicationIconBadgeNumber');
const cancelAllLocalNotifications = jest.spyOn(PushNotification, 'cancelAllLocalNotifications');
PushNotification.clearNotifications();
expect(setApplicationIconBadgeNumber).toHaveBeenCalledWith(0);
expect(NotificationsIOS.setBadgesCount).toHaveBeenCalledWith(0);
expect(cancelAllLocalNotifications).toHaveBeenCalled();
expect(NotificationsIOS.cancelAllLocalNotifications).toHaveBeenCalled();
});
});