2
0
mirror of synced 2025-01-11 14:44:12 +00:00

Tests for firestore and auth

This commit is contained in:
Chris Bianca 2018-03-20 16:07:37 +00:00
parent 65cd6c8ae1
commit e791648c24
14 changed files with 86 additions and 145 deletions

View File

@ -24,7 +24,7 @@ export type {
} from './modules/auth/types';
export type {
default as ConfirmationResult,
} from './modules/auth/ConfirmationResult';
} from './modules/auth/phone/ConfirmationResult';
export type { default as User } from './modules/auth/User';
/*

View File

@ -8,7 +8,8 @@ import { getAppEventName, SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
import INTERNALS from '../../utils/internals';
import ConfirmationResult from './ConfirmationResult';
import ConfirmationResult from './phone/ConfirmationResult';
import PhoneAuthListener from './phone/PhoneAuthListener';
// providers
import EmailAuthProvider from './providers/EmailAuthProvider';
@ -19,8 +20,6 @@ import OAuthProvider from './providers/OAuthProvider';
import TwitterAuthProvider from './providers/TwitterAuthProvider';
import FacebookAuthProvider from './providers/FacebookAuthProvider';
import PhoneAuthListener from './PhoneAuthListener';
import type {
ActionCodeInfo,
ActionCodeSettings,

View File

@ -2,9 +2,9 @@
* @flow
* ConfirmationResult representation wrapper
*/
import { getNativeModule } from '../../utils/native';
import type Auth from './';
import type User from './User';
import { getNativeModule } from '../../../utils/native';
import type Auth from '../';
import type User from '../User';
export default class ConfirmationResult {
_auth: Auth;

View File

@ -1,6 +1,6 @@
// @flow
import INTERNALS from '../../utils/internals';
import { SharedEventEmitter } from '../../utils/events';
import INTERNALS from '../../../utils/internals';
import { SharedEventEmitter } from '../../../utils/events';
import {
generatePushID,
isFunction,
@ -8,10 +8,10 @@ import {
isIOS,
isString,
nativeToJSError,
} from '../../utils';
import { getNativeModule } from '../../utils/native';
} from '../../../utils';
import { getNativeModule } from '../../../utils/native';
import type Auth from './';
import type Auth from '../';
type PhoneAuthSnapshot = {
state: 'sent' | 'timeout' | 'verified' | 'error',

View File

@ -4,12 +4,11 @@
*/
import CollectionReference from './CollectionReference';
import DocumentSnapshot from './DocumentSnapshot';
import FieldPath from './FieldPath';
import { mergeFieldPathData } from './utils';
import { parseUpdateArgs } from './utils';
import { buildNativeMap } from './utils/serialize';
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { firestoreAutoId, isFunction, isObject, isString } from '../../utils';
import { firestoreAutoId, isFunction, isObject } from '../../utils';
import { getNativeModule } from '../../utils/native';
import type Firestore from './';
@ -50,9 +49,7 @@ export default class DocumentReference {
get parent(): CollectionReference {
const parentPath = this._documentPath.parent();
if (!parentPath) {
throw new Error('Invalid document path');
}
// $FlowExpectedError: parentPath can never be null
return new CollectionReference(this._firestore, parentPath);
}
@ -232,34 +229,7 @@ export default class DocumentReference {
}
update(...args: any[]): Promise<void> {
let data = {};
if (args.length === 1) {
if (!isObject(args[0])) {
throw new Error(
'DocumentReference.update failed: If using a single argument, it must be an object.'
);
}
// eslint-disable-next-line prefer-destructuring
data = args[0];
} else if (args.length % 2 === 1) {
throw new Error(
'DocumentReference.update failed: Must have either a single object argument, or equal numbers of key/value pairs.'
);
} else {
for (let i = 0; i < args.length; i += 2) {
const key = args[i];
const value = args[i + 1];
if (isString(key)) {
data[key] = value;
} else if (key instanceof FieldPath) {
data = mergeFieldPathData(data, key._segments, value);
} else {
throw new Error(
`DocumentReference.update failed: Argument at index ${i} must be a string or FieldPath`
);
}
}
}
const data = parseUpdateArgs(args, 'DocumentReference.update');
const nativeData = buildNativeMap(data);
return getNativeModule(this._firestore).documentUpdate(
this.path,

View File

@ -14,10 +14,7 @@ export default class Path {
}
get id(): string | null {
if (this._parts.length > 0) {
return this._parts[this._parts.length - 1];
}
return null;
return this._parts.length > 0 ? this._parts[this._parts.length - 1] : null;
}
get isDocument(): boolean {
@ -37,11 +34,9 @@ export default class Path {
}
parent(): Path | null {
if (this._parts.length === 0) {
return null;
}
return new Path(this._parts.slice(0, this._parts.length - 1));
return this._parts.length > 0
? new Path(this._parts.slice(0, this._parts.length - 1))
: null;
}
/**
@ -50,10 +45,6 @@ export default class Path {
*/
static fromName(name: string): Path {
const parts = name.split('/');
if (parts.length === 0) {
return new Path([]);
}
return new Path(parts);
return parts.length === 0 ? new Path([]) : new Path(parts);
}
}

View File

@ -2,15 +2,13 @@
* @flow
* Firestore Transaction representation wrapper
*/
import { mergeFieldPathData } from './utils';
import { parseUpdateArgs } from './utils';
import { buildNativeMap } from './utils/serialize';
import type Firestore from './';
import type { TransactionMeta } from './TransactionHandler';
import type DocumentReference from './DocumentReference';
import DocumentSnapshot from './DocumentSnapshot';
import { isObject, isString } from '../../utils';
import FieldPath from './FieldPath';
import { getNativeModule } from '../../utils/native';
type Command = {
@ -124,35 +122,7 @@ export default class Transaction {
*/
update(documentRef: DocumentReference, ...args: Array<any>): Transaction {
// todo validate doc ref
let data = {};
if (args.length === 1) {
if (!isObject(args[0])) {
throw new Error(
'Transaction.update failed: If using a single data argument, it must be an object.'
);
}
[data] = args;
} else if (args.length % 2 === 1) {
throw new Error(
'Transaction.update failed: Must have either a single object data argument, or equal numbers of data key/value pairs.'
);
} else {
for (let i = 0; i < args.length; i += 2) {
const key = args[i];
const value = args[i + 1];
if (isString(key)) {
data[key] = value;
} else if (key instanceof FieldPath) {
data = mergeFieldPathData(data, key._segments, value);
} else {
throw new Error(
`Transaction.update failed: Argument at index ${i} must be a string or FieldPath`
);
}
}
}
const data = parseUpdateArgs(args, 'Transaction.update');
this._commandBuffer.push({
type: 'update',
path: documentRef.path,

View File

@ -3,7 +3,6 @@
* Firestore Transaction representation wrapper
*/
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
import Transaction from './Transaction';
import type Firestore from './';
@ -103,9 +102,7 @@ export default class TransactionHandler {
* @private
*/
_remove(id) {
// todo confirm pending arg no longer needed
getNativeModule(this._firestore).transactionDispose(id);
// TODO may need delaying to next event loop
delete this._pending[id];
}
@ -124,19 +121,17 @@ export default class TransactionHandler {
* @private
*/
_handleTransactionEvent(event: TransactionEvent) {
// eslint-disable-next-line default-case
switch (event.type) {
case 'update':
return this._handleUpdate(event);
this._handleUpdate(event);
break;
case 'error':
return this._handleError(event);
this._handleError(event);
break;
case 'complete':
return this._handleComplete(event);
default:
getLogger(this._firestore).warn(
`Unknown transaction event type: '${event.type}'`,
event
);
return undefined;
this._handleComplete(event);
break;
}
}

View File

@ -2,10 +2,8 @@
* @flow
* WriteBatch representation wrapper
*/
import FieldPath from './FieldPath';
import { mergeFieldPathData } from './utils';
import { parseUpdateArgs } from './utils';
import { buildNativeMap } from './utils/serialize';
import { isObject, isString } from '../../utils';
import { getNativeModule } from '../../utils/native';
import type DocumentReference from './DocumentReference';
@ -66,38 +64,9 @@ export default class WriteBatch {
update(docRef: DocumentReference, ...args: any[]): WriteBatch {
// TODO: Validation
// validate.isDocumentReference('docRef', docRef);
let data = {};
if (args.length === 1) {
if (!isObject(args[0])) {
throw new Error(
'WriteBatch.update failed: If using two arguments, the second must be an object.'
);
}
// eslint-disable-next-line prefer-destructuring
data = args[0];
} else if (args.length % 2 === 1) {
throw new Error(
'WriteBatch.update failed: Must have a document reference, followed by either a single object argument, or equal numbers of key/value pairs.'
);
} else {
for (let i = 0; i < args.length; i += 2) {
const key = args[i];
const value = args[i + 1];
if (isString(key)) {
data[key] = value;
} else if (key instanceof FieldPath) {
data = mergeFieldPathData(data, key._segments, value);
} else {
throw new Error(
`WriteBatch.update failed: Argument at index ${i} must be a string or FieldPath`
);
}
}
}
const nativeData = buildNativeMap(data);
const data = parseUpdateArgs(args, 'WriteBatch.update');
this._writes.push({
data: nativeData,
data: buildNativeMap(data),
path: docRef.path,
type: 'UPDATE',
});

View File

@ -1,6 +1,8 @@
/**
* @flow
*/
import FieldPath from '../FieldPath';
import { isObject, isString } from '../../../utils';
const buildFieldPathData = (segments: string[], value: any): Object => {
if (segments.length === 1) {
@ -39,3 +41,34 @@ export const mergeFieldPathData = (
[segments[0]]: buildFieldPathData(segments.slice(1), value),
};
};
export const parseUpdateArgs = (args: any[], methodName: string) => {
let data = {};
if (args.length === 1) {
if (!isObject(args[0])) {
throw new Error(
`${methodName} failed: If using a single update argument, it must be an object.`
);
}
[data] = args;
} else if (args.length % 2 === 1) {
throw new Error(
`${methodName} failed: The update arguments must be either a single object argument, or equal numbers of key/value pairs.`
);
} else {
for (let i = 0; i < args.length; i += 2) {
const key = args[i];
const value = args[i + 1];
if (isString(key)) {
data[key] = value;
} else if (key instanceof FieldPath) {
data = mergeFieldPathData(data, key._segments, value);
} else {
throw new Error(
`${methodName} failed: Argument at index ${i} must be a string or FieldPath`
);
}
}
}
return data;
};

View File

@ -164,7 +164,7 @@ PODS:
- React/Core
- React/fishhook
- React/RCTBlob
- RNFirebase (3.3.1):
- RNFirebase (4.0.0-alpha.1):
- React
- yoga (0.52.3.React)

View File

@ -4,8 +4,11 @@ import firebase from 'firebase';
import RNfirebase from './../firebase';
import DatabaseContents from './tests/support/DatabaseContents';
// RNfirebase.database.enableLogging(true);
// RNfirebase.firestore.enableLogging(true);
// Verify logging works
RNfirebase.database.enableLogging(true);
RNfirebase.database.enableLogging(false);
RNfirebase.firestore.enableLogging(true);
RNfirebase.firestore.enableLogging(false);
// RNfirebase.utils().logLevel = 'debug';
// RNfirebase.utils().logLevel = 'info';

View File

@ -37,6 +37,17 @@ function documentReferenceTests({ describe, it, context, firebase }) {
const collection = document.collection('pages');
collection.id.should.equal('pages');
});
it('should error if invalid collection path supplied', () => {
(() => {
firebase.native
.firestore()
.doc('documents/doc1')
.collection('pages/page1');
}).should.throw(
'Argument "collectionPath" must point to a collection.'
);
});
});
context('delete()', () => {
@ -586,12 +597,12 @@ function documentReferenceTests({ describe, it, context, firebase }) {
(() => {
docRef.update('error');
}).should.throw(
'DocumentReference.update failed: If using a single argument, it must be an object.'
'DocumentReference.update failed: If using a single update argument, it must be an object.'
);
(() => {
docRef.update('error1', 'error2', 'error3');
}).should.throw(
'DocumentReference.update failed: Must have either a single object argument, or equal numbers of key/value pairs.'
'DocumentReference.update failed: The update arguments must be either a single object argument, or equal numbers of key/value pairs.'
);
(() => {
docRef.update(0, 'error');

View File

@ -111,12 +111,12 @@ function firestoreTests({ describe, it, context, fcontext, firebase }) {
(() => {
batch.update(ref, 'error');
}).should.throw(
'WriteBatch.update failed: If using two arguments, the second must be an object.'
'WriteBatch.update failed: If using a single update argument, it must be an object.'
);
(() => {
batch.update(ref, 'error1', 'error2', 'error3');
}).should.throw(
'WriteBatch.update failed: Must have a document reference, followed by either a single object argument, or equal numbers of key/value pairs.'
'WriteBatch.update failed: The update arguments must be either a single object argument, or equal numbers of key/value pairs.'
);
(() => {
batch.update(ref, 0, 'error');