firestore-jest-mock
Advanced tools
Comparing version 0.20.0 to 0.21.0
@@ -367,3 +367,3 @@ const { FakeFirestore } = require('firestore-jest-mock'); | ||
expect.assertions(2); | ||
// See https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#doc | ||
// See https://firebase.google.com/docs/reference/js/firestore_#doc | ||
// "If no path is specified, an automatically-generated unique ID will be used for the returned DocumentReference." | ||
@@ -370,0 +370,0 @@ const col = db().collection('foo'); |
@@ -90,2 +90,10 @@ const { | ||
}, | ||
{ | ||
id: 'cow', | ||
name: 'cow', | ||
appearance: { | ||
color: 'brown', | ||
size: 'large', | ||
}, | ||
}, | ||
], | ||
@@ -178,2 +186,14 @@ foodSchedule: [ | ||
test('it can query nested values', async () => { | ||
const brownColor = await db | ||
.collection('animals') | ||
.where('appearance.color', '==', 'brown') | ||
.get(); | ||
expect(brownColor).toHaveProperty('size', 1); | ||
const cow = brownColor.docs[0]; | ||
expect(cow).toBeDefined(); | ||
expect(cow).toHaveProperty('id', 'cow'); | ||
}); | ||
test('it can query date values for equality', async () => { | ||
@@ -180,0 +200,0 @@ const elephant = await db |
@@ -93,3 +93,3 @@ const mockCollectionGroup = jest.fn(); | ||
// Accept any collection path | ||
// See https://firebase.google.com/docs/reference/js/firebase.firestore.Firestore#collection | ||
// See https://firebase.google.com/docs/reference/js/firestore_#collection | ||
mockCollection(...arguments); | ||
@@ -134,3 +134,3 @@ | ||
// Accept any document path | ||
// See https://firebase.google.com/docs/reference/js/firebase.firestore.Firestore#doc | ||
// See https://firebase.google.com/docs/reference/js/firestore_#doc | ||
@@ -137,0 +137,0 @@ if (path === undefined) { |
@@ -18,3 +18,3 @@ const timestamp = require('../timestamp'); | ||
// From Firestore docs: "Returns 'undefined' if the document doesn't exist." | ||
// See https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#data | ||
// See https://firebase.google.com/docs/reference/js/firestore_.documentsnapshot#documentsnapshotdata | ||
return undefined; | ||
@@ -21,0 +21,0 @@ } |
@@ -98,7 +98,10 @@ const buildDocFromHash = require('./buildDocFromHash'); | ||
function _recordsWithKey(records, key) { | ||
return records.filter(record => record && record[key] !== undefined); | ||
return records.filter(record => record && getValueByPath(record, key) !== undefined); | ||
} | ||
function _recordsWithNonNullKey(records, key) { | ||
return records.filter(record => record && record[key] !== undefined && record[key] !== null); | ||
return records.filter( | ||
record => | ||
record && getValueByPath(record, key) !== undefined && getValueByPath(record, key) !== null, | ||
); | ||
} | ||
@@ -126,9 +129,10 @@ | ||
return _recordsWithNonNullKey(records, key).filter(record => { | ||
if (_shouldCompareNumerically(record[key], value)) { | ||
return record[key] < value; | ||
const recordValue = getValueByPath(record, key); | ||
if (_shouldCompareNumerically(recordValue, value)) { | ||
return recordValue < value; | ||
} | ||
if (_shouldCompareTimestamp(record[key], value)) { | ||
return record[key].toMillis() < value; | ||
if (_shouldCompareTimestamp(recordValue, value)) { | ||
return recordValue.toMillis() < value; | ||
} | ||
return String(record[key]) < String(value); | ||
return String(recordValue) < String(value); | ||
}); | ||
@@ -145,9 +149,10 @@ } | ||
return _recordsWithNonNullKey(records, key).filter(record => { | ||
if (_shouldCompareNumerically(record[key], value)) { | ||
return record[key] <= value; | ||
const recordValue = getValueByPath(record, key); | ||
if (_shouldCompareNumerically(recordValue, value)) { | ||
return recordValue <= value; | ||
} | ||
if (_shouldCompareTimestamp(record[key], value)) { | ||
return record[key].toMillis() <= value; | ||
if (_shouldCompareTimestamp(recordValue, value)) { | ||
return recordValue.toMillis() <= value; | ||
} | ||
return String(record[key]) <= String(value); | ||
return String(recordValue) <= String(value); | ||
}); | ||
@@ -164,7 +169,8 @@ } | ||
return _recordsWithKey(records, key).filter(record => { | ||
if (_shouldCompareTimestamp(record[key], value)) { | ||
const recordValue = getValueByPath(record, key); | ||
if (_shouldCompareTimestamp(recordValue, value)) { | ||
//NOTE: for equality, we must compare numbers! | ||
return record[key].toMillis() === value.getTime(); | ||
return recordValue.toMillis() === value.getTime(); | ||
} | ||
return String(record[key]) === String(value); | ||
return String(recordValue) === String(value); | ||
}); | ||
@@ -181,7 +187,8 @@ } | ||
return _recordsWithKey(records, key).filter(record => { | ||
if (_shouldCompareTimestamp(record[key], value)) { | ||
const recordValue = getValueByPath(record, key); | ||
if (_shouldCompareTimestamp(recordValue, value)) { | ||
//NOTE: for equality, we must compare numbers! | ||
return record[key].toMillis() !== value.getTime(); | ||
return recordValue.toMillis() !== value.getTime(); | ||
} | ||
return String(record[key]) !== String(value); | ||
return String(recordValue) !== String(value); | ||
}); | ||
@@ -198,9 +205,10 @@ } | ||
return _recordsWithNonNullKey(records, key).filter(record => { | ||
if (_shouldCompareNumerically(record[key], value)) { | ||
return record[key] >= value; | ||
const recordValue = getValueByPath(record, key); | ||
if (_shouldCompareNumerically(recordValue, value)) { | ||
return recordValue >= value; | ||
} | ||
if (_shouldCompareTimestamp(record[key], value)) { | ||
return record[key].toMillis() >= value; | ||
if (_shouldCompareTimestamp(recordValue, value)) { | ||
return recordValue.toMillis() >= value; | ||
} | ||
return String(record[key]) >= String(value); | ||
return String(recordValue) >= String(value); | ||
}); | ||
@@ -217,9 +225,10 @@ } | ||
return _recordsWithNonNullKey(records, key).filter(record => { | ||
if (_shouldCompareNumerically(record[key], value)) { | ||
return record[key] > value; | ||
const recordValue = getValueByPath(record, key); | ||
if (_shouldCompareNumerically(recordValue, value)) { | ||
return recordValue > value; | ||
} | ||
if (_shouldCompareTimestamp(record[key], value)) { | ||
return record[key].toMillis() > value; | ||
if (_shouldCompareTimestamp(recordValue, value)) { | ||
return recordValue.toMillis() > value; | ||
} | ||
return String(record[key]) > String(value); | ||
return String(recordValue) > String(value); | ||
}); | ||
@@ -238,3 +247,7 @@ } | ||
return records.filter( | ||
record => record && record[key] && Array.isArray(record[key]) && record[key].includes(value), | ||
record => | ||
record && | ||
getValueByPath(record, key) && | ||
Array.isArray(getValueByPath(record, key)) && | ||
getValueByPath(record, key).includes(value), | ||
); | ||
@@ -254,6 +267,6 @@ } | ||
return records.filter(record => { | ||
if (!record || record[key] === undefined) { | ||
if (!record || getValueByPath(record, key) === undefined) { | ||
return false; | ||
} | ||
return value && Array.isArray(value) && value.includes(record[key]); | ||
return value && Array.isArray(value) && value.includes(getValueByPath(record, key)); | ||
}); | ||
@@ -273,3 +286,3 @@ } | ||
return _recordsWithKey(records, key).filter( | ||
record => value && Array.isArray(value) && !value.includes(record[key]), | ||
record => value && Array.isArray(value) && !value.includes(getValueByPath(record, key)), | ||
); | ||
@@ -291,8 +304,13 @@ } | ||
record && | ||
record[key] && | ||
Array.isArray(record[key]) && | ||
getValueByPath(record, key) && | ||
Array.isArray(getValueByPath(record, key)) && | ||
value && | ||
Array.isArray(value) && | ||
record[key].some(v => value.includes(v)), | ||
getValueByPath(record, key).some(v => value.includes(v)), | ||
); | ||
} | ||
function getValueByPath(record, path) { | ||
const keys = path.split('.'); | ||
return keys.reduce((nestedObject = {}, key) => nestedObject[key], record); | ||
} |
{ | ||
"name": "firestore-jest-mock", | ||
"version": "0.20.0", | ||
"version": "0.21.0", | ||
"description": "Jest helper for mocking Google Cloud Firestore", | ||
@@ -5,0 +5,0 @@ "author": "", |
@@ -154,3 +154,3 @@ # Mock Firestore | ||
expect(mockCollection).toHaveBeenCalledWith('users'); | ||
expect(userDocs[0].name).toEqual('Homer Simpson'); | ||
expect(userDocs.docs[0].data().name).toEqual('Homer Simpson'); | ||
}); | ||
@@ -165,8 +165,8 @@ }); | ||
If you use `@react-native-firebase/firestore`, use `mockGoogleCloudFirestore` instead of `mockFirebase` in all the documentation. | ||
If you use `@react-native-firebase/firestore`, use `mockReactNativeFirestore` instead of `mockFirebase` in all the documentation. | ||
```js | ||
const { mockGoogleCloudFirestore } = require('firestore-jest-mock'); | ||
const { mockReactNativeFirestore } = require('firestore-jest-mock'); | ||
mockGoogleCloudFirestore({ | ||
mockReactNativeFirestore({ | ||
database: { | ||
@@ -192,3 +192,3 @@ users: [ | ||
expect(mockCollection).toHaveBeenCalledWith('users'); | ||
expect(userDocs[0].name).toEqual('Homer Simpson'); | ||
expect(userDocs.docs[0].data().name).toEqual('Homer Simpson'); | ||
}); | ||
@@ -412,3 +412,3 @@ }); | ||
| `mockDelete` | Assert delete is called on ref. Returns a promise | [delete](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#delete) | | ||
| `mockUseEmulator` | Assert correct host and port are passed | [useEmulator](https://firebase.google.com/docs/reference/js/firebase.firestore.Firestore#useemulator) | | ||
| `mockUseEmulator` | Assert correct host and port are passed | [useEmulator](https://firebase.google.com/docs/reference/js/v8/firebase.firestore.Firestore#useemulator) | | ||
@@ -426,3 +426,3 @@ #### [Firestore.Query](https://googleapis.dev/nodejs/firestore/latest/Query.html) | ||
| `mockStartAt` | Assert startAt is called | [startAt](https://googleapis.dev/nodejs/firestore/latest/Query.html#startAt) | | ||
| `mockWithConverter` | Assert withConverter is called | [withConverter](https://firebase.google.com/docs/reference/js/firebase.firestore.Query#withconverter) | | ||
| `mockWithConverter` | Assert withConverter is called | [withConverter](https://firebase.google.com/docs/reference/js/v8/firebase.firestore.Query#withconverter) | | ||
@@ -459,17 +459,17 @@ #### [Firestore.FieldValue](https://googleapis.dev/nodejs/firestore/latest/FieldValue.html) | ||
#### [Auth](https://firebase.google.com/docs/reference/js/firebase.auth.Auth) | ||
#### [Auth](https://firebase.google.com/docs/reference/js/auth.auth) | ||
| Method | Use | Method in Firebase | | ||
| ------------------------------------ | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `mockCreateUserWithEmailAndPassword` | Assert correct email and password are passed. Returns a promise | [createUserWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#createuserwithemailandpassword) | | ||
| `mockGetUser` | Assert correct user IDs are passed. Returns a promise | [getUser](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#getuser) | | ||
| `mockDeleteUser` | Assert correct ID is passed to delete method. Returns a promise | [deleteUser](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#deleteuser) | | ||
| `mockSendVerificationEmail` | Assert request for verification email was sent. Lives on the `currentUser` | [sendVerificationEmail](https://firebase.google.com/docs/reference/js/firebase.User#send-email-verification) | | ||
| `mockCreateCustomToken` | Assert correct user ID and claims are passed. Returns a promise | [createCustomToken](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#createcustomtoken) | | ||
| `mockSetCustomUserClaims` | Assert correct user ID and claims are set. | [setCustomUserClaims](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#setcustomuserclaims) | | ||
| `mockSignInWithEmailAndPassword` | Assert correct email and password were passed. Returns a promise | [signInWithEmailAndPassword](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#signinwithemailandpassword) | | ||
| `mockSendPasswordResetEmail` | Assert correct email was passed. | [sendPasswordResetEmail](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#send-password-reset-email) | | ||
| `mockVerifyIdToken` | Assert correct token is passed. Returns a promise | [verifyIdToken](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#verifyidtoken) | | ||
| `mockUseEmulator` | Assert correct emulator url is passed | [useEmulator](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#useemulator) | | ||
| `mockSignOut` | Assert sign out is called. Returns a promise | [signOut](https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#signout) | | ||
| `mockCreateUserWithEmailAndPassword` | Assert correct email and password are passed. Returns a promise | [createUserWithEmailAndPassword](https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#createuserwithemailandpassword) | | ||
| `mockGetUser` | Assert correct user IDs are passed. Returns a promise | [getUser](https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data) | | ||
| `mockDeleteUser` | Assert correct ID is passed to delete method. Returns a promise | [deleteUser](https://firebase.google.com/docs/auth/admin/manage-users#delete_a_user) | | ||
| `mockSendVerificationEmail` | Assert request for verification email was sent. Lives on the `currentUser` | [sendVerificationEmail](https://firebase.google.com/docs/reference/js/auth.md#sendemailverification) | | ||
| `mockCreateCustomToken` | Assert correct user ID and claims are passed. Returns a promise | [createCustomToken](https://firebase.google.com/docs/auth/admin/create-custom-tokens) | | ||
| `mockSetCustomUserClaims` | Assert correct user ID and claims are set. | [setCustomUserClaims](https://firebase.google.com/docs/auth/admin/custom-claims) | | ||
| `mockSignInWithEmailAndPassword` | Assert correct email and password were passed. Returns a promise | [signInWithEmailAndPassword](https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#signinwithemailandpassword) | | ||
| `mockSendPasswordResetEmail` | Assert correct email was passed. | [sendPasswordResetEmail](https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#sendpasswordresetemail) | | ||
| `mockVerifyIdToken` | Assert correct token is passed. Returns a promise | [verifyIdToken](https://firebase.google.com/docs/auth/admin/verify-id-tokens) | | ||
| `mockUseEmulator` | Assert correct emulator url is passed | [useEmulator](https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#useemulator) | | ||
| `mockSignOut` | Assert sign out is called. Returns a promise | [signOut](https://firebase.google.com/docs/reference/js/auth.auth.md#authsignout) | | ||
@@ -476,0 +476,0 @@ ## Contributing |
212470
4084