|
| 1 | +/* |
| 2 | + Mock for admin.messaging.Messaging |
| 3 | + https://firebase.google.com/docs/reference/admin/node/admin.messaging |
| 4 | +*/ |
1 | 5 | 'use strict'; |
| 6 | +var _ = require('./lodash'); |
| 7 | +var assert = require('assert'); |
| 8 | +var Promise = require('rsvp').Promise; |
| 9 | +var autoId = require('firebase-auto-ids'); |
| 10 | +var Queue = require('./queue').Queue; |
2 | 11 |
|
3 | 12 | function MockMessaging() { |
| 13 | + this.results = {}; |
| 14 | + this.callbacks = {}; |
| 15 | + this.queue = new Queue(); |
| 16 | + this.flushDelay = false; |
4 | 17 | } |
5 | 18 |
|
| 19 | +// https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging.html#send |
| 20 | +MockMessaging.prototype.send = function(message, dryRun) { |
| 21 | + assert(!_.isUndefined(message), 'message must not be undefined'); |
| 22 | + return this._send(_.toArray(arguments), 'send', function() { |
| 23 | + var messageId = autoId(new Date().getTime()); |
| 24 | + return messageId; |
| 25 | + }); |
| 26 | +}; |
| 27 | + |
| 28 | +// https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging.html#send-all |
| 29 | +MockMessaging.prototype.sendAll = function(messages, dryRun) { |
| 30 | + assert(Array.isArray(messages), 'messages must be an "Array"'); |
| 31 | + var self = this; |
| 32 | + return this._send(_.toArray(arguments), 'sendAll', function() { |
| 33 | + return self._mapMessagesToBatchResponse(messages); |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +// https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging.html#send-multicast |
| 38 | +MockMessaging.prototype.sendMulticast = function(multicastMessage, dryRun) { |
| 39 | + assert(!_.isUndefined(multicastMessage), 'multicastMessage must not be undefined'); |
| 40 | + assert(Array.isArray(multicastMessage.tokens)); |
| 41 | + var self = this; |
| 42 | + return this._send(_.toArray(arguments), 'sendMulticast', function() { |
| 43 | + return self._mapMessagesToBatchResponse(multicastMessage.tokens); |
| 44 | + }); |
| 45 | +}; |
| 46 | + |
| 47 | +MockMessaging.prototype.flush = function(delay) { |
| 48 | + this.queue.flush(delay); |
| 49 | + return this; |
| 50 | +}; |
| 51 | + |
| 52 | +MockMessaging.prototype.autoFlush = function (delay) { |
| 53 | + if (_.isUndefined(delay)) { |
| 54 | + delay = true; |
| 55 | + } |
| 56 | + |
| 57 | + this.flushDelay = delay; |
| 58 | + |
| 59 | + return this; |
| 60 | +}; |
| 61 | + |
| 62 | +MockMessaging.prototype.failNext = function(methodName, err) { |
| 63 | + assert(_.isString(methodName), 'methodName must be a string'); |
| 64 | + assert(err instanceof Error, 'err must be an "Error" object'); |
| 65 | + this.results[methodName] = err; |
| 66 | +}; |
| 67 | + |
| 68 | +MockMessaging.prototype.respondNext = function(methodName, result) { |
| 69 | + assert(_.isString(methodName), 'methodName must be a string'); |
| 70 | + assert(!_.isUndefined(result), 'result must not be undefined'); |
| 71 | + assert(!(result instanceof Error), 'result must not be an "Error" object'); |
| 72 | + this.results[methodName] = result; |
| 73 | +}; |
| 74 | + |
| 75 | +MockMessaging.prototype.on = function(methodName, callback) { |
| 76 | + assert(_.isString(methodName), 'methodName must be a string'); |
| 77 | + assert(_.isFunction(callback), 'callback must be a function'); |
| 78 | + this.callbacks[methodName] = callback; |
| 79 | +}; |
| 80 | + |
| 81 | +MockMessaging.prototype._send = function(args, methodName, defaultResultFn) { |
| 82 | + var result = this._nextResult(methodName); |
| 83 | + var self = this; |
| 84 | + return new Promise(function (resolve, reject) { |
| 85 | + self._defer(methodName, args, function () { |
| 86 | + self._invokeOn(methodName, args); |
| 87 | + |
| 88 | + if (result === null) { |
| 89 | + resolve(defaultResultFn()); |
| 90 | + } else if (result instanceof Error) { |
| 91 | + reject(result); |
| 92 | + } else { |
| 93 | + resolve(result); |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | +MockMessaging.prototype._invokeOn = function(methodName, args) { |
| 100 | + var callback = this.callbacks[methodName]; |
| 101 | + if (callback) { |
| 102 | + callback(args); |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +MockMessaging.prototype._nextResult = function(type) { |
| 107 | + var result = this.results[type]; |
| 108 | + delete this.results[type]; |
| 109 | + return result || null; |
| 110 | +}; |
| 111 | + |
| 112 | +MockMessaging.prototype._defer = function(sourceMethod, sourceArgs, callback) { |
| 113 | + this.queue.push({ |
| 114 | + fn: callback, |
| 115 | + context: this, |
| 116 | + sourceData: { |
| 117 | + ref: this, |
| 118 | + method: sourceMethod, |
| 119 | + args: sourceArgs |
| 120 | + } |
| 121 | + }); |
| 122 | + if (this.flushDelay !== false) { |
| 123 | + this.flush(this.flushDelay); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +MockMessaging.prototype._mapMessagesToBatchResponse = function(messages) { |
| 128 | + return { |
| 129 | + failureCount: 0, |
| 130 | + successCount: messages.length, |
| 131 | + responses: messages.map(function(message) { |
| 132 | + return { |
| 133 | + error: undefined, |
| 134 | + messageId: autoId(new Date().getTime()), |
| 135 | + success: true, |
| 136 | + }; |
| 137 | + }) |
| 138 | + }; |
| 139 | +}; |
| 140 | + |
6 | 141 | module.exports = MockMessaging; |
0 commit comments