Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
onesignal-node
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Cedar Studios
onesignal-node
Commits
1a8268b7
Commit
1a8268b7
authored
Mar 16, 2018
by
Fellipe Capelli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test for push notification
parent
4cc4a041
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
6 deletions
+74
-6
client.js
lib/client.js
+7
-6
client.test.js
test/client.test.js
+30
-0
notification.js
test/mocks/notification.js
+8
-0
notification.test.js
test/notification.test.js
+29
-0
No files found.
lib/client.js
View file @
1a8268b7
...
...
@@ -31,16 +31,17 @@ var basicRequest = function (url, apiKey, method, body, callback) {
if
(
body
)
{
options
.
body
=
body
;
options
.
json
=
true
;
}
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
request
(
options
,
function
(
err
,
httpResponse
,
data
)
{
if
(
err
)
{
callback
&&
callback
(
err
,
httpResponse
,
data
);
reject
(
err
);
}
else
{
callback
&&
callback
(
err
,
httpResponse
,
data
);
resolve
({
httpResponse
:
httpResponse
,
data
:
data
});
return
reject
(
err
);
}
callback
&&
callback
(
err
,
httpResponse
,
data
);
return
resolve
({
httpResponse
:
httpResponse
,
data
:
data
});
});
});
};
...
...
@@ -123,7 +124,7 @@ Client.prototype.sendNotification = function (notification, callback) {
return
basicRequest
(
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
,
this
.
userAuthKey
,
'POST'
,
postBody
,
callback
);
}
if
(
this
.
app
)
{
postBody
.
app_id
=
this
.
app
.
appId
;
postBody
.
app_id
=
this
.
app
.
appId
;
return
basicRequest
(
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
,
this
.
app
.
appAuthKey
,
'POST'
,
postBody
,
callback
);
}
throw
'You must set either an "app" or "apps" on Client'
;
...
...
test/client.test.js
View file @
1a8268b7
...
...
@@ -3,6 +3,7 @@
var
expect
=
require
(
'chai'
).
expect
;
var
OneSignal
=
require
(
'../lib'
);
var
ClientMock
=
require
(
'./mocks/client'
);
var
NotificationMock
=
require
(
'./mocks/notification'
);
var
Constants
=
require
(
'../lib/constants'
);
describe
(
'Client Tests'
,
function
()
{
...
...
@@ -94,4 +95,33 @@ describe('Client Tests', function () {
expect
(
response
.
app
.
appId
).
to
.
equal
(
ClientMock
.
validSetApp
.
appId
);
})
})
describe
(
'Send Notification'
,
function
()
{
it
(
'Expect to throw an error when sending a notification withou a notification object'
,
function
()
{
var
client
=
ClientMock
.
validClient
;
var
clientObject
=
new
OneSignal
.
Client
(
client
);
var
notification
=
NotificationMock
.
emptyNotification
;
try
{
var
response
=
clientObject
.
sendNotification
(
notification
);
expect
(
response
).
to
.
equal
(
undefined
);
}
catch
(
err
)
{
expect
(
err
).
to
.
be
.
an
(
'string'
);
expect
(
err
).
to
.
equal
(
'notification parameter must be a typeof Notification object.'
);
}
})
it
(
'Expect to throw an error when sending a notification for client without app'
,
function
()
{
var
client
=
ClientMock
.
validEmptyClient
;
var
clientObject
=
new
OneSignal
.
Client
(
client
);
var
notification
=
NotificationMock
.
validWithContents
;
var
notificationObject
=
new
OneSignal
.
Notification
(
notification
);
try
{
var
response
=
clientObject
.
sendNotification
(
notificationObject
);
expect
(
response
).
to
.
equal
(
undefined
);
}
catch
(
err
)
{
expect
(
err
).
to
.
be
.
an
(
'string'
);
expect
(
err
).
to
.
equal
(
'You must set either an "app" or "apps" on Client'
);
}
})
})
})
test/mocks/notification.js
View file @
1a8268b7
...
...
@@ -14,5 +14,13 @@ module.exports = {
},
validWithTemplateId
:
{
template_id
:
'test'
},
invalidParameter
:
{
name
:
'other'
,
value
:
'other'
},
validParameter
:
{
name
:
'filters'
,
value
:
'test'
}
}
test/notification.test.js
View file @
1a8268b7
...
...
@@ -59,4 +59,33 @@ describe('Notification Tests', function () {
expect
(
response
.
postBody
.
template_id
).
to
.
equal
(
notification
.
template_id
);
})
})
describe
(
'Setting OneSignal Properties'
,
function
()
{
it
(
'Expect to throw an error when setting invalid parameter'
,
function
()
{
var
notification
=
NotificationMock
.
validWithContents
;
var
notificationObject
=
new
OneSignal
.
Notification
(
notification
);
try
{
var
parameter
=
NotificationMock
.
invalidParameter
;
notificationObject
.
setParameter
(
parameter
.
name
,
parameter
.
value
);
expect
(
response
).
to
.
equal
(
undefined
);
}
catch
(
err
)
{
expect
(
err
).
to
.
be
.
an
(
'string'
);
expect
(
err
).
to
.
equal
(
'"other" is not present in documentation. You should add a exclamation'
.
concat
(
' mark to the begging of the name, if you want to set it : !other'
));
}
})
it
(
'Expect to valid data when setting parameter'
,
function
()
{
var
notification
=
NotificationMock
.
validWithContents
;
var
notificationObject
=
new
OneSignal
.
Notification
(
notification
);
var
parameter
=
NotificationMock
.
validParameter
;
notificationObject
.
setParameter
(
parameter
.
name
,
parameter
.
value
);
var
response
=
notificationObject
;
expect
(
response
).
to
.
be
.
an
(
'object'
);
expect
(
response
.
postBody
).
to
.
be
.
an
(
'object'
);
expect
(
response
.
postBody
.
filters
).
to
.
be
.
an
(
'string'
);
expect
(
response
.
postBody
.
filters
).
to
.
equal
(
parameter
.
value
);
})
})
})
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