|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var chai = require('chai'); |
| 4 | +var sinon = require('sinon'); |
| 5 | + |
| 6 | +chai.use(require('chai-as-promised')); |
| 7 | +chai.use(require('sinon-chai')); |
| 8 | + |
| 9 | +var expect = chai.expect; |
| 10 | +var _ = require('../../src/lodash'); |
| 11 | +var Messaging = require('../../').MockMessaging; |
| 12 | + |
| 13 | +describe('MockMessaging', function() { |
| 14 | + |
| 15 | + var messaging; |
| 16 | + beforeEach(function() { |
| 17 | + messaging = new Messaging(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('#flush', function() { |
| 21 | + it('flushes the queue and returns itself', function() { |
| 22 | + sinon.stub(messaging.queue, 'flush'); |
| 23 | + expect(messaging.flush(10)).to.equal(messaging); |
| 24 | + expect(messaging.queue.flush).to.have.been.calledWith(10); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('#autoFlush', function() { |
| 29 | + it('enables autoflush with no args', function() { |
| 30 | + messaging.autoFlush(); |
| 31 | + expect(messaging.flushDelay).to.equal(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('can specify a flush delay', function() { |
| 35 | + messaging.autoFlush(10); |
| 36 | + expect(messaging.flushDelay).to.equal(10); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns itself', function() { |
| 40 | + expect(messaging.autoFlush()).to.equal(messaging); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('#send', function() { |
| 45 | + it('should check that message is not undefined', function() { |
| 46 | + expect(function() { |
| 47 | + messaging.send(); |
| 48 | + }).to.throw(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return thenable reference', function(done) { |
| 52 | + var thenable = messaging.send({ |
| 53 | + message: 'foo' |
| 54 | + }); |
| 55 | + thenable.then(function(result) { |
| 56 | + expect(result).to.be.a('string'); |
| 57 | + done(); |
| 58 | + }, done).catch(function(err) { |
| 59 | + done(err); |
| 60 | + }); |
| 61 | + messaging.flush(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('can simulate an error', function(done) { |
| 65 | + var err = new Error(); |
| 66 | + messaging.failNext('send', err); |
| 67 | + var thenable = messaging.send({message: 'foo'}); |
| 68 | + messaging.flush(); |
| 69 | + thenable.then(function() { |
| 70 | + done('send() should not resolve'); |
| 71 | + }).catch(function() { |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('can return user defined results', function(done) { |
| 77 | + messaging.nextResult('send', 'the result'); |
| 78 | + var thenable = messaging.send({message: 'foo'}); |
| 79 | + messaging.flush(); |
| 80 | + thenable.then(function(result) { |
| 81 | + expect(result).to.equal('the result'); |
| 82 | + done(); |
| 83 | + }).catch(function(err) { |
| 84 | + done(err); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('#sendAll', function() { |
| 90 | + it('should check that messages is an array', function() { |
| 91 | + expect(function() { |
| 92 | + messaging.sendAll('not an array'); |
| 93 | + }).to.throw(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return thenable reference', function(done) { |
| 97 | + var thenable = messaging.sendAll([{message: 'foobar'}]); |
| 98 | + thenable.then(function(result) { |
| 99 | + expect(result).to.be.not.undefined; // jshint ignore:line |
| 100 | + expect(result).to.be.not.null; // jshint ignore:line |
| 101 | + expect(result.failureCount).to.be.eql(0); |
| 102 | + expect(result.successCount).to.be.eql(1); |
| 103 | + expect(result.responses).to.be.an('array'); |
| 104 | + expect(result.responses).to.have.lengthOf(1); |
| 105 | + expect(result.responses[0].error).to.be.undefined; // jshint ignore:line |
| 106 | + expect(result.responses[0].messageId).to.be.a('string'); |
| 107 | + expect(result.responses[0].success).to.be.true; // jshint ignore:line |
| 108 | + done(); |
| 109 | + }, done).catch(function(err) { |
| 110 | + done(err); |
| 111 | + }); |
| 112 | + messaging.flush(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('can simulate an error', function(done) { |
| 116 | + var err = new Error(); |
| 117 | + messaging.failNext('sendAll', err); |
| 118 | + var thenable = messaging.sendAll([{message: 'foobar'}]); |
| 119 | + messaging.flush(); |
| 120 | + thenable.then(function() { |
| 121 | + done('sendAll() should not resolve'); |
| 122 | + }).catch(function() { |
| 123 | + done(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('can return user defined results', function(done) { |
| 128 | + messaging.nextResult('sendAll', 'the result'); |
| 129 | + var thenable = messaging.sendAll([{message: 'foobar'}]); |
| 130 | + messaging.flush(); |
| 131 | + thenable.then(function(result) { |
| 132 | + expect(result).to.equal('the result'); |
| 133 | + done(); |
| 134 | + }).catch(function(err) { |
| 135 | + done(err); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('#sendMulticast', function() { |
| 141 | + it('should check that message is not undefined', function() { |
| 142 | + expect(function() { |
| 143 | + messaging.sendMulticast(); |
| 144 | + }).to.throw(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should check that message contains a "tokens" array', function() { |
| 148 | + expect(function() { |
| 149 | + messaging.sendMulticast({message: 'foobar'}); |
| 150 | + }).to.throw(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should return thenable reference', function(done) { |
| 154 | + var thenable = messaging.sendMulticast({message: 'foobar', tokens: ['t1', 't2']}); |
| 155 | + thenable.then(function(result) { |
| 156 | + expect(result).to.be.not.undefined; // jshint ignore:line |
| 157 | + expect(result).to.be.not.null; // jshint ignore:line |
| 158 | + expect(result.failureCount).to.be.eql(0); |
| 159 | + expect(result.successCount).to.be.eql(2); |
| 160 | + expect(result.responses).to.be.an('array'); |
| 161 | + expect(result.responses).to.have.lengthOf(2); |
| 162 | + expect(result.responses[0].error).to.be.undefined; // jshint ignore:line |
| 163 | + expect(result.responses[0].messageId).to.be.a('string'); |
| 164 | + expect(result.responses[0].success).to.be.true; // jshint ignore:line |
| 165 | + expect(result.responses[1].error).to.be.undefined; // jshint ignore:line |
| 166 | + expect(result.responses[1].messageId).to.be.a('string'); |
| 167 | + expect(result.responses[1].success).to.be.true; // jshint ignore:line |
| 168 | + done(); |
| 169 | + }, done).catch(function(err) { |
| 170 | + done(err); |
| 171 | + }); |
| 172 | + messaging.flush(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('can simulate an error', function(done) { |
| 176 | + var err = new Error(); |
| 177 | + messaging.failNext('sendMulticast', err); |
| 178 | + var thenable = messaging.sendMulticast({message: 'foobar', tokens: ['t1']}); |
| 179 | + messaging.flush(); |
| 180 | + thenable.then(function() { |
| 181 | + done('sendMulticast() should not resolve'); |
| 182 | + }).catch(function() { |
| 183 | + done(); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + it('can return user defined results', function(done) { |
| 188 | + messaging.nextResult('sendMulticast', 'the result'); |
| 189 | + var thenable = messaging.sendMulticast({message: 'foobar', tokens: ['t1']}); |
| 190 | + messaging.flush(); |
| 191 | + thenable.then(function(result) { |
| 192 | + expect(result).to.equal('the result'); |
| 193 | + done(); |
| 194 | + }).catch(function(err) { |
| 195 | + done(err); |
| 196 | + }); |
| 197 | + }); |
| 198 | + }); |
| 199 | +}); |
0 commit comments