mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +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 api from '../../../../../website/server/libs/payments';
|
||||
import analytics from '../../../../../website/server/libs/analyticsService';
|
||||
import notifications from '../../../../../website/server/libs/pushNotifications';
|
||||
import { model as User } from '../../../../../website/server/models/user';
|
||||
import moment from 'moment';
|
||||
|
||||
@@ -15,6 +16,7 @@ describe('payments/index', () => {
|
||||
sandbox.stub(user, 'sendMessage');
|
||||
sandbox.stub(analytics, 'trackPurchase');
|
||||
sandbox.stub(analytics, 'track');
|
||||
sandbox.stub(notifications, 'sendNotification');
|
||||
|
||||
data = {
|
||||
user,
|
||||
@@ -149,15 +151,15 @@ describe('payments/index', () => {
|
||||
it('sends an email about the gift', async () => {
|
||||
await api.createSubscription(data);
|
||||
|
||||
expect(sender.sendTxn).to.be.calledOnce;
|
||||
expect(sender.sendTxn).to.be.calledWith(recipient, 'gifted-subscription', [
|
||||
{name: 'GIFTER', content: 'sender'},
|
||||
{name: 'X_MONTHS_SUBSCRIPTION', content: 3},
|
||||
]);
|
||||
});
|
||||
|
||||
xit('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
|
||||
it('sends a push notification about the gift', async () => {
|
||||
await api.createSubscription(data);
|
||||
expect(notifications.sendNotification).to.be.calledOnce;
|
||||
});
|
||||
|
||||
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!\`');
|
||||
});
|
||||
|
||||
xit('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
|
||||
it('sends a push notification if user did not gift to self', async () => {
|
||||
await api.buyGems(data);
|
||||
expect(notifications.sendNotification).to.be.calledOnce;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ describe('pushNotifications', () => {
|
||||
send: apnSendSpy,
|
||||
});
|
||||
|
||||
sendPushNotification = requireAgain(pathToPushNotifications);
|
||||
sendPushNotification = requireAgain(pathToPushNotifications).sendNotification;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
import { removeFromArray } from '../../libs/collectionManipulators';
|
||||
import { sendTxn as sendTxnEmail } from '../../libs/email';
|
||||
import { encrypt } from '../../libs/encryption';
|
||||
import sendPushNotification from '../../libs/pushNotifications';
|
||||
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||
import pusher from '../../libs/pusher';
|
||||
|
||||
let api = {};
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
sendTxn as sendTxnEmail,
|
||||
} from '../../libs/email';
|
||||
import Bluebird from 'bluebird';
|
||||
import sendPushNotification from '../../libs/pushNotifications';
|
||||
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||
|
||||
let api = {};
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
sendTxn as sendTxnEmail,
|
||||
} from '../../libs/email';
|
||||
import common from '../../../../common';
|
||||
import sendPushNotification from '../../libs/pushNotifications';
|
||||
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
||||
|
||||
const questScrolls = common.content.quests;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
sendTxn as txnEmail,
|
||||
} from './email';
|
||||
import moment from 'moment';
|
||||
import sendPushNotification from './pushNotifications';
|
||||
import { sendNotification as sendPushNotification } from './pushNotifications';
|
||||
import shared from '../../../common' ;
|
||||
|
||||
let api = {};
|
||||
|
||||
@@ -59,8 +59,7 @@ if (APN_ENABLED) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function sendNotification (user, details = {}) {
|
||||
function sendNotification (user, details = {}) {
|
||||
if (!user) throw new Error('User is required.');
|
||||
if (user.preferences.pushNotifications.unsubscribeFromAll === true) return;
|
||||
let pushDevices = user.pushDevices.toObject ? user.pushDevices.toObject() : user.pushDevices;
|
||||
@@ -103,4 +102,8 @@ module.exports = function sendNotification (user, details = {}) {
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendNotification,
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
import { removeFromArray } from '../libs/collectionManipulators';
|
||||
import shared from '../../../common';
|
||||
import { sendTxn as txnEmail } from '../libs/email';
|
||||
import sendPushNotification from '../libs/pushNotifications';
|
||||
import { sendNotification as sendPushNotification } from '../libs/pushNotifications';
|
||||
import cwait from 'cwait';
|
||||
import { syncableAttrs } from '../libs/taskManager';
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import baseModel from '../libs/baseModel';
|
||||
import { sendTxn as sendTxnEmail } from '../libs/email';
|
||||
import Bluebird from 'bluebird';
|
||||
import nconf from 'nconf';
|
||||
import sendPushNotification from '../libs/pushNotifications';
|
||||
import { sendNotification as sendPushNotification } from '../libs/pushNotifications';
|
||||
import pusher from '../libs/pusher';
|
||||
import {
|
||||
syncableAttrs,
|
||||
|
||||
Reference in New Issue
Block a user