Commit 1a8268b7 authored by Fellipe Capelli's avatar Fellipe Capelli

Add test for push notification

parent 4cc4a041
...@@ -31,16 +31,17 @@ var basicRequest = function (url, apiKey, method, body, callback) { ...@@ -31,16 +31,17 @@ var basicRequest = function (url, apiKey, method, body, callback) {
if (body) { if (body) {
options.body = body; options.body = body;
options.json = true; options.json = true;
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
request(options, function (err, httpResponse, data) { request(options, function (err, httpResponse, data) {
if (err) { if (err) {
callback && callback(err, httpResponse, data); callback && callback(err, httpResponse, data);
reject(err); return reject(err);
} else {
callback && callback(err, httpResponse, data);
resolve({ httpResponse: httpResponse, data: data });
} }
callback && callback(err, httpResponse, data);
return resolve({ httpResponse: httpResponse, data: data });
}); });
}); });
}; };
...@@ -123,7 +124,7 @@ Client.prototype.sendNotification = function (notification, callback) { ...@@ -123,7 +124,7 @@ Client.prototype.sendNotification = function (notification, callback) {
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback); return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
} }
if (this.app) { 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); 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'; throw 'You must set either an "app" or "apps" on Client';
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
var expect = require('chai').expect; var expect = require('chai').expect;
var OneSignal = require('../lib'); var OneSignal = require('../lib');
var ClientMock = require('./mocks/client'); var ClientMock = require('./mocks/client');
var NotificationMock = require('./mocks/notification');
var Constants = require('../lib/constants'); var Constants = require('../lib/constants');
describe('Client Tests', function () { describe('Client Tests', function () {
...@@ -94,4 +95,33 @@ describe('Client Tests', function () { ...@@ -94,4 +95,33 @@ describe('Client Tests', function () {
expect(response.app.appId).to.equal(ClientMock.validSetApp.appId); 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');
}
})
})
}) })
...@@ -14,5 +14,13 @@ module.exports = { ...@@ -14,5 +14,13 @@ module.exports = {
}, },
validWithTemplateId: { validWithTemplateId: {
template_id: 'test' template_id: 'test'
},
invalidParameter: {
name: 'other',
value: 'other'
},
validParameter: {
name: 'filters',
value: 'test'
} }
} }
...@@ -59,4 +59,33 @@ describe('Notification Tests', function () { ...@@ -59,4 +59,33 @@ describe('Notification Tests', function () {
expect(response.postBody.template_id).to.equal(notification.template_id); 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);
})
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment