mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
fix: Update notifications lib to make it testable
closes #7770 closes #7782
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import * as sender from '../../../../../website/server/libs/email';
|
import * as sender from '../../../../../website/server/libs/email';
|
||||||
import * as api from '../../../../../website/server/libs/payments';
|
import * as api from '../../../../../website/server/libs/payments';
|
||||||
import analytics from '../../../../../website/server/libs/analyticsService';
|
import analytics from '../../../../../website/server/libs/analyticsService';
|
||||||
|
import notifications from '../../../../../website/server/libs/pushNotifications';
|
||||||
import { model as User } from '../../../../../website/server/models/user';
|
import { model as User } from '../../../../../website/server/models/user';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ describe('payments/index', () => {
|
|||||||
sandbox.stub(user, 'sendMessage');
|
sandbox.stub(user, 'sendMessage');
|
||||||
sandbox.stub(analytics, 'trackPurchase');
|
sandbox.stub(analytics, 'trackPurchase');
|
||||||
sandbox.stub(analytics, 'track');
|
sandbox.stub(analytics, 'track');
|
||||||
|
sandbox.stub(notifications, 'sendNotification');
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
user,
|
user,
|
||||||
@@ -149,15 +151,15 @@ describe('payments/index', () => {
|
|||||||
it('sends an email about the gift', async () => {
|
it('sends an email about the gift', async () => {
|
||||||
await api.createSubscription(data);
|
await api.createSubscription(data);
|
||||||
|
|
||||||
expect(sender.sendTxn).to.be.calledOnce;
|
|
||||||
expect(sender.sendTxn).to.be.calledWith(recipient, 'gifted-subscription', [
|
expect(sender.sendTxn).to.be.calledWith(recipient, 'gifted-subscription', [
|
||||||
{name: 'GIFTER', content: 'sender'},
|
{name: 'GIFTER', content: 'sender'},
|
||||||
{name: 'X_MONTHS_SUBSCRIPTION', content: 3},
|
{name: 'X_MONTHS_SUBSCRIPTION', content: 3},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('sends a push notification about the gift', async () => {
|
it('sends a push notification about the gift', async () => {
|
||||||
// TODO: the push notification library needs to be refactored to export an object with properties to better stub it
|
await api.createSubscription(data);
|
||||||
|
expect(notifications.sendNotification).to.be.calledOnce;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tracks subscription purchase as gift', async () => {
|
it('tracks subscription purchase as gift', async () => {
|
||||||
@@ -518,8 +520,9 @@ describe('payments/index', () => {
|
|||||||
expect(user.sendMessage).to.be.calledWith(recipient, '\`Hello recipient, sender has sent you 4 gems!\`');
|
expect(user.sendMessage).to.be.calledWith(recipient, '\`Hello recipient, sender has sent you 4 gems!\`');
|
||||||
});
|
});
|
||||||
|
|
||||||
xit('sends a push notification if user did not gift to self', async () => {
|
it('sends a push notification if user did not gift to self', async () => {
|
||||||
// TODO: the push notification library needs to be refactored to export an object with properties to better stub it
|
await api.buyGems(data);
|
||||||
|
expect(notifications.sendNotification).to.be.calledOnce;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ describe('pushNotifications', () => {
|
|||||||
send: apnSendSpy,
|
send: apnSendSpy,
|
||||||
});
|
});
|
||||||
|
|
||||||
sendPushNotification = requireAgain(pathToPushNotifications);
|
sendPushNotification = requireAgain(pathToPushNotifications).sendNotification;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import {
|
|||||||
import { removeFromArray } from '../../libs/collectionManipulators';
|
import { removeFromArray } from '../../libs/collectionManipulators';
|
||||||
import { sendTxn as sendTxnEmail } from '../../libs/email';
|
import { sendTxn as sendTxnEmail } from '../../libs/email';
|
||||||
import { encrypt } from '../../libs/encryption';
|
import { encrypt } from '../../libs/encryption';
|
||||||
import sendPushNotification from '../../libs/pushNotifications';
|
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||||
import pusher from '../../libs/pusher';
|
import pusher from '../../libs/pusher';
|
||||||
|
|
||||||
let api = {};
|
let api = {};
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
sendTxn as sendTxnEmail,
|
sendTxn as sendTxnEmail,
|
||||||
} from '../../libs/email';
|
} from '../../libs/email';
|
||||||
import Bluebird from 'bluebird';
|
import Bluebird from 'bluebird';
|
||||||
import sendPushNotification from '../../libs/pushNotifications';
|
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||||
|
|
||||||
let api = {};
|
let api = {};
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
sendTxn as sendTxnEmail,
|
sendTxn as sendTxnEmail,
|
||||||
} from '../../libs/email';
|
} from '../../libs/email';
|
||||||
import common from '../../../../common';
|
import common from '../../../../common';
|
||||||
import sendPushNotification from '../../libs/pushNotifications';
|
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||||
|
|
||||||
const questScrolls = common.content.quests;
|
const questScrolls = common.content.quests;
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
sendTxn as txnEmail,
|
sendTxn as txnEmail,
|
||||||
} from './email';
|
} from './email';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import sendPushNotification from './pushNotifications';
|
import { sendNotification as sendPushNotification } from './pushNotifications';
|
||||||
import shared from '../../../common' ;
|
import shared from '../../../common' ;
|
||||||
|
|
||||||
let api = {};
|
let api = {};
|
||||||
|
|||||||
@@ -59,8 +59,7 @@ if (APN_ENABLED) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function sendNotification (user, details = {}) {
|
||||||
module.exports = function sendNotification (user, details = {}) {
|
|
||||||
if (!user) throw new Error('User is required.');
|
if (!user) throw new Error('User is required.');
|
||||||
if (user.preferences.pushNotifications.unsubscribeFromAll === true) return;
|
if (user.preferences.pushNotifications.unsubscribeFromAll === true) return;
|
||||||
let pushDevices = user.pushDevices.toObject ? user.pushDevices.toObject() : user.pushDevices;
|
let pushDevices = user.pushDevices.toObject ? user.pushDevices.toObject() : user.pushDevices;
|
||||||
@@ -103,4 +102,8 @@ module.exports = function sendNotification (user, details = {}) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
sendNotification,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import {
|
|||||||
import { removeFromArray } from '../libs/collectionManipulators';
|
import { removeFromArray } from '../libs/collectionManipulators';
|
||||||
import shared from '../../../common';
|
import shared from '../../../common';
|
||||||
import { sendTxn as txnEmail } from '../libs/email';
|
import { sendTxn as txnEmail } from '../libs/email';
|
||||||
import sendPushNotification from '../libs/pushNotifications';
|
import { sendNotification as sendPushNotification } from '../libs/pushNotifications';
|
||||||
import cwait from 'cwait';
|
import cwait from 'cwait';
|
||||||
import { syncableAttrs } from '../libs/taskManager';
|
import { syncableAttrs } from '../libs/taskManager';
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import baseModel from '../libs/baseModel';
|
|||||||
import { sendTxn as sendTxnEmail } from '../libs/email';
|
import { sendTxn as sendTxnEmail } from '../libs/email';
|
||||||
import Bluebird from 'bluebird';
|
import Bluebird from 'bluebird';
|
||||||
import nconf from 'nconf';
|
import nconf from 'nconf';
|
||||||
import sendPushNotification from '../libs/pushNotifications';
|
import { sendNotification as sendPushNotification } from '../libs/pushNotifications';
|
||||||
import pusher from '../libs/pusher';
|
import pusher from '../libs/pusher';
|
||||||
import {
|
import {
|
||||||
syncableAttrs,
|
syncableAttrs,
|
||||||
|
|||||||
Reference in New Issue
Block a user