Skip to content

Commit

Permalink
Make docs match tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurvihill committed Jan 3, 2020
1 parent f687e89 commit 891551e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 107 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,4 @@ When `methodName` is next invoked, the `Promise` will be rejected with the speci

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`.

See [docs.js](/test/unit/docs.js) for an example.
See [docs.js](/test/unit/docs.js) for an example.
31 changes: 18 additions & 13 deletions test/unit/docs.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const MockUser = require('../../src/user');
'use strict';

/* jshint browser:true */
/* globals expect:false */

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

let ref;
beforeEach(() => {
const Authentication = require('../../').MockAuthentication;
const Authentication = require('../../src').MockAuthentication;
ref = new Authentication();
});

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

describe('Messaging examples', () => {
let ref;
let mockMessaging;

beforeEach(() => {
const Messaging = require('../../').MockMessaging;
ref = new Messaging();
mockMessaging = new Messaging();
});

it('send messages', () => {
Expand All @@ -52,8 +57,8 @@ describe('API.md', () => {
body: 'foobar'
}
};
var result = ref.send(message);
ref.flush();
var result = mockMessaging.send(message);
mockMessaging.flush();
result.then(function (messageId) {
console.assert(messageId !== '', 'message id is ' + messageId);
});
Expand Down Expand Up @@ -88,9 +93,9 @@ describe('API.md', () => {
],
successCount: 1,
};
ref.respondNext('sendAll', batchResponse);
var result = ref.sendAll(messages);
ref.flush();
mockMessaging.respondNext('sendAll', batchResponse);
var result = mockMessaging.sendAll(messages);
mockMessaging.flush();
result.then(function (response) {
console.assert(response === batchResponse, 'custom batch response is returned');
});
Expand All @@ -103,11 +108,11 @@ describe('API.md', () => {
body: 'foobar'
}
};
ref.on('send', function(args) {
console.assert(args[0] === message, 'message argument is coorect');
mockMessaging.on('send', function(args) {
console.assert(args[0] === message, 'message argument is correct');
});
ref.send(message);
ref.flush();
mockMessaging.send(message);
mockMessaging.flush();
});
});
});
146 changes: 53 additions & 93 deletions tutorials/admin/messaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,20 @@ MockFirebase replaces most of Firebase's messaging method of the admin API with

## Send message

In this example, we'll create send a new message using Firebase messaging.

##### Source

```js
var ref;
var messageService = {
messaging: function () {
if (!ref) ref = firebase.messaging();
return ref;
},
send: function () {
var message = {

};
messageService.ref().send();
},
}
```

##### Test
In this example, we'll send a new message using Firebase messaging.

```js
MockFirebase.override();
var result = messageService.send();
messageService.messaging().flush();
var mockMessaging = new MockMessaging();
var message = {
notification: {
title: 'message title',
body: 'foobar'
}
};
var result = mockMessaging.send(message);
mockMessaging.flush();
result.then(function (messageId) {
console.log(messageId);
console.assert(messageId !== '', 'message id is ' + messageId);
});
```

Expand All @@ -44,85 +30,59 @@ result.then(function (messageId) {
## Set custom message response
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)`).

##### Source

```js
var ref;
var messageService = {
messaging: function () {
if (!ref) ref = firebase.messaging();
return ref;
},
send: function () {
var message = {

};
messageService.ref().send();
var mockMessaging = new MockMessaging();
var messages = [
{
notification: {
title: 'message title',
body: 'foobar'
}
},
}
```

##### Test

```js
MockFirebase.override();
{
notification: {
title: 'message title',
body: 'second message'
}
}
];
var batchResponse = {
failureCount: 1,
response: [
{
error: {...},
messageId: undefined,
success: false
},
{
error: undefined,
messageId: '...',
success: true
}
responses: [
{
error: "something went wrong",
success: false,
},
{
messageId: "12345",
success: true,
},
],
successCount: 1
}
messageService.messaging().respondNext('sendAll', batchResponse);
var result = messageService.sendAll();
messageService.messaging().flush();
result.then(function (res) {
console.assert(res.failureCount === 1, '1 message failed');
console.assert(res.successCount === 1, '1 message succeeded');
successCount: 1,
};
mockMessaging.respondNext('sendAll', batchResponse);
var result = mockMessaging.sendAll(messages);
mockMessaging.flush();
result.then(function (response) {
console.assert(response === batchResponse, 'custom batch response is returned');
});
```

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

##### Source

```js
var ref;
var messageService = {
messaging: function () {
if (!ref) ref = firebase.messaging();
return ref;
},
send: function () {
var message = {
data: {
foo: 'bar'
},
...
};
messageService.ref().send();
},
}
```

##### Test

```js
MockFirebase.override();
messageService.ref().on('send', function(args) {
console.assert(args[0].data.foo === 'bar', 'message is coorect');
var mockMessaging = new MockMessaging();
var message = {
notification: {
title: 'message title',
body: 'foobar'
}
};
mockMessaging.on('send', function(args) {
console.assert(args[0] === message, 'message argument is coorect');
});
messageService.send();
messageService.messaging().flush();
```
mockMessaging.send(message);
mockMessaging.flush();
```

0 comments on commit 891551e

Please sign in to comment.