Skip to content

Commit 891551e

Browse files
committed
Make docs match tests
1 parent f687e89 commit 891551e

File tree

3 files changed

+72
-107
lines changed

3 files changed

+72
-107
lines changed

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,4 @@ When `methodName` is next invoked, the `Promise` will be rejected with the speci
273273

274274
When `methodName` is next invoked, the `callback` will be triggered. The callback gets an array as argument. The array contains all arguments, that were passed on invoking `methodName`. This is useful to assert the input arguments of `methodName`.
275275

276-
See [docs.js](/test/unit/docs.js) for an example.
276+
See [docs.js](/test/unit/docs.js) for an example.

test/unit/docs.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
const MockUser = require('../../src/user');
1+
'use strict';
2+
3+
/* jshint browser:true */
4+
/* globals expect:false */
25

36
describe('API.md', () => {
47
describe('Auth example for changeAuthState', () => {
58

69
let ref;
710
beforeEach(() => {
8-
const Authentication = require('../../').MockAuthentication;
11+
const Authentication = require('../../src').MockAuthentication;
912
ref = new Authentication();
1013
});
1114

1215
it('works as described', () => {
16+
const MockUser = require('../../src/user');
1317
ref.changeAuthState(new MockUser(ref, {
1418
uid: 'theUid',
1519
email: 'me@example.com',
@@ -39,10 +43,11 @@ describe('API.md', () => {
3943
});
4044

4145
describe('Messaging examples', () => {
42-
let ref;
46+
let mockMessaging;
47+
4348
beforeEach(() => {
4449
const Messaging = require('../../').MockMessaging;
45-
ref = new Messaging();
50+
mockMessaging = new Messaging();
4651
});
4752

4853
it('send messages', () => {
@@ -52,8 +57,8 @@ describe('API.md', () => {
5257
body: 'foobar'
5358
}
5459
};
55-
var result = ref.send(message);
56-
ref.flush();
60+
var result = mockMessaging.send(message);
61+
mockMessaging.flush();
5762
result.then(function (messageId) {
5863
console.assert(messageId !== '', 'message id is ' + messageId);
5964
});
@@ -88,9 +93,9 @@ describe('API.md', () => {
8893
],
8994
successCount: 1,
9095
};
91-
ref.respondNext('sendAll', batchResponse);
92-
var result = ref.sendAll(messages);
93-
ref.flush();
96+
mockMessaging.respondNext('sendAll', batchResponse);
97+
var result = mockMessaging.sendAll(messages);
98+
mockMessaging.flush();
9499
result.then(function (response) {
95100
console.assert(response === batchResponse, 'custom batch response is returned');
96101
});
@@ -103,11 +108,11 @@ describe('API.md', () => {
103108
body: 'foobar'
104109
}
105110
};
106-
ref.on('send', function(args) {
107-
console.assert(args[0] === message, 'message argument is coorect');
111+
mockMessaging.on('send', function(args) {
112+
console.assert(args[0] === message, 'message argument is correct');
108113
});
109-
ref.send(message);
110-
ref.flush();
114+
mockMessaging.send(message);
115+
mockMessaging.flush();
111116
});
112117
});
113118
});

tutorials/admin/messaging.md

Lines changed: 53 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,20 @@ MockFirebase replaces most of Firebase's messaging method of the admin API with
44

55
## Send message
66

7-
In this example, we'll create send a new message using Firebase messaging.
8-
9-
##### Source
10-
11-
```js
12-
var ref;
13-
var messageService = {
14-
messaging: function () {
15-
if (!ref) ref = firebase.messaging();
16-
return ref;
17-
},
18-
send: function () {
19-
var message = {
20-
21-
};
22-
messageService.ref().send();
23-
},
24-
}
25-
```
26-
27-
##### Test
7+
In this example, we'll send a new message using Firebase messaging.
288

299
```js
30-
MockFirebase.override();
31-
var result = messageService.send();
32-
messageService.messaging().flush();
10+
var mockMessaging = new MockMessaging();
11+
var message = {
12+
notification: {
13+
title: 'message title',
14+
body: 'foobar'
15+
}
16+
};
17+
var result = mockMessaging.send(message);
18+
mockMessaging.flush();
3319
result.then(function (messageId) {
34-
console.log(messageId);
20+
console.assert(messageId !== '', 'message id is ' + messageId);
3521
});
3622
```
3723

@@ -44,85 +30,59 @@ result.then(function (messageId) {
4430
## Set custom message response
4531
In some cases it could be necessary to fake a custom send response (like a [BatchResponse](https://firebase.google.com/docs/reference/admin/node/admin.messaging.BatchResponse.html)). In this cases you can use `firebase.messaging().respondNext(methodName, result)` (similar to `firebase.messaging().failNext(methodName, err)`).
4632

47-
##### Source
48-
4933
```js
50-
var ref;
51-
var messageService = {
52-
messaging: function () {
53-
if (!ref) ref = firebase.messaging();
54-
return ref;
55-
},
56-
send: function () {
57-
var message = {
58-
59-
};
60-
messageService.ref().send();
34+
var mockMessaging = new MockMessaging();
35+
var messages = [
36+
{
37+
notification: {
38+
title: 'message title',
39+
body: 'foobar'
40+
}
6141
},
62-
}
63-
```
64-
65-
##### Test
66-
67-
```js
68-
MockFirebase.override();
42+
{
43+
notification: {
44+
title: 'message title',
45+
body: 'second message'
46+
}
47+
}
48+
];
6949
var batchResponse = {
7050
failureCount: 1,
71-
response: [
72-
{
73-
error: {...},
74-
messageId: undefined,
75-
success: false
76-
},
77-
{
78-
error: undefined,
79-
messageId: '...',
80-
success: true
81-
}
51+
responses: [
52+
{
53+
error: "something went wrong",
54+
success: false,
55+
},
56+
{
57+
messageId: "12345",
58+
success: true,
59+
},
8260
],
83-
successCount: 1
84-
}
85-
messageService.messaging().respondNext('sendAll', batchResponse);
86-
var result = messageService.sendAll();
87-
messageService.messaging().flush();
88-
result.then(function (res) {
89-
console.assert(res.failureCount === 1, '1 message failed');
90-
console.assert(res.successCount === 1, '1 message succeeded');
61+
successCount: 1,
62+
};
63+
mockMessaging.respondNext('sendAll', batchResponse);
64+
var result = mockMessaging.sendAll(messages);
65+
mockMessaging.flush();
66+
result.then(function (response) {
67+
console.assert(response === batchResponse, 'custom batch response is returned');
9168
});
9269
```
9370

9471
## Assert arguments
9572
If you want to assert the arguments that were passed to any of the send-functions you can register a callback.
9673
The callback receives as argument that contains all arguments that were passed to the send-function.
9774

98-
##### Source
99-
100-
```js
101-
var ref;
102-
var messageService = {
103-
messaging: function () {
104-
if (!ref) ref = firebase.messaging();
105-
return ref;
106-
},
107-
send: function () {
108-
var message = {
109-
data: {
110-
foo: 'bar'
111-
},
112-
...
113-
};
114-
messageService.ref().send();
115-
},
116-
}
117-
```
118-
119-
##### Test
120-
12175
```js
122-
MockFirebase.override();
123-
messageService.ref().on('send', function(args) {
124-
console.assert(args[0].data.foo === 'bar', 'message is coorect');
76+
var mockMessaging = new MockMessaging();
77+
var message = {
78+
notification: {
79+
title: 'message title',
80+
body: 'foobar'
81+
}
82+
};
83+
mockMessaging.on('send', function(args) {
84+
console.assert(args[0] === message, 'message argument is coorect');
12585
});
126-
messageService.send();
127-
messageService.messaging().flush();
128-
```
86+
mockMessaging.send(message);
87+
mockMessaging.flush();
88+
```

0 commit comments

Comments
 (0)