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
c97e559a
Commit
c97e559a
authored
Jul 09, 2019
by
yogevbd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix unit tests
parent
13863605
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
111 additions
and
98 deletions
+111
-98
example/index.ios.js
example/index.ios.js
+0
-2
lib/src/index.ios.js
lib/src/index.ios.js
+2
-2
lib/src/notification.ios.js
lib/src/notification.ios.js
+4
-4
test/index.android.spec.js
test/index.android.spec.js
+22
-27
test/index.ios.spec.js
test/index.ios.spec.js
+46
-61
test/notification.ios.spec.js
test/notification.ios.spec.js
+2
-2
wallaby.js
wallaby.js
+35
-0
No files found.
example/index.ios.js
View file @
c97e559a
...
...
@@ -37,8 +37,6 @@ class NotificationsExampleApp extends Component {
NotificationsIOS
.
addEventListener
(
'
remoteNotificationsRegistered
'
,
this
.
onPushRegistered
.
bind
(
this
));
NotificationsIOS
.
addEventListener
(
'
remoteNotificationsRegistrationFailed
'
,
this
.
onPushRegisteredFailed
.
bind
(
this
));
NotificationsIOS
.
consumeBackgroundQueue
();
NotificationsIOS
.
addEventListener
(
'
pushKitRegistered
'
,
this
.
onPushKitRegistered
.
bind
(
this
));
NotificationsIOS
.
registerPushKit
();
...
...
lib/src/index.ios.js
View file @
c97e559a
...
...
@@ -2,7 +2,7 @@
* @flow
*/
'
use strict
'
;
import
{
NativeModules
,
DeviceEventEmitter
,
NativeAppEventEmitter
}
from
'
react-native
'
;
import
{
NativeModules
,
DeviceEventEmitter
}
from
'
react-native
'
;
import
Map
from
'
core-js/library/es6/map
'
;
import
uuid
from
'
uuid
'
;
const
NativeRNNotifications
=
NativeModules
.
RNBridgeModule
;
// eslint-disable-line no-unused-vars
...
...
@@ -58,7 +58,7 @@ export default class NotificationsIOS {
static
addEventListener
(
type
:
string
,
handler
:
Function
)
{
if
(
_exportedEvents
.
indexOf
(
type
)
!==
-
1
)
{
let
listener
;
if
(
type
===
DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT
)
{
listener
=
DeviceEventEmitter
.
addListener
(
DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT
,
...
...
lib/src/notification.ios.js
View file @
c97e559a
...
...
@@ -22,7 +22,7 @@ export default class IOSNotification {
this
.
_badge
=
notification
.
aps
.
badge
;
this
.
_category
=
notification
.
managedAps
.
category
;
this
.
_type
=
'
managed
'
;
this
.
_thread
=
notification
.
aps
[
'
thread-id
'
]
;
this
.
_thread
=
notification
.
aps
.
thread
;
}
else
if
(
notification
.
aps
&&
notification
.
aps
.
alert
)
{
...
...
@@ -30,12 +30,12 @@ export default class IOSNotification {
this
.
_alert
=
notification
.
aps
.
alert
;
this
.
_sound
=
notification
.
aps
.
sound
;
this
.
_badge
=
notification
.
aps
.
badge
;
this
.
_category
=
notification
.
category
;
this
.
_category
=
notification
.
aps
.
category
;
this
.
_type
=
'
regular
'
;
this
.
_thread
=
notification
.
thread
;
this
.
_thread
=
notification
.
aps
.
thread
;
}
Object
.
keys
(
notification
).
filter
(
key
=>
key
!==
'
userInfo
'
).
forEach
(
key
=>
{
Object
.
keys
(
notification
).
filter
(
key
=>
key
!==
'
aps
'
).
forEach
(
key
=>
{
this
.
_data
[
key
]
=
notification
[
key
];
});
}
...
...
test/index.android.spec.js
View file @
c97e559a
describe
(
'
Notifications-Android
'
,
()
=>
{
let
refreshTokenStub
;
let
getInitialNotificationStub
;
let
postLocalNotificationStub
;
let
cancelLocalNotificationStub
;
let
deviceEventEmitterListenerStub
;
let
libUnderTest
;
let
deviceEventEmitterListenerStub
;
let
WixRNNotifications
;
beforeEach
(()
=>
{
refreshTokenStub
=
jest
.
fn
();
getInitialNotificationStub
=
jest
.
fn
();
postLocalNotificationStub
=
jest
.
fn
();
cancelLocalNotificationStub
=
jest
.
fn
();
deviceEventEmitterListenerStub
=
jest
.
fn
();
jest
.
mock
(
'
react-native
'
,
()
=>
{
return
{
NativeModules
:
{
WixRNNotifications
:
{
refreshToken
:
refreshTokenStub
,
getInitialNotification
:
getInitialNotificationStub
,
postLocalNotification
:
postLocalNotificationStub
,
cancelLocalNotification
:
cancelLocalNotificationStub
refreshToken
:
jest
.
fn
()
,
getInitialNotification
:
jest
.
fn
()
,
postLocalNotification
:
jest
.
fn
()
,
cancelLocalNotification
:
jest
.
fn
()
}
},
DeviceEventEmitter
:
{
addListener
:
deviceEventEmitterListenerStub
addListener
:
jest
.
fn
()
}
};
});
deviceEventEmitterListenerStub
=
require
(
'
react-native
'
).
DeviceEventEmitter
.
addListener
;
WixRNNotifications
=
require
(
'
react-native
'
).
NativeModules
.
WixRNNotifications
;
libUnderTest
=
require
(
'
../lib/src/index.android
'
);
});
...
...
@@ -154,17 +149,17 @@ describe('Notifications-Android', () => {
describe
(
'
Notification token
'
,
()
=>
{
it
(
'
should refresh notification token upon refreshing request by the user
'
,
()
=>
{
expect
(
refreshTokenStub
).
toHaveBeenCalledTimes
(
0
);
expect
(
WixRNNotifications
.
refreshToken
).
toHaveBeenCalledTimes
(
0
);
libUnderTest
.
NotificationsAndroid
.
refreshToken
();
expect
(
refreshTokenStub
).
toHaveBeenCalledTimes
(
1
);
expect
(
WixRNNotifications
.
refreshToken
).
toHaveBeenCalledTimes
(
1
);
});
});
describe
(
'
Initial notification API
'
,
()
=>
{
it
(
'
should return initial notification data if available
'
,
(
done
)
=>
{
expect
(
getInitialNotificationStub
).
toHaveBeenCalledTimes
(
0
);
expect
(
WixRNNotifications
.
getInitialNotification
).
toHaveBeenCalledTimes
(
0
);
const
rawNotification
=
{
foo
:
'
bar
'
};
getInitialNotificationStub
.
mockReturnValueOnce
(
Promise
.
resolve
(
rawNotification
));
WixRNNotifications
.
getInitialNotification
.
mockReturnValueOnce
(
Promise
.
resolve
(
rawNotification
));
libUnderTest
.
PendingNotifications
.
getInitialNotification
()
.
then
((
notification
)
=>
{
...
...
@@ -175,8 +170,8 @@ describe('Notifications-Android', () => {
});
it
(
'
should return empty notification if not available
'
,
(
done
)
=>
{
expect
(
getInitialNotificationStub
).
toHaveBeenCalledTimes
(
0
);
getInitialNotificationStub
.
mockReturnValueOnce
(
Promise
.
resolve
(
null
));
expect
(
WixRNNotifications
.
getInitialNotification
).
toHaveBeenCalledTimes
(
0
);
WixRNNotifications
.
getInitialNotification
.
mockReturnValueOnce
(
Promise
.
resolve
(
null
));
libUnderTest
.
PendingNotifications
.
getInitialNotification
()
.
then
((
notification
)
=>
{
...
...
@@ -195,15 +190,15 @@ describe('Notifications-Android', () => {
};
it
(
'
should get published when posted manually
'
,
()
=>
{
expect
(
postLocalNotificationStub
).
toHaveBeenCalledTimes
(
0
);
expect
(
WixRNNotifications
.
postLocalNotification
).
toHaveBeenCalledTimes
(
0
);
const
id
=
libUnderTest
.
NotificationsAndroid
.
localNotification
(
notification
);
expect
(
id
).
toBeDefined
();
expect
(
postLocalNotificationStub
).
toHaveBeenCalledWith
(
notification
,
id
);
expect
(
WixRNNotifications
.
postLocalNotification
).
toHaveBeenCalledWith
(
notification
,
id
);
});
it
(
'
should be called with a unique ID
'
,
()
=>
{
expect
(
postLocalNotificationStub
).
toHaveBeenCalledTimes
(
0
);
expect
(
WixRNNotifications
.
postLocalNotification
).
toHaveBeenCalledTimes
(
0
);
const
id
=
libUnderTest
.
NotificationsAndroid
.
localNotification
(
notification
);
const
id2
=
libUnderTest
.
NotificationsAndroid
.
localNotification
(
notification
);
...
...
@@ -213,11 +208,11 @@ describe('Notifications-Android', () => {
});
it
(
'
should be cancellable with an ID
'
,
()
=>
{
expect
(
cancelLocalNotificationStub
).
toHaveBeenCalledTimes
(
0
);
expect
(
WixRNNotifications
.
cancelLocalNotification
).
toHaveBeenCalledTimes
(
0
);
libUnderTest
.
NotificationsAndroid
.
cancelLocalNotification
(
666
);
expect
(
cancelLocalNotificationStub
).
toHaveBeenCalledWith
(
666
);
expect
(
WixRNNotifications
.
cancelLocalNotification
).
toHaveBeenCalledWith
(
666
);
});
});
});
test/index.ios.spec.js
View file @
c97e559a
...
...
@@ -4,7 +4,6 @@ describe('NotificationsIOS', () => {
'
remoteNotificationsRegistered
'
,
'
remoteNotificationsRegistrationFailed
'
,
'
notificationReceivedForeground
'
,
'
notificationReceivedBackground
'
,
'
notificationOpened
'
];
...
...
@@ -12,62 +11,56 @@ describe('NotificationsIOS', () => {
let
constantGuid
=
'
some-random-uuid
'
;
let
identifiers
=
[
'
some-random-uuid
'
,
'
other-random-uuid
'
];
let
someHandler
=
()
=>
{};
let
nativeAppAddEventListener
;
let
deviceAddEventListener
;
let
deviceRemoveEventListener
;
let
nativeAppRemoveEventListener
;
let
nativeModule
;
let
NativeAppEventEmitter
;
let
DeviceEventEmitter
;
beforeEach
(()
=>
{
deviceRemoveEventListener
=
jest
.
fn
();
nativeAppRemoveEventListener
=
jest
.
fn
();
nativeAppAddEventListener
=
jest
.
fn
(()
=>
{
return
{
remove
:
nativeAppRemoveEventListener
};
});
deviceAddEventListener
=
jest
.
fn
(()
=>
{
return
{
remove
:
deviceRemoveEventListener
};
});
const
RNBridgeModule
=
{
requestPermissionsWithCategories
:
jest
.
fn
(),
abandonPermissions
:
jest
.
fn
(),
registerPushKit
:
jest
.
fn
(),
backgroundTimeRemaining
:
jest
.
fn
(),
consumeBackgroundQueue
:
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
()
};
jest
.
mock
(
'
react-native
'
,
()
=>
{
const
RNBridgeModule
=
{
requestPermissionsWithCategories
:
jest
.
fn
(),
abandonPermissions
:
jest
.
fn
(),
registerPushKit
:
jest
.
fn
(),
backgroundTimeRemaining
:
jest
.
fn
(),
consumeBackgroundQueue
:
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
},
NativeAppEventEmitter
:
{
addListener
:
nativeAppAddEventListener
addListener
:
jest
.
fn
(()
=>
{
return
{
remove
:
jest
.
fn
()
};
})
},
DeviceEventEmitter
:
{
addListener
:
deviceAddEventListener
addListener
:
jest
.
fn
(()
=>
{
return
{
remove
:
jest
.
fn
()
};
})
}
};
});
nativeModule
=
RNBridgeModule
;
nativeModule
=
require
(
'
react-native
'
).
NativeModules
.
RNBridgeModule
;
NativeAppEventEmitter
=
require
(
'
react-native
'
).
NativeAppEventEmitter
;
DeviceEventEmitter
=
require
(
'
react-native
'
).
DeviceEventEmitter
;
jest
.
mock
(
'
uuid
'
,
()
=>
{
return
{
v4
:
()
=>
constantGuid
v4
:
()
=>
'
some-random-uuid
'
};
});
...
...
@@ -81,34 +74,42 @@ describe('NotificationsIOS', () => {
deviceEvents
.
forEach
(
event
=>
{
it
(
`should subscribe the given handler to device event:
${
event
}
`
,
()
=>
{
NotificationsIOS
.
addEventListener
(
event
,
someHandler
);
expect
(
deviceAddEventListener
).
toHaveBeenCalledWith
(
event
,
expect
.
any
(
Function
));
expect
(
DeviceEventEmitter
.
addListener
).
toHaveBeenCalledWith
(
event
,
expect
.
any
(
Function
));
});
});
it
(
'
should not subscribe to unknown device events
'
,
()
=>
{
NotificationsIOS
.
addEventListener
(
'
someUnsupportedEvent
'
,
someHandler
);
expect
(
deviceAddEvent
Listener
).
toHaveBeenCalledTimes
(
0
);
expect
(
DeviceEventEmitter
.
add
Listener
).
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
(
deviceRemoveEventListener
).
toHaveBeenCalledTimes
(
1
);
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
(
deviceRemoveEventListener
).
toHaveBeenCalledTimes
(
0
);
expect
(
removeCallback
).
toHaveBeenCalledTimes
(
0
);
});
});
...
...
@@ -148,22 +149,6 @@ describe('NotificationsIOS', () => {
expect
(
nativeModule
.
requestPermissionsWithCategories
).
toHaveBeenCalledWith
([]);
});
it
(
'
should subscribe to notificationActionReceived event once, with a single event handler
'
,
()
=>
{
NotificationsIOS
.
requestPermissions
([
someCategory
]);
expect
(
nativeAppAddEventListener
).
toHaveBeenCalledTimes
(
1
);
expect
(
nativeAppAddEventListener
).
toHaveBeenCalledWith
(
'
notificationActionReceived
'
,
expect
.
any
(
Function
));
});
});
describe
(
'
reset categories
'
,
()
=>
{
it
(
'
should remove notificationActionReceived event handler
'
,
()
=>
{
NotificationsIOS
.
requestPermissions
([
someCategory
]);
NotificationsIOS
.
resetCategories
();
expect
(
nativeAppRemoveEventListener
).
toHaveBeenCalledTimes
(
1
);
});
});
describe
(
'
get badges count
'
,
()
=>
{
...
...
test/notification.ios.spec.js
View file @
c97e559a
...
...
@@ -16,7 +16,7 @@ describe('iOS Notification Object', () => {
badge
:
someBadgeCount
,
sound
:
someSound
,
category
:
someCategory
,
'
thread-id
'
:
someThread
thread
:
someThread
},
key1
:
'
value1
'
,
key2
:
'
value2
'
...
...
@@ -33,7 +33,7 @@ describe('iOS Notification Object', () => {
badge
:
someBadgeCount
,
sound
:
someSound
,
category
:
someCategory
,
'
thread-id
'
:
someThread
thread
:
someThread
},
key1
:
'
value1
'
,
key2
:
'
value2
'
...
...
wallaby.js
0 → 100644
View file @
c97e559a
const
babelOptions
=
require
(
'
./package.json
'
).
babel
;
module
.
exports
=
function
(
wallaby
)
{
return
{
env
:
{
type
:
'
node
'
,
runner
:
'
node
'
},
testFramework
:
'
jest
'
,
files
:
[
'
package.json
'
,
'
lib/src/**/*.js
'
,
'
lib/src/**/*.ts
'
,
'
lib/src/**/*.tsx
'
],
tests
:
[
'
test/**/*.spec.js
'
],
compilers
:
{
'
**/*.js
'
:
wallaby
.
compilers
.
babel
(
babelOptions
),
'
**/*.ts?(x)
'
:
wallaby
.
compilers
.
typeScript
({
module
:
'
commonjs
'
,
jsx
:
'
React
'
})
},
setup
:
(
w
)
=>
{
w
.
testFramework
.
configure
(
require
(
'
./package.json
'
).
jest
);
}
};
};
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