Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-notifications
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
冷佳娟
react-native-notifications
Commits
e6281eb0
Commit
e6281eb0
authored
Sep 09, 2019
by
yogevbd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move ios and android commands to designated classes
parent
5cd92c2e
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
148 additions
and
689 deletions
+148
-689
docs/android-api.md
docs/android-api.md
+1
-1
docs/general-api.md
docs/general-api.md
+2
-2
docs/ios-api.md
docs/ios-api.md
+16
-9
example/index.js
example/index.js
+1
-1
lib/src/Notifications.ts
lib/src/Notifications.ts
+7
-64
lib/src/NotificationsAndroid.ts
lib/src/NotificationsAndroid.ts
+23
-0
lib/src/NotificationsIOS.ts
lib/src/NotificationsIOS.ts
+89
-0
lib/src/adapters/NativeCommandsSender.ts
lib/src/adapters/NativeCommandsSender.ts
+5
-0
lib/src/commands/Commands.ts
lib/src/commands/Commands.ts
+4
-0
test/index.android.spec.js
test/index.android.spec.js
+0
-218
test/index.ios.spec.js
test/index.ios.spec.js
+0
-268
test/notification.ios.spec.js
test/notification.ios.spec.js
+0
-126
No files found.
docs/android-api.md
View file @
e6281eb0
...
...
@@ -8,5 +8,5 @@ sidebar_label: Android specific
refreshToken
```
js
Notifications
.
refreshToken
();
Notifications
.
android
.
refreshToken
();
```
docs/general-api.md
View file @
e6281eb0
...
...
@@ -8,7 +8,7 @@ sidebar_label: General
Return the notification that caused the app to launch from dead state.
```
js
const
notification
:
Notification
=
await
getInitialNotification
();
const
notification
:
Notification
=
await
Notifications
.
getInitialNotification
();
```
## postLocalNotification(notification, id?)
...
...
@@ -36,5 +36,5 @@ Notifications.cancelLocalNotification(id);
Check if the app has permissions to send remote notifications.
```
js
const
hasPermissions
:
boolean
=
await
getInitialNotification
();
const
hasPermissions
:
boolean
=
await
Notifications
.
getInitialNotification
();
```
docs/ios-api.md
View file @
e6281eb0
...
...
@@ -8,61 +8,68 @@ sidebar_label: iOS specific
request permissions
```
js
Notifications
.
requestPermissions
();
Notifications
.
ios
.
requestPermissions
();
```
## checkPermissions
checkPermissions
```
js
Notifications
.
checkPermissions
();
Notifications
.
ios
.
checkPermissions
();
```
## abandonPermissions
Unregister for all remote notifications received via Apple Push Notification service
```
js
Notifications
.
ios
.
abandonPermissions
();
```
## registerPushKit
registerPushKit
```
js
Notifications
.
registerPushKit
();
Notifications
.
ios
.
registerPushKit
();
```
## cancelAllLocalNotifications
cancelAllLocalNotifications
```
js
Notifications
.
cancelAllLocalNotifications
();
Notifications
.
ios
.
cancelAllLocalNotifications
();
```
## getDeliveredNotifications
getDeliveredNotifications
```
js
Notifications
.
getDeliveredNotifications
();
Notifications
.
ios
.
getDeliveredNotifications
();
```
## removeAllDeliveredNotifications
removeAllDeliveredNotifications
```
js
Notifications
.
removeAllDeliveredNotifications
();
Notifications
.
ios
.
removeAllDeliveredNotifications
();
```
## removeDeliveredNotifications
removeDeliveredNotifications
```
js
Notifications
.
removeDeliveredNotifications
();
Notifications
.
ios
.
removeDeliveredNotifications
();
```
## getBadgeCount
getBadgeCount
```
js
Notifications
.
getBadgeCount
();
Notifications
.
ios
.
getBadgeCount
();
```
## setBadgeCount
setBadgeCount
```
js
Notifications
.
setBadgeCount
(
1
);
Notifications
.
ios
.
setBadgeCount
(
1
);
```
\ No newline at end of file
example/index.js
View file @
e6281eb0
...
...
@@ -48,7 +48,7 @@ class NotificationsExampleApp extends Component {
}
requestPermissions
()
{
Notifications
.
requestPermissions
();
Notifications
.
ios
.
requestPermissions
();
}
setCategories
()
{
...
...
lib/src/Notifications.ts
View file @
e6281eb0
...
...
@@ -6,8 +6,13 @@ import { Notification } from './DTO/Notification';
import
{
UniqueIdProvider
}
from
'
./adapters/UniqueIdProvider
'
;
import
{
CompletionCallbackWrapper
}
from
'
./adapters/CompletionCallbackWrapper
'
;
import
{
NotificationCategory
}
from
'
./interfaces/NotificationCategory
'
;
import
{
NotificationsIOS
}
from
'
./NotificationsIOS
'
;
import
{
NotificationsAndroid
}
from
'
./NotificationsAndroid
'
;
export
class
NotificationsRoot
{
public
readonly
ios
:
NotificationsIOS
;
public
readonly
android
:
NotificationsAndroid
;
private
readonly
nativeEventsReceiver
:
NativeEventsReceiver
;
private
readonly
nativeCommandsSender
:
NativeCommandsSender
;
private
readonly
commands
:
Commands
;
...
...
@@ -25,20 +30,9 @@ export class NotificationsRoot {
this
.
uniqueIdProvider
);
this
.
eventsRegistry
=
new
EventsRegistry
(
this
.
nativeEventsReceiver
,
this
.
completionCallbackWrapper
);
}
/**
* Request permissions to send remote notifications - iOS only
*/
public
requestPermissions
()
{
return
this
.
commands
.
requestPermissions
();
}
/**
* registerPushKit - iOS only
*/
public
registerPushKit
()
{
return
this
.
commands
.
registerPushKit
();
this
.
ios
=
new
NotificationsIOS
(
this
.
commands
);
this
.
android
=
new
NotificationsAndroid
(
this
.
commands
);
}
/**
...
...
@@ -62,21 +56,6 @@ export class NotificationsRoot {
this
.
commands
.
setCategories
(
categories
);
}
/**
* getBadgesCount
*/
public
getBadgeCount
():
Promise
<
number
>
{
return
this
.
commands
.
getBadgeCount
();
}
/**
* setBadgeCount
* @param count number of the new badge count
*/
public
setBadgeCount
(
count
:
number
)
{
return
this
.
commands
.
setBadgeCount
(
count
);
}
/**
* cancelLocalNotification
*/
...
...
@@ -84,13 +63,6 @@ export class NotificationsRoot {
return
this
.
commands
.
cancelLocalNotification
(
notificationId
);
}
/**
* cancelAllLocalNotifications
*/
public
cancelAllLocalNotifications
()
{
this
.
commands
.
cancelAllLocalNotifications
();
}
/**
* isRegisteredForRemoteNotifications
*/
...
...
@@ -98,39 +70,10 @@ export class NotificationsRoot {
return
this
.
commands
.
isRegisteredForRemoteNotifications
();
}
/**
* checkPermissions
*/
public
checkPermissions
()
{
return
this
.
commands
.
checkPermissions
();
}
/**
* removeAllDeliveredNotifications
*/
public
removeAllDeliveredNotifications
()
{
return
this
.
commands
.
removeAllDeliveredNotifications
();
}
/**
* removeDeliveredNotifications
* @param identifiers Array of notification identifiers
*/
public
removeDeliveredNotifications
(
identifiers
:
Array
<
string
>
)
{
return
this
.
commands
.
removeDeliveredNotifications
(
identifiers
);
}
/**
* Obtain the events registry instance
*/
public
events
():
EventsRegistry
{
return
this
.
eventsRegistry
;
}
/**
* getDeliveredNotifications
*/
public
getDeliveredNotifications
():
Array
<
Notification
>
{
return
this
.
commands
.
getDeliveredNotifications
();
}
}
lib/src/NotificationsAndroid.ts
0 → 100644
View file @
e6281eb0
import
{
Commands
}
from
'
./commands/Commands
'
;
import
{
Platform
}
from
'
react-native
'
;
export
class
NotificationsAndroid
{
constructor
(
private
readonly
commands
:
Commands
)
{
return
new
Proxy
(
this
,
{
get
(
target
,
name
)
{
if
(
Platform
.
OS
===
'
android
'
)
{
return
(
target
as
any
)[
name
];
}
else
{
return
()
=>
{};
}
}
});
}
/**
* Refresh FCM token
*/
public
refreshToken
()
{
return
this
.
commands
.
refreshToken
();
}
}
lib/src/NotificationsIOS.ts
0 → 100644
View file @
e6281eb0
import
{
Notification
}
from
'
./DTO/Notification
'
;
import
{
Commands
}
from
'
./commands/Commands
'
;
import
{
Platform
}
from
'
react-native
'
;
export
class
NotificationsIOS
{
constructor
(
private
readonly
commands
:
Commands
)
{
return
new
Proxy
(
this
,
{
get
(
target
,
name
)
{
if
(
Platform
.
OS
===
'
ios
'
)
{
return
(
target
as
any
)[
name
];
}
else
{
return
()
=>
{};
}
}
});
}
/**
* Request permissions to send remote notifications
*/
public
requestPermissions
()
{
return
this
.
commands
.
requestPermissions
();
}
/**
* Unregister for all remote notifications received via Apple Push Notification service
*/
public
abandonPermissions
()
{
return
this
.
commands
.
abandonPermissions
();
}
/**
* registerPushKit
*/
public
registerPushKit
()
{
return
this
.
commands
.
registerPushKit
();
}
/**
* getBadgesCount
*/
public
getBadgeCount
():
Promise
<
number
>
{
return
this
.
commands
.
getBadgeCount
();
}
/**
* setBadgeCount
* @param count number of the new badge count
*/
public
setBadgeCount
(
count
:
number
)
{
return
this
.
commands
.
setBadgeCount
(
count
);
}
/**
* cancelAllLocalNotifications
*/
public
cancelAllLocalNotifications
()
{
this
.
commands
.
cancelAllLocalNotifications
();
}
/**
* checkPermissions
*/
public
checkPermissions
()
{
return
this
.
commands
.
checkPermissions
();
}
/**
* removeAllDeliveredNotifications
*/
public
removeAllDeliveredNotifications
()
{
return
this
.
commands
.
removeAllDeliveredNotifications
();
}
/**
* removeDeliveredNotifications
* @param identifiers Array of notification identifiers
*/
public
removeDeliveredNotifications
(
identifiers
:
Array
<
string
>
)
{
return
this
.
commands
.
removeDeliveredNotifications
(
identifiers
);
}
/**
* getDeliveredNotifications
*/
public
getDeliveredNotifications
():
Array
<
Notification
>
{
return
this
.
commands
.
getDeliveredNotifications
();
}
}
lib/src/adapters/NativeCommandsSender.ts
View file @
e6281eb0
...
...
@@ -9,6 +9,7 @@ interface NativeCommandsModule {
postLocalNotification
(
notification
:
Notification
,
id
:
number
):
void
;
requestPermissions
():
void
;
abandonPermissions
():
void
;
refreshToken
():
void
;
registerPushKit
():
void
;
getBadgeCount
():
Promise
<
number
>
;
setBadgeCount
(
count
:
number
):
void
;
...
...
@@ -46,6 +47,10 @@ export class NativeCommandsSender {
return
this
.
nativeCommandsModule
.
abandonPermissions
();
}
refreshToken
()
{
return
this
.
nativeCommandsModule
.
refreshToken
();
}
registerPushKit
()
{
return
this
.
nativeCommandsModule
.
registerPushKit
();
}
...
...
lib/src/commands/Commands.ts
View file @
e6281eb0
...
...
@@ -75,4 +75,8 @@ export class Commands {
public
getDeliveredNotifications
():
Array
<
Notification
>
{
return
this
.
nativeCommandsSender
.
getDeliveredNotifications
();
}
public
refreshToken
()
{
return
this
.
nativeCommandsSender
.
refreshToken
();
}
}
test/index.android.spec.js
deleted
100644 → 0
View file @
5cd92c2e
describe
(
'
Notifications-Android
'
,
()
=>
{
let
libUnderTest
;
let
deviceEventEmitterListenerStub
;
let
WixRNNotifications
;
beforeEach
(()
=>
{
jest
.
mock
(
'
react-native
'
,
()
=>
{
return
{
NativeModules
:
{
WixRNNotifications
:
{
refreshToken
:
jest
.
fn
(),
getInitialNotification
:
jest
.
fn
(),
postLocalNotification
:
jest
.
fn
(),
cancelLocalNotification
:
jest
.
fn
()
}
},
DeviceEventEmitter
:
{
addListener
:
jest
.
fn
()
}
};
});
deviceEventEmitterListenerStub
=
require
(
'
react-native
'
).
DeviceEventEmitter
.
addListener
;
WixRNNotifications
=
require
(
'
react-native
'
).
NativeModules
.
WixRNNotifications
;
libUnderTest
=
require
(
'
../lib/src/index.android
'
);
});
describe
(
'
Registration token API
'
,
()
=>
{
it
(
'
should assign callback to native event upon listener registration
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListener
=
()
=>
{};
libUnderTest
.
NotificationsAndroid
.
setRegistrationTokenUpdateListener
(
userListener
);
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledWith
(
'
remoteNotificationsRegistered
'
,
userListener
);
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
1
);
});
it
(
'
should clear native event listener upon listener deregister
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListener
=
()
=>
{};
const
nativeListener
=
{
remove
:
jest
.
fn
()
};
deviceEventEmitterListenerStub
.
mockReturnValueOnce
(
nativeListener
);
libUnderTest
.
NotificationsAndroid
.
setRegistrationTokenUpdateListener
(
userListener
);
libUnderTest
.
NotificationsAndroid
.
clearRegistrationTokenUpdateListener
();
expect
(
nativeListener
.
remove
).
toHaveBeenCalledTimes
(
1
);
});
it
(
'
shouldn`t fail if deregister without registering
'
,
()
=>
{
libUnderTest
.
NotificationsAndroid
.
clearRegistrationTokenUpdateListener
();
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
});
});
describe
(
'
notification-opening API
'
,
()
=>
{
it
(
'
should assign callback to native event upon registration
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListenerStub
=
jest
.
fn
();
libUnderTest
.
NotificationsAndroid
.
setNotificationOpenedListener
(
userListenerStub
);
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
1
);
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledWith
(
'
notificationOpened
'
,
expect
.
any
(
Function
));
});
it
(
'
should assign a wrapper-callback upon registration
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListenerStub
=
jest
.
fn
();
const
notification
=
{
foo
:
'
bar
'
};
libUnderTest
.
NotificationsAndroid
.
setNotificationOpenedListener
(
userListenerStub
);
expect
(
userListenerStub
).
toHaveBeenCalledTimes
(
0
);
deviceEventEmitterListenerStub
.
mock
.
calls
[
0
][
1
](
notification
);
expect
(
userListenerStub
).
toHaveBeenCalledTimes
(
1
);
expect
(
userListenerStub
.
mock
.
calls
[
0
][
0
].
getData
()).
toEqual
(
notification
);
});
it
(
'
should clear native event listener upon listener deregister
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListener
=
()
=>
{};
const
nativeListener
=
{
remove
:
jest
.
fn
()
};
deviceEventEmitterListenerStub
.
mockReturnValueOnce
(
nativeListener
);
libUnderTest
.
NotificationsAndroid
.
setNotificationOpenedListener
(
userListener
);
libUnderTest
.
NotificationsAndroid
.
clearNotificationOpenedListener
();
expect
(
nativeListener
.
remove
).
toHaveBeenCalledTimes
(
1
);
});
it
(
'
shouldnt fail if deregister without registering
'
,
()
=>
{
libUnderTest
.
NotificationsAndroid
.
clearNotificationOpenedListener
();
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
});
});
describe
(
'
notification-receive API
'
,
()
=>
{
it
(
'
should assign callback to native event upon registration
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListenerStub
=
jest
.
fn
();
libUnderTest
.
NotificationsAndroid
.
setNotificationReceivedListener
(
userListenerStub
);
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
1
);
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledWith
(
'
notificationReceived
'
,
expect
.
any
(
Function
));
});
it
(
'
should assign a wrapper-callback upon registration
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListenerStub
=
jest
.
fn
();
const
notification
=
{
foo
:
'
bar
'
};
libUnderTest
.
NotificationsAndroid
.
setNotificationReceivedListener
(
userListenerStub
);
expect
(
userListenerStub
).
toHaveBeenCalledTimes
(
0
);
deviceEventEmitterListenerStub
.
mock
.
calls
[
0
][
1
](
notification
);
expect
(
userListenerStub
).
toHaveBeenCalledTimes
(
1
);
expect
(
userListenerStub
.
mock
.
calls
[
0
][
0
].
getData
()).
toEqual
(
notification
);
});
it
(
'
should clear native event listener upon listener deregister
'
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
const
userListener
=
()
=>
{};
const
nativeListener
=
{
remove
:
jest
.
fn
()
};
deviceEventEmitterListenerStub
.
mockReturnValueOnce
(
nativeListener
);
libUnderTest
.
NotificationsAndroid
.
setNotificationReceivedListener
(
userListener
);
libUnderTest
.
NotificationsAndroid
.
clearNotificationReceivedListener
();
expect
(
nativeListener
.
remove
).
toHaveBeenCalledTimes
(
1
);
});
it
(
'
shouldn`t fail if deregister without registering
'
,
()
=>
{
libUnderTest
.
NotificationsAndroid
.
clearNotificationReceivedListener
();
expect
(
deviceEventEmitterListenerStub
).
toHaveBeenCalledTimes
(
0
);
});
});
describe
(
'
Notification token
'
,
()
=>
{
it
(
'
should refresh notification token upon refreshing request by the user
'
,
()
=>
{
expect
(
WixRNNotifications
.
refreshToken
).
toHaveBeenCalledTimes
(
0
);
libUnderTest
.
NotificationsAndroid
.
refreshToken
();
expect
(
WixRNNotifications
.
refreshToken
).
toHaveBeenCalledTimes
(
1
);
});
});
describe
(
'
Initial notification API
'
,
()
=>
{
it
(
'
should return initial notification data if available
'
,
(
done
)
=>
{
expect
(
WixRNNotifications
.
getInitialNotification
).
toHaveBeenCalledTimes
(
0
);
const
rawNotification
=
{
foo
:
'
bar
'
};
WixRNNotifications
.
getInitialNotification
.
mockReturnValueOnce
(
Promise
.
resolve
(
rawNotification
));
libUnderTest
.
PendingNotifications
.
getInitialNotification
()
.
then
((
notification
)
=>
{
expect
(
notification
.
getData
()).
toEqual
(
rawNotification
);
done
();
})
.
catch
((
err
)
=>
done
(
err
));
});
it
(
'
should return empty notification if not available
'
,
(
done
)
=>
{
expect
(
WixRNNotifications
.
getInitialNotification
).
toHaveBeenCalledTimes
(
0
);
WixRNNotifications
.
getInitialNotification
.
mockReturnValueOnce
(
Promise
.
resolve
(
null
));
libUnderTest
.
PendingNotifications
.
getInitialNotification
()
.
then
((
notification
)
=>
{
expect
(
notification
).
toBeUndefined
();
done
();
})
.
catch
((
err
)
=>
done
(
err
));
});
});
describe
(
'
Local notification
'
,
()
=>
{
const
notification
=
{
title
:
'
notification-title
'
,
body
:
'
notification-body
'
};
it
(
'
should get published when posted manually
'
,
()
=>
{
expect
(
WixRNNotifications
.
postLocalNotification
).
toHaveBeenCalledTimes
(
0
);
const
id
=
libUnderTest
.
NotificationsAndroid
.
localNotification
(
notification
);
expect
(
id
).
toBeDefined
();
expect
(
WixRNNotifications
.
postLocalNotification
).
toHaveBeenCalledWith
(
notification
,
id
);
});
it
(
'
should be called with a unique ID
'
,
()
=>
{
expect
(
WixRNNotifications
.
postLocalNotification
).
toHaveBeenCalledTimes
(
0
);
const
id
=
libUnderTest
.
NotificationsAndroid
.
localNotification
(
notification
);
const
id2
=
libUnderTest
.
NotificationsAndroid
.
localNotification
(
notification
);
expect
(
id
).
toBeDefined
();
expect
(
id2
).
toBeDefined
();
expect
(
id
).
not
.
toBe
(
id2
);
});
it
(
'
should be cancellable with an ID
'
,
()
=>
{
expect
(
WixRNNotifications
.
cancelLocalNotification
).
toHaveBeenCalledTimes
(
0
);
libUnderTest
.
NotificationsAndroid
.
cancelLocalNotification
(
666
);
expect
(
WixRNNotifications
.
cancelLocalNotification
).
toHaveBeenCalledWith
(
666
);
});
});
});
test/index.ios.spec.js
deleted
100644 → 0
View file @
5cd92c2e
describe
(
'
NotificationsIOS
'
,
()
=>
{
let
deviceEvents
=
[
'
pushKitRegistered
'
,
'
remoteNotificationsRegistered
'
,
'
remoteNotificationsRegistrationFailed
'
,
'
notificationReceivedForeground
'
,
'
notificationOpened
'
];
let
NotificationsIOS
,
NotificationAction
,
NotificationCategory
;
let
constantGuid
=
'
some-random-uuid
'
;
let
identifiers
=
[
'
some-random-uuid
'
,
'
other-random-uuid
'
];
let
someHandler
=
()
=>
{};
let
nativeModule
;
let
DeviceEventEmitter
;
beforeEach
(()
=>
{
jest
.
mock
(
'
react-native
'
,
()
=>
{
const
RNBridgeModule
=
{
requestPermissionsWithCategories
:
jest
.
fn
(),
abandonPermissions
:
jest
.
fn
(),
registerPushKit
:
jest
.
fn
(),
backgroundTimeRemaining
:
jest
.
fn
(),
localNotification
:
jest
.
fn
(),
cancelLocalNotification
:
jest
.
fn
(),
cancelAllLocalNotifications
:
jest
.
fn
(),
getBadgesCount
:
jest
.
fn
(),
setBadgesCount
:
jest
.
fn
(),
isRegisteredForRemoteNotifications
:
jest
.
fn
(),
checkPermissions
:
jest
.
fn
(),
removeAllDeliveredNotifications
:
jest
.
fn
(),
removeDeliveredNotifications
:
jest
.
fn
(),
getDeliveredNotifications
:
jest
.
fn
()
};
return
{
NativeModules
:
{
RNBridgeModule
},
DeviceEventEmitter
:
{
addListener
:
jest
.
fn
(()
=>
{
return
{
remove
:
jest
.
fn
()
};
})
}
};
});
nativeModule
=
require
(
'
react-native
'
).
NativeModules
.
RNBridgeModule
;
DeviceEventEmitter
=
require
(
'
react-native
'
).
DeviceEventEmitter
;
jest
.
mock
(
'
uuid
'
,
()
=>
{
return
{
v4
:
()
=>
'
some-random-uuid
'
};
});
let
libUnderTest
=
require
(
'
../lib/src/index.ios
'
);
NotificationsIOS
=
libUnderTest
.
default
;
NotificationAction
=
libUnderTest
.
NotificationAction
;
NotificationCategory
=
libUnderTest
.
NotificationCategory
;
});
describe
(
'
Add Event Listener
'
,
()
=>
{
deviceEvents
.
forEach
(
event
=>
{
it
(
`should subscribe the given handler to device event:
${
event
}
`
,
()
=>
{
NotificationsIOS
.
addEventListener
(
event
,
someHandler
);
expect
(
DeviceEventEmitter
.
addListener
).
toHaveBeenCalledWith
(
event
,
expect
.
any
(
Function
));
});
});
it
(
'
should not subscribe to unknown device events
'
,
()
=>
{
NotificationsIOS
.
addEventListener
(
'
someUnsupportedEvent
'
,
someHandler
);
expect
(
DeviceEventEmitter
.
addListener
).
toHaveBeenCalledTimes
(
0
);
});
});
describe
(
'
Remove Event Listener
'
,
()
=>
{
deviceEvents
.
forEach
(
event
=>
{
it
(
`should unsubscribe the given handler from device event:
${
event
}
`
,
()
=>
{
const
removeCallback
=
jest
.
fn
();
DeviceEventEmitter
.
addListener
.
mockReturnValueOnce
({
remove
:
removeCallback
});
NotificationsIOS
.
addEventListener
(
event
,
someHandler
);
NotificationsIOS
.
removeEventListener
(
event
,
someHandler
);
expect
(
removeCallback
).
toHaveBeenCalledTimes
(
1
);
});
});
it
(
'
should not unsubscribe to unknown device events
'
,
()
=>
{
let
someUnsupportedEvent
=
'
someUnsupportedEvent
'
;
const
removeCallback
=
jest
.
fn
();
DeviceEventEmitter
.
addListener
.
mockReturnValueOnce
({
remove
:
removeCallback
});
NotificationsIOS
.
addEventListener
(
someUnsupportedEvent
,
someHandler
);
NotificationsIOS
.
removeEventListener
(
someUnsupportedEvent
,
someHandler
);
expect
(
removeCallback
).
toHaveBeenCalledTimes
(
0
);
});
});
describe
(
'
Notification actions handling
'
,
()
=>
{
let
someAction
,
someCategory
;
let
actionOpts
=
{
activationMode
:
'
foreground
'
,
title
:
'
someAction
'
,
behavior
:
'
default
'
,
identifier
:
'
SOME_ACTION
'
};
beforeEach
(()
=>
{
someAction
=
new
NotificationAction
(
actionOpts
,
()
=>
{});
someCategory
=
new
NotificationCategory
({
identifier
:
'
SOME_CATEGORY
'
,
actions
:
[
someAction
],
context
:
'
default
'
});
});
describe
(
'
register push notifications
'
,
()
=>
{
it
(
'
should call native request permissions with array of categories
'
,
()
=>
{
NotificationsIOS
.
requestPermissions
([
someCategory
]);
expect
(
nativeModule
.
requestPermissionsWithCategories
).
toHaveBeenCalledWith
([{
identifier
:
'
SOME_CATEGORY
'
,
actions
:
[
actionOpts
],
context
:
'
default
'
}]);
});
it
(
'
should call native request permissions with empty array if no categories specified
'
,
()
=>
{
NotificationsIOS
.
requestPermissions
();
expect
(
nativeModule
.
requestPermissionsWithCategories
).
toHaveBeenCalledWith
([]);
});
});
describe
(
'
get badges count
'
,
()
=>
{
it
(
'
should call native getBadgesCount
'
,
()
=>
{
const
callback
=
(
count
)
=>
console
.
log
(
count
);
NotificationsIOS
.
getBadgesCount
(
callback
);
expect
(
nativeModule
.
getBadgesCount
).
toHaveBeenCalledWith
(
callback
);
});
});
describe
(
'
set badges count
'
,
()
=>
{
it
(
'
should call native setBadgesCount
'
,
()
=>
{
NotificationsIOS
.
setBadgesCount
(
44
);
expect
(
nativeModule
.
setBadgesCount
).
toHaveBeenCalledWith
(
44
);
});
});
});
describe
(
'
register push kit for background notifications
'
,
function
()
{
it
(
'
should call native register push kit method
'
,
function
()
{
NotificationsIOS
.
registerPushKit
();
expect
(
nativeModule
.
registerPushKit
).
toHaveBeenCalledTimes
(
1
);
});
});
describe
(
'
Abandon push notifications permissions
'
,
()
=>
{
it
(
'
should call native abandon permissions method
'
,
()
=>
{
NotificationsIOS
.
abandonPermissions
();
expect
(
nativeModule
.
abandonPermissions
).
toHaveBeenCalledTimes
(
1
);
});
});
describe
(
'
Get background remaining time
'
,
()
=>
{
it
(
'
should call native background remaining time method
'
,
()
=>
{
let
someCallback
=
()
=>
{};
NotificationsIOS
.
backgroundTimeRemaining
(
someCallback
);
expect
(
nativeModule
.
backgroundTimeRemaining
).
toHaveBeenCalledWith
(
someCallback
);
});
});
describe
(
'
Dispatch local notification
'
,
()
=>
{
it
(
'
should return generated notification guid
'
,
()
=>
{
expect
(
NotificationsIOS
.
localNotification
({})).
toEqual
(
constantGuid
);
});
it
(
'
should call native local notification method with generated notification guid and notification object
'
,
()
=>
{
let
someLocalNotification
=
{
body
:
'
some body
'
,
title
:
'
some title
'
,
alertAction
:
'
some action
'
,
sound
:
'
sound
'
,
category
:
'
SOME_CATEGORY
'
,
userInfo
:
{
'
key
'
:
'
value
'
}
};
NotificationsIOS
.
localNotification
(
someLocalNotification
);
expect
(
nativeModule
.
localNotification
).
toHaveBeenCalledWith
(
someLocalNotification
,
constantGuid
);
});
});
describe
(
'
Cancel local notification
'
,
()
=>
{
it
(
'
should call native cancel local notification method
'
,
()
=>
{
NotificationsIOS
.
cancelLocalNotification
(
constantGuid
);
expect
(
nativeModule
.
cancelLocalNotification
).
toHaveBeenCalledWith
(
constantGuid
);
});
});
describe
(
'
Cancel all local notifications
'
,
()
=>
{
it
(
'
should call native cancel all local notifications method
'
,
()
=>
{
NotificationsIOS
.
cancelAllLocalNotifications
();
expect
(
nativeModule
.
cancelAllLocalNotifications
).
toHaveBeenCalledWith
();
});
});
describe
(
'
Is registered for remote notifications
'
,
()
=>
{
it
(
'
should call native is registered for remote notifications
'
,
()
=>
{
NotificationsIOS
.
isRegisteredForRemoteNotifications
();
expect
(
nativeModule
.
isRegisteredForRemoteNotifications
).
toHaveBeenCalledWith
();
});
});
describe
(
'
Check permissions
'
,
()
=>
{
it
(
'
should call native check permissions
'
,
()
=>
{
NotificationsIOS
.
checkPermissions
();
expect
(
nativeModule
.
checkPermissions
).
toHaveBeenCalledWith
();
});
});
describe
(
'
Remove all delivered notifications
'
,
()
=>
{
it
(
'
should call native remove all delivered notifications method
'
,
()
=>
{
NotificationsIOS
.
removeAllDeliveredNotifications
();
expect
(
nativeModule
.
removeAllDeliveredNotifications
).
toHaveBeenCalledWith
();
});
});
describe
(
'
Remove delivered notifications
'
,
()
=>
{
it
(
'
should call native remove delivered notifications method
'
,
()
=>
{
NotificationsIOS
.
removeDeliveredNotifications
(
identifiers
);
expect
(
nativeModule
.
removeDeliveredNotifications
).
toHaveBeenCalledWith
(
identifiers
);
});
});
describe
(
'
Get delivered notifications
'
,
()
=>
{
it
(
'
should call native get delivered notifications method
'
,
()
=>
{
const
callback
=
(
notifications
)
=>
console
.
log
(
notifications
);
NotificationsIOS
.
getDeliveredNotifications
(
callback
);
expect
(
nativeModule
.
getDeliveredNotifications
).
toHaveBeenCalledWith
(
callback
);
});
});
});
test/notification.ios.spec.js
deleted
100644 → 0
View file @
5cd92c2e
import
IOSNotification
from
'
../lib/src/notification.ios
'
;
describe
(
'
iOS Notification Object
'
,
()
=>
{
let
notification
;
let
someBadgeCount
=
123
,
someSound
=
'
someSound
'
,
someCategory
=
'
some_notification_category
'
,
someThread
=
'
thread-1
'
;
describe
(
'
for a regular iOS push notification
'
,
()
=>
{
let
regularNativeNotifications
=
[
// basic example, without content-available = 1 (aka silent notification)
{
aps
:
{
alert
:
{
title
:
'
some title
'
,
body
:
'
some body
'
},
badge
:
someBadgeCount
,
sound
:
someSound
,
category
:
someCategory
,
thread
:
someThread
},
key1
:
'
value1
'
,
key2
:
'
value2
'
},
// another example, with content-available but also with alert object (should not be a silent notification)
{
aps
:
{
'
content-available
'
:
1
,
alert
:
{
title
:
'
some title
'
,
body
:
'
some body
'
},
badge
:
someBadgeCount
,
sound
:
someSound
,
category
:
someCategory
,
thread
:
someThread
},
key1
:
'
value1
'
,
key2
:
'
value2
'
}
];
regularNativeNotifications
.
forEach
(
nativeNotification
=>
{
beforeEach
(()
=>
{
notification
=
new
IOSNotification
(
nativeNotification
);
});
it
(
'
should return regular type
'
,
function
()
{
expect
(
notification
.
getType
()).
toEqual
(
'
regular
'
);
});
it
(
'
should return the alert object
'
,
()
=>
{
expect
(
notification
.
getMessage
()).
toEqual
(
nativeNotification
.
aps
.
alert
);
});
it
(
'
should return the sound
'
,
()
=>
{
expect
(
notification
.
getSound
()).
toEqual
(
someSound
);
});
it
(
'
should return the badge count
'
,
()
=>
{
expect
(
notification
.
getBadgeCount
()).
toEqual
(
someBadgeCount
);
});
it
(
'
should return the category
'
,
()
=>
{
expect
(
notification
.
getCategory
()).
toEqual
(
someCategory
);
});
it
(
'
should return the thread
'
,
()
=>
{
expect
(
notification
.
getThread
()).
toEqual
(
'
thread-1
'
);
});
it
(
'
should return the custom data
'
,
()
=>
{
expect
(
notification
.
getData
()).
toEqual
({
key1
:
'
value1
'
,
key2
:
'
value2
'
});
});
});
});
describe
(
'
for a managed iOS push notification (silent notification, with managedAps key and content-available = 1)
'
,
()
=>
{
let
managedNativeNotification
=
{
aps
:
{
'
content-available
'
:
1
,
badge
:
someBadgeCount
},
managedAps
:
{
action
:
'
CREATE
'
,
notificationId
:
'
1
'
,
alert
:
{
title
:
'
some title
'
,
body
:
'
some body
'
},
sound
:
someSound
,
category
:
someCategory
},
key1
:
'
value1
'
,
key2
:
'
value2
'
};
beforeEach
(()
=>
{
notification
=
new
IOSNotification
(
managedNativeNotification
);
});
it
(
'
should return managed type
'
,
function
()
{
expect
(
notification
.
getType
()).
toEqual
(
'
managed
'
);
});
it
(
'
should return the alert object
'
,
()
=>
{
expect
(
notification
.
getMessage
()).
toEqual
(
managedNativeNotification
.
managedAps
.
alert
);
});
it
(
'
should return the sound
'
,
()
=>
{
expect
(
notification
.
getSound
()).
toEqual
(
someSound
);
});
it
(
'
should return the badge count
'
,
()
=>
{
expect
(
notification
.
getBadgeCount
()).
toEqual
(
someBadgeCount
);
});
it
(
'
should return the category
'
,
()
=>
{
expect
(
notification
.
getCategory
()).
toEqual
(
someCategory
);
});
it
(
'
should return the custom data
'
,
()
=>
{
expect
(
notification
.
getData
()).
toEqual
({
managedAps
:
managedNativeNotification
.
managedAps
,
key1
:
'
value1
'
,
key2
:
'
value2
'
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment