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
d7697c11
Commit
d7697c11
authored
Nov 23, 2016
by
d4vidi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add JS-lib unit tests (Android)
parent
db74bd66
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
210 additions
and
18 deletions
+210
-18
index.android.js
index.android.js
+20
-18
test/index.android.spec.js
test/index.android.spec.js
+190
-0
No files found.
index.android.js
View file @
d7697c11
...
@@ -8,26 +8,10 @@ let notificationOpenedListener;
...
@@ -8,26 +8,10 @@ let notificationOpenedListener;
let
registrationTokenUpdateListener
;
let
registrationTokenUpdateListener
;
export
class
NotificationsAndroid
{
export
class
NotificationsAndroid
{
static
setRegistrationTokenUpdateListener
(
listener
)
{
registrationTokenUpdateListener
=
DeviceEventEmitter
.
addListener
(
"
remoteNotificationsRegistered
"
,
listener
);
}
static
clearRegistrationTokenUpdateListener
()
{
if
(
registrationTokenUpdateListener
)
{
registrationTokenUpdateListener
.
remove
();
registrationTokenUpdateListener
=
null
;
}
}
static
setNotificationOpenedListener
(
listener
)
{
static
setNotificationOpenedListener
(
listener
)
{
notificationOpenedListener
=
DeviceEventEmitter
.
addListener
(
"
notificationOpened
"
,
(
notification
)
=>
listener
(
new
NotificationAndroid
(
notification
)));
notificationOpenedListener
=
DeviceEventEmitter
.
addListener
(
"
notificationOpened
"
,
(
notification
)
=>
listener
(
new
NotificationAndroid
(
notification
)));
}
}
static
setNotificationReceivedListener
(
listener
)
{
notificationReceivedListener
=
DeviceEventEmitter
.
addListener
(
"
notificationReceived
"
,
(
notification
)
=>
listener
(
new
NotificationAndroid
(
notification
)));
}
static
clearNotificationOpenedListener
()
{
static
clearNotificationOpenedListener
()
{
if
(
notificationOpenedListener
)
{
if
(
notificationOpenedListener
)
{
notificationOpenedListener
.
remove
();
notificationOpenedListener
.
remove
();
...
@@ -35,6 +19,10 @@ export class NotificationsAndroid {
...
@@ -35,6 +19,10 @@ export class NotificationsAndroid {
}
}
}
}
static
setNotificationReceivedListener
(
listener
)
{
notificationReceivedListener
=
DeviceEventEmitter
.
addListener
(
"
notificationReceived
"
,
(
notification
)
=>
listener
(
new
NotificationAndroid
(
notification
)));
}
static
clearNotificationReceivedListener
()
{
static
clearNotificationReceivedListener
()
{
if
(
notificationReceivedListener
)
{
if
(
notificationReceivedListener
)
{
notificationReceivedListener
.
remove
();
notificationReceivedListener
.
remove
();
...
@@ -42,13 +30,27 @@ export class NotificationsAndroid {
...
@@ -42,13 +30,27 @@ export class NotificationsAndroid {
}
}
}
}
static
setRegistrationTokenUpdateListener
(
listener
)
{
registrationTokenUpdateListener
=
DeviceEventEmitter
.
addListener
(
"
remoteNotificationsRegistered
"
,
listener
);
}
static
clearRegistrationTokenUpdateListener
()
{
if
(
registrationTokenUpdateListener
)
{
registrationTokenUpdateListener
.
remove
();
registrationTokenUpdateListener
=
null
;
}
}
static
refreshToken
()
{
static
refreshToken
()
{
RNNotifications
.
refreshToken
();
RNNotifications
.
refreshToken
();
}
}
}
}
export
class
PendingNotifications
{
export
class
PendingNotifications
{
static
async
getInitialNotification
()
{
static
getInitialNotification
()
{
return
new
NotificationAndroid
(
await
RNNotifications
.
getInitialNotification
());
return
RNNotifications
.
getInitialNotification
()
.
then
((
rawNotification
)
=>
{
return
new
NotificationAndroid
(
rawNotification
);
});
}
}
}
}
test/index.android.spec.js
0 → 100644
View file @
d7697c11
"
use strict
"
;
let
expect
=
require
(
"
chai
"
).
use
(
require
(
"
sinon-chai
"
)).
expect
;
import
proxyquire
from
"
proxyquire
"
;
import
sinon
from
"
sinon
"
;
describe
(
"
Notifications-Android
"
,
()
=>
{
proxyquire
.
noCallThru
();
let
refreshTokenStub
;
let
getInitialNotificationStub
;
let
deviceEventEmitterListenerStub
;
let
libUnderTest
;
beforeEach
(()
=>
{
refreshTokenStub
=
sinon
.
stub
();
getInitialNotificationStub
=
sinon
.
stub
();
deviceEventEmitterListenerStub
=
sinon
.
stub
();
libUnderTest
=
proxyquire
(
"
../index.android
"
,
{
"
react-native
"
:
{
NativeModules
:
{
WixRNNotifications
:
{
refreshToken
:
refreshTokenStub
,
getInitialNotification
:
getInitialNotificationStub
}
},
DeviceEventEmitter
:
{
addListener
:
deviceEventEmitterListenerStub
}
},
"
./notification
"
:
require
(
"
../notification.android
"
)
});
});
describe
(
"
Registration token API
"
,
()
=>
{
it
(
"
should assign callback to native event upon listener registration
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListener
=
()
=>
{};
libUnderTest
.
NotificationsAndroid
.
setRegistrationTokenUpdateListener
(
userListener
);
expect
(
deviceEventEmitterListenerStub
).
to
.
have
.
been
.
calledWith
(
"
remoteNotificationsRegistered
"
,
userListener
);
expect
(
deviceEventEmitterListenerStub
).
to
.
have
.
been
.
calledOnce
;
});
it
(
"
should clear native event listener upon listener deregister
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListener
=
()
=>
{};
const
nativeListener
=
{
remove
:
sinon
.
spy
()
};
deviceEventEmitterListenerStub
.
returns
(
nativeListener
);
libUnderTest
.
NotificationsAndroid
.
setRegistrationTokenUpdateListener
(
userListener
);
libUnderTest
.
NotificationsAndroid
.
clearRegistrationTokenUpdateListener
();
expect
(
nativeListener
.
remove
).
to
.
have
.
been
.
calledOnce
;
});
it
(
"
shouldn't fail if deregister without registering
"
,
()
=>
{
libUnderTest
.
NotificationsAndroid
.
clearRegistrationTokenUpdateListener
();
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
});
});
describe
(
"
notification-opening API
"
,
()
=>
{
it
(
"
should assign callback to native event upon registration
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListenerStub
=
sinon
.
stub
();
libUnderTest
.
NotificationsAndroid
.
setNotificationOpenedListener
(
userListenerStub
);
expect
(
deviceEventEmitterListenerStub
).
to
.
have
.
been
.
calledOnce
;
expect
(
deviceEventEmitterListenerStub
).
to
.
have
.
been
.
calledWith
(
"
notificationOpened
"
,
sinon
.
match
.
func
);
});
it
(
"
should assign a wrapper-callback upon registration
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListenerStub
=
sinon
.
stub
();
const
notification
=
{
foo
:
"
bar
"
};
libUnderTest
.
NotificationsAndroid
.
setNotificationOpenedListener
(
userListenerStub
);
expect
(
userListenerStub
).
to
.
not
.
have
.
been
.
called
;
deviceEventEmitterListenerStub
.
args
[
0
][
1
](
notification
);
expect
(
userListenerStub
).
to
.
have
.
been
.
calledOnce
;
expect
(
userListenerStub
.
args
[
0
][
0
].
getData
()).
to
.
equal
(
notification
);
});
it
(
"
should clear native event listener upon listener deregister
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListener
=
()
=>
{};
const
nativeListener
=
{
remove
:
sinon
.
spy
()
};
deviceEventEmitterListenerStub
.
returns
(
nativeListener
);
libUnderTest
.
NotificationsAndroid
.
setNotificationOpenedListener
(
userListener
);
libUnderTest
.
NotificationsAndroid
.
clearNotificationOpenedListener
();
expect
(
nativeListener
.
remove
).
to
.
have
.
been
.
calledOnce
;
});
it
(
"
shouldn't fail if deregister without registering
"
,
()
=>
{
libUnderTest
.
NotificationsAndroid
.
clearNotificationOpenedListener
();
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
});
});
describe
(
"
notification-receive API
"
,
()
=>
{
it
(
"
should assign callback to native event upon registration
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListenerStub
=
sinon
.
stub
();
libUnderTest
.
NotificationsAndroid
.
setNotificationReceivedListener
(
userListenerStub
);
expect
(
deviceEventEmitterListenerStub
).
to
.
have
.
been
.
calledOnce
;
expect
(
deviceEventEmitterListenerStub
).
to
.
have
.
been
.
calledWith
(
"
notificationReceived
"
,
sinon
.
match
.
func
);
});
it
(
"
should assign a wrapper-callback upon registration
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListenerStub
=
sinon
.
stub
();
const
notification
=
{
foo
:
"
bar
"
};
libUnderTest
.
NotificationsAndroid
.
setNotificationReceivedListener
(
userListenerStub
);
expect
(
userListenerStub
).
to
.
not
.
have
.
been
.
called
;
deviceEventEmitterListenerStub
.
args
[
0
][
1
](
notification
);
expect
(
userListenerStub
).
to
.
have
.
been
.
calledOnce
;
expect
(
userListenerStub
.
args
[
0
][
0
].
getData
()).
to
.
equal
(
notification
);
});
it
(
"
should clear native event listener upon listener deregister
"
,
()
=>
{
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
const
userListener
=
()
=>
{};
const
nativeListener
=
{
remove
:
sinon
.
spy
()
};
deviceEventEmitterListenerStub
.
returns
(
nativeListener
);
libUnderTest
.
NotificationsAndroid
.
setNotificationReceivedListener
(
userListener
);
libUnderTest
.
NotificationsAndroid
.
clearNotificationReceivedListener
();
expect
(
nativeListener
.
remove
).
to
.
have
.
been
.
calledOnce
;
});
it
(
"
shouldn't fail if deregister without registering
"
,
()
=>
{
libUnderTest
.
NotificationsAndroid
.
clearNotificationReceivedListener
();
expect
(
deviceEventEmitterListenerStub
).
to
.
not
.
have
.
been
.
called
;
});
});
it
(
"
should refresh notification token upon refreshing request by the user
"
,
()
=>
{
expect
(
refreshTokenStub
).
to
.
not
.
have
.
been
.
called
;
libUnderTest
.
NotificationsAndroid
.
refreshToken
();
expect
(
refreshTokenStub
).
to
.
have
.
been
.
calledOnce
;
});
describe
(
"
Initial notification API
"
,
()
=>
{
it
(
"
should return initial notification data if available
"
,
(
done
)
=>
{
expect
(
getInitialNotificationStub
).
to
.
not
.
have
.
been
.
called
;
const
rawNotification
=
{
foo
:
"
bar
"
};
getInitialNotificationStub
.
returns
(
Promise
.
resolve
(
rawNotification
));
libUnderTest
.
PendingNotifications
.
getInitialNotification
()
.
then
((
notification
)
=>
{
expect
(
notification
.
getData
()).
to
.
equal
(
rawNotification
);
done
();
})
.
catch
((
err
)
=>
done
(
err
));
});
it
(
"
should return empty notification data if not available
"
,
(
done
)
=>
{
expect
(
getInitialNotificationStub
).
to
.
not
.
have
.
been
.
called
;
getInitialNotificationStub
.
returns
(
Promise
.
resolve
(
null
));
libUnderTest
.
PendingNotifications
.
getInitialNotification
()
.
then
((
notification
)
=>
{
expect
(
notification
.
getData
()).
to
.
equal
(
null
);
done
();
})
.
catch
((
err
)
=>
done
(
err
));
});
});
});
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