Skip to content

Commit 48313d7

Browse files
committed
Add callbacks to messagint for assertions
1 parent 7c1c36a commit 48313d7

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/messaging.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Queue = require('./queue').Queue;
1111

1212
function MockMessaging() {
1313
this.results = {};
14+
this.callbacks = {};
1415
this.queue = new Queue();
1516
this.flushDelay = false;
1617
}
@@ -20,7 +21,6 @@ MockMessaging.prototype.send = function(message, dryRun) {
2021
assert(!_.isUndefined(message), 'message must not be undefined');
2122
return this._send(_.toArray(arguments), 'send', function() {
2223
var messageId = autoId(new Date().getTime());
23-
console.log(typeof(result));
2424
return messageId;
2525
});
2626
};
@@ -70,11 +70,18 @@ MockMessaging.prototype.nextResult = function(methodName, result) {
7070
this.results[methodName] = result;
7171
};
7272

73+
MockMessaging.prototype.on = function(methodName, callback) {
74+
assert(_.isFunction(callback), 'callback must be a function');
75+
this.callbacks[methodName] = callback;
76+
};
77+
7378
MockMessaging.prototype._send = function(args, methodName, defaultResultFn) {
7479
var result = this._nextResult(methodName);
7580
var self = this;
7681
return new Promise(function (resolve, reject) {
7782
self._defer(methodName, args, function () {
83+
self._invokeOn(methodName, args);
84+
7885
if (result === null) {
7986
resolve(defaultResultFn());
8087
} else if (result instanceof Error) {
@@ -85,6 +92,13 @@ MockMessaging.prototype._send = function(args, methodName, defaultResultFn) {
8592
});
8693
});
8794
};
95+
96+
MockMessaging.prototype._invokeOn = function(methodName, args) {
97+
var callback = this.callbacks[methodName];
98+
if (callback) {
99+
callback(args);
100+
}
101+
};
88102

89103
MockMessaging.prototype._nextResult = function(type) {
90104
var err = this.results[type];

test/unit/messaging.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ describe('MockMessaging', function() {
4141
});
4242
});
4343

44+
describe('#on', function() {
45+
it('throws if callback is not a function', function() {
46+
expect(function() {
47+
messaging.on('send', 'not a function');
48+
}).to.throw();
49+
});
50+
51+
it('registers a callback', function() {
52+
var callback = function() {};
53+
messaging.on('send', callback);
54+
expect(messaging.callbacks.send).to.be.eql(callback);
55+
});
56+
});
57+
4458
describe('#send', function() {
4559
it('should check that message is not undefined', function() {
4660
expect(function() {
@@ -84,6 +98,19 @@ describe('MockMessaging', function() {
8498
done(err);
8599
});
86100
});
101+
102+
it('invokes callback with args', function(done) {
103+
var message = {
104+
message: 'foo'
105+
};
106+
messaging.on('send', function(args) {
107+
expect(args).to.have.lengthOf(1);
108+
expect(args[0]).to.be.eql(message);
109+
done();
110+
});
111+
messaging.send(message);
112+
messaging.flush();
113+
});
87114
});
88115

89116
describe('#sendAll', function() {
@@ -135,6 +162,17 @@ describe('MockMessaging', function() {
135162
done(err);
136163
});
137164
});
165+
166+
it('invokes callback with args', function(done) {
167+
var message = [{message: 'foobar'}];
168+
messaging.on('sendAll', function(args) {
169+
expect(args).to.have.lengthOf(1);
170+
expect(args[0]).to.be.eql(message);
171+
done();
172+
});
173+
messaging.sendAll(message);
174+
messaging.flush();
175+
});
138176
});
139177

140178
describe('#sendMulticast', function() {
@@ -195,5 +233,16 @@ describe('MockMessaging', function() {
195233
done(err);
196234
});
197235
});
236+
237+
it('invokes callback with args', function(done) {
238+
var message = {message: 'foobar', tokens: ['t1']};
239+
messaging.on('sendMulticast', function(args) {
240+
expect(args).to.have.lengthOf(1);
241+
expect(args[0]).to.be.eql(message);
242+
done();
243+
});
244+
messaging.sendMulticast(message);
245+
messaging.flush();
246+
});
198247
});
199248
});

tutorials/admin/messaging.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,39 @@ result.then(function (res) {
8989
console.assert(res.failureCount === 1, '1 message failed');
9090
console.assert(res.successCount === 1, '1 message succeeded');
9191
});
92+
```
93+
94+
## Assert arguments
95+
If you want to assert the arguments that were passed to any of the send-functions you can register a callback.
96+
97+
##### Source
98+
99+
```js
100+
var ref;
101+
var messageService = {
102+
messaging: function () {
103+
if (!ref) ref = firebase.messaging();
104+
return ref;
105+
},
106+
send: function () {
107+
var message = {
108+
data: {
109+
foo: 'bar'
110+
},
111+
...
112+
};
113+
messageService.ref().send();
114+
},
115+
}
116+
```
117+
118+
##### Test
119+
120+
```js
121+
MockFirebase.override();
122+
messageService.ref().on('send', function(args) {
123+
console.assert(args.data.foo === 'bar', 'message is coorect');
124+
});
125+
messageService.send();
126+
messageService.messaging().flush();
92127
```

0 commit comments

Comments
 (0)