Skip to content

Commit 4dd67a7

Browse files
committed
Merge branch 'master' into list-documents
# Conflicts: # test/unit/firestore-collection.js
2 parents ea2431c + eea4eca commit 4dd67a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5743
-747
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
node_modules
44
coverage
55
browser
6+
.vscode

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ node_js:
44
- "4.5"
55
- "5.12"
66
- "6.4"
7+
- "8"
78
before_script:
89
- gulp lint
910
script:

API.md

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ Only `MockFirebase` methods are included here. For details on normal Firebase AP
1212
- [`fakeEvent(event [, key] [, data] [, previousChild] [, priority])`](#fakeeventevent--key--data--previouschild--priority---ref)
1313
- [`getFlushQueue()`](#getflushqueue---array)
1414
- [Auth](#auth)
15-
- [`changeAuthState(user)`](#changeauthstateauthdata---undefined)
16-
- [`getUserByEmail(email)`](#getemailuseremail---objectnull)
17-
- [`getUser(uid)`](#getemailuseremail---objectnull)
15+
- [`changeAuthState(user)`](#changeauthstateuser---undefined)
16+
- [`getUserByEmail(email)`](#getuserbyemailemail---promiseobject)
17+
- [`getUser(uid)`](#getuseruid---promiseobject)
18+
- [`updateUser(user)`](#updateuseruser---promisemockuser)
1819
- [Server Timestamps](#server-timestamps)
1920
- [`setClock(fn)`](#firebasesetclockfn---undefined)
2021
- [`restoreClock()`](#firebasesetclockfn---undefined)
22+
- [Messaging](#messaging)
23+
- [`respondNext(methodName, result)`](#respondnextmethodname-result---undefined)
24+
- [`failNext(methodName, err)`](#failnextmethodname-err---undefined)
25+
- [`on(methodName, callback)`](#onmethodname-callback---undefined)
2126

2227
## Core
2328

@@ -117,7 +122,7 @@ function onValue (_snapshot_) {
117122
}
118123
ref.on('value', onValue);
119124
ref.set({
120-
foo: 'bar';
125+
foo: 'bar',
121126
});
122127
ref.flush();
123128
console.assert(ref.getData().foo === 'bar', 'data has foo');
@@ -169,26 +174,47 @@ ref.flush(); // added foo after null
169174

170175
Authentication methods for simulating changes to the auth state of a Firebase reference.
171176

172-
##### `changeAuthState(authData)` -> `undefined`
177+
##### `changeAuthState(user)` -> `undefined`
173178

174-
Changes the active authentication credentials to the `authData` object. Before changing the authentication state, `changeAuthState` checks whether the `authData` object is deeply equal to the current authentication data. `onAuth` listeners will only be triggered if the data is not deeply equal. To simulate no user being authenticated, pass `null` for `authData`. This operation is queued until the next `flush`.
179+
Changes the active authentication credentials to the `authData` object.
180+
Before changing the authentication state, `changeAuthState` checks the
181+
`user` object against the current authentication data.
182+
`onIdTokenChanged` listeners will be triggered if the data is not
183+
deeply equal. `onAuthStateChanged` listeners will be triggered if the
184+
data is deeply equal but with different ID token validity.
175185

176-
`authData` should adhere to the [documented schema](https://www.firebase.com/docs/web/api/firebase/onauth.html).
186+
`user` should be a `MockUser` object or an object with the same fields
187+
as `MockUser`. To simulate no user being authenticated, pass `null` for
188+
`user`. This operation is queued until the next `flush`.
177189

178190
Example:
179191

180192
```js
181-
ref.changeAuthState({
193+
ref.changeAuthState(new MockUser(ref, {
182194
uid: 'theUid',
183-
provider: 'github',
184-
token: 'theToken',
185-
expires: Math.floor(new Date() / 1000) + 24 * 60 * 60, // expire in 24 hours
186-
auth: {
187-
myAuthProperty: true
188-
}
189-
});
195+
196+
emailVerified: true,
197+
displayName: 'Mr. Meeseeks',
198+
phoneNumber: '+1-508-123-4567',
199+
photoURL: 'https://example.com/image.png',
200+
isAnonymous: false,
201+
providerId: 'github',
202+
providerData: [],
203+
refreshToken: '123e4567-e89b-12d3-a456-426655440000',
204+
metadata: {}, // firebase-mock offers limited support for this field
205+
customClaims: {
206+
isAdmin: true,
207+
// etc.
208+
},
209+
_idtoken: 'theToken',
210+
_tokenValidity: {
211+
authTime: '2019-11-22T08:46:15Z',
212+
issuedAtTime: '2019-11-22T08:46:15Z',
213+
expirationTime: '2019-11-22T09:46:15Z',
214+
},
215+
}));
190216
ref.flush();
191-
console.assert(ref.getAuth().auth.myAuthProperty, 'authData has custom property');
217+
console.assert(ref.getAuth().displayName === 'Mr. Meeseeks', 'Auth name is correct');
192218
```
193219

194220
<hr>
@@ -201,6 +227,17 @@ Finds a user previously created with [`createUser`](https://www.firebase.com/doc
201227

202228
Finds a user previously created with [`createUser`](https://www.firebase.com/docs/web/api/firebase/createuser.html). If no user was created with the specified `email`, the promise is rejected.
203229

230+
##### `updateUser(user)` -> `Promise<MockUser>`
231+
232+
Replace the existing user with a new one, by matching uid. Throws an
233+
error if no user exists whose uid matches the given user's uid.
234+
Appropriate `onAuthStateChanged` and `onIdTokenChanged` listeners will
235+
be triggered if the new user has the same `uid` as the current
236+
authenticated user.
237+
238+
Resolves with the updated user when complete. This operation is queued
239+
until the next flush.
240+
204241
## Server Timestamps
205242

206243
MockFirebase allow you to simulate the behavior of [server timestamps](https://www.firebase.com/docs/web/api/servervalue/timestamp.html) when using a real Firebase instance. Unless you use `Firebase.setClock`, `Firebase.ServerValue.TIMESTAMP` will be transformed to the current date (`Date.now()`) when your data change is flushed.
@@ -214,3 +251,31 @@ Instead of using `Date.now()`, MockFirebase will call the `fn` you provide to ge
214251
##### `Firebase.restoreClock()` -> `undefined`
215252

216253
After calling `Firebase.setClock`, calling `Firebase.restoreClock` will restore the default timestamp behavior.
254+
255+
## Messaging
256+
257+
API reference of `MockMessaging`.
258+
259+
##### `respondNext(methodName, result)` -> `undefined`
260+
261+
When `methodName` is next invoked, the `Promise` (that is returned from the `methodName`) will be resolved with the specified `result`. This is useful for testing specific results of firebase messaging (e. g. partial success of sending messaging). The result will be triggered with the next `flush`.
262+
263+
If no result is specified, `methodName` will resolve a default response.
264+
265+
`result` must not be undefined.
266+
267+
<hr>
268+
269+
##### `failNext(methodName, err)` -> `undefined`
270+
271+
When `methodName` is next invoked, the `Promise` will be rejected with the specified `err`. This is useful for simulating validation or any other errors. The error will be triggered with the next `flush`.
272+
273+
`err` must be a proper `Error` object and not a string or any other primitive.
274+
275+
<hr>
276+
277+
##### `on(methodName, callback)` -> `undefined`
278+
279+
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`.
280+
281+
See [docs.js](/test/unit/docs.js) for an example.

CHANGELOG.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
### Added
9+
- Changelog
10+
- Mock `auth.Auth.onIdTokenChanged()` method, matching the previous
11+
behavior of `onAuthStateChanged()` (see below)
12+
- Support for Firebase Messaging (Admin API)
13+
14+
### Changed
15+
- (Breaking) Consistent with Firebase SDK [version 4.0.0](https://firebase.google.com/support/release-notes/js#version_500_-_may_8_2018) and later,
16+
and later, `onAuthStateChanged` no longer issues an event when a new
17+
ID token is issued for the same user. The `onIdTokenChanged` method is
18+
now mocked, keeping the previous behavior.
19+
- `MockStorageFile.download()` now allows omitting the destination arg;
20+
in that case, it simply resolves the `Promise` with the file contents
21+
and does not write it anywhere else.
22+
23+
### Fixed
24+
- `onAuthStateChanged` now correctly calls its callback immediately with
25+
the current auth state.
26+
- `MockStorage.bucket()` and `MockStorageBucket.file()` now return the
27+
existing artifact if one exists, rather than overwriting it with a new
28+
one.
29+
- `DataSnapshot.child` now correctly splits child paths by '/'
30+
characters
31+
- Boolean values are now allowed in RTDB priority fields and as
32+
arguments to `Query.startAt`, `Query.endAt`, and `Query.equalTo`.
33+
- `MockFirestoreDocument.create()` now correctly returns a
34+
`Promise<WriteResult>` instead of `Promise<void>`.
35+
36+
37+
[Unreleased]: https://github.com/dmurvihill/firebase-mock/compare/v2.2.10...HEAD

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Firebase Mock supports the client-side [JavaScript API](https://firebase.google.
3535
* Authentication
3636
* [Basic](tutorials/admin/authentication.md)
3737
* [JWT Tokens](tutorials/admin/tokens.md)
38+
* [Messaging](tutorials/admin/messaging.md)
3839
* [Realtime Database](tutorials/admin/rtdb.md)
3940
* [Firestore](tutorials/admin/firestore.md)
4041
* [Storage](tutorials/admin/storage.md)

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "firebase-mock",
3-
"version": "2.2.10",
3+
"version": "2.3.0",
44
"homepage": "https://github.com/soumak77/firebase-mock",
55
"authors": [
66
"Kato"

0 commit comments

Comments
 (0)