Skip to content

Commit

Permalink
fix: remove usage of elvis operator to prevent webpack error #788 (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrelfrancis authored Jul 3, 2023
1 parent 9007f9a commit b790023
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
18 changes: 11 additions & 7 deletions src/attachCustomCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default function attachCustomCommands(
* @name cy.login
*/
Cypress.Commands.add(
options?.commandNames?.login || 'login',
(options && options.commandNames && options.commandNames.login) || 'login',
(
uid?: string,
customClaims?: any,
Expand Down Expand Up @@ -379,7 +379,8 @@ export default function attachCustomCommands(
* cy.logout()
*/
Cypress.Commands.add(
options?.commandNames?.logout || 'logout',
(options && options.commandNames && options.commandNames.logout) ||
'logout',
(
tenantId: string | undefined = Cypress.env('TEST_TENANT_ID'),
): Promise<any> =>
Expand Down Expand Up @@ -408,7 +409,8 @@ export default function attachCustomCommands(
* @name cy.callRtdb
*/
Cypress.Commands.add(
options?.commandNames?.callRtdb || 'callRtdb',
(options && options.commandNames && options.commandNames.callRtdb) ||
'callRtdb',
(
action: RTDBAction,
actionPath: string,
Expand All @@ -426,7 +428,7 @@ export default function attachCustomCommands(
const dataToWrite = dataIsObject ? { ...dataOrOptions } : dataOrOptions;

// Add metadata to dataToWrite if specified by options
if (dataIsObject && options?.withMeta) {
if (dataIsObject && options && options.withMeta) {
if (!dataToWrite.createdBy && Cypress.env('TEST_UID')) {
dataToWrite.createdBy = Cypress.env('TEST_UID');
}
Expand Down Expand Up @@ -456,7 +458,8 @@ export default function attachCustomCommands(
* @name cy.callFirestore
*/
Cypress.Commands.add(
options?.commandNames?.callFirestore || 'callFirestore',
(options && options.commandNames && options.commandNames.callFirestore) ||
'callFirestore',
(
action: FirestoreAction,
actionPath: string,
Expand All @@ -474,7 +477,7 @@ export default function attachCustomCommands(
const dataToWrite = dataIsObject ? { ...dataOrOptions } : dataOrOptions;

// Add metadata to dataToWrite if specified by options
if (dataIsObject && options?.withMeta) {
if (dataIsObject && options && options.withMeta) {
if (!dataToWrite.createdBy) {
dataToWrite.createdBy = Cypress.env('TEST_UID');
}
Expand Down Expand Up @@ -503,7 +506,8 @@ export default function attachCustomCommands(
* cy.getAuthUser()
*/
Cypress.Commands.add(
options?.commandNames?.getAuthUser || 'getAuthUser',
(options && options.commandNames && options.commandNames.getAuthUser) ||
'getAuthUser',
(uid: string): Promise<any> => cy.task('getAuthUser', uid),
);
}
2 changes: 1 addition & 1 deletion src/extendWithFirebaseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function extendWithFirebaseConfig(
...cypressConfig,
env: {
...valuesFromEnv,
...cypressConfig?.env,
...((cypressConfig && cypressConfig.env) || {}),
},
};
}
18 changes: 10 additions & 8 deletions src/firebase-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ export function initializeFirebase(
// Add projectId to fb config if it doesn't already exist
if (!fbConfig.projectId) {
const projectId =
process.env.GCLOUD_PROJECT || (fbConfig.credential as any)?.projectId; // eslint-disable-line camelcase
process.env.GCLOUD_PROJECT ||
(((fbConfig as any) && (fbConfig.credential as any)) || {}).projectId; // eslint-disable-line camelcase
if (projectId) {
fbConfig.projectId = projectId;
}
Expand Down Expand Up @@ -257,7 +258,7 @@ export function slashPathToFirestoreRef(
firestoreInstance.collection(slashPath);

// Apply orderBy to query if it exists
if (options?.orderBy && typeof ref.orderBy === 'function') {
if (options && options.orderBy && typeof ref.orderBy === 'function') {
if (Array.isArray(options.orderBy)) {
ref = ref.orderBy(...options.orderBy);
} else {
Expand All @@ -266,7 +267,8 @@ export function slashPathToFirestoreRef(
}
// Apply where to query if it exists
if (
options?.where &&
options &&
options.where &&
Array.isArray(options.where) &&
typeof ref.where === 'function'
) {
Expand All @@ -287,12 +289,12 @@ export function slashPathToFirestoreRef(
}

// Apply limit to query if it exists
if (options?.limit && typeof ref.limit === 'function') {
if (options && options.limit && typeof ref.limit === 'function') {
ref = ref.limit(options.limit);
}

// Apply limitToLast to query if it exists
if (options?.limitToLast && typeof ref.limitToLast === 'function') {
if (options && options.limitToLast && typeof ref.limitToLast === 'function') {
ref = ref.limitToLast(options.limitToLast);
}

Expand Down Expand Up @@ -356,13 +358,13 @@ export function deleteCollection(
let baseQuery = refOrQuery.orderBy('__name__');

// If no ordering is applied, order by id (__name__) to have groups in order
if (!options?.orderBy) {
if (!(options && options.orderBy)) {
baseQuery = refOrQuery.orderBy('__name__');
}

// Limit to batches to set batchSize or 500
if (!options?.limit) {
baseQuery = refOrQuery.limit(options?.batchSize || 500);
if (!(options && options.limit)) {
baseQuery = refOrQuery.limit((options && options.batchSize) || 500);
}

return new Promise((resolve, reject) => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function pluginWithTasks(
overrideConfig?: AppOptions,
): ExtendedCypressConfig {
// Only initialize admin instance if it hasn't already been initialized
if (adminInstance.apps?.length === 0) {
if (adminInstance.apps && adminInstance.apps.length === 0) {
initializeFirebase(adminInstance, overrideConfig);
}
// Parse single argument from task into arguments for task methods while
Expand All @@ -39,7 +39,7 @@ export default function pluginWithTasks(
const tasksWithFirebase: tasksType = Object.keys(tasks).reduce(
(acc, taskName: string) => {
(acc as any)[taskName] = (taskSettings: any): any => {
if (taskSettings?.uid) {
if (taskSettings && taskSettings.uid) {
return (tasks as any)[taskName](
adminInstance,
taskSettings.uid,
Expand Down
38 changes: 24 additions & 14 deletions src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,27 @@ export function convertValueToTimestampOrGeoPointIfPossible(
): firestore.FieldValue {
/* eslint-disable no-underscore-dangle */
if (
dataVal?._methodName === 'serverTimestamp' ||
dataVal?._methodName === 'FieldValue.serverTimestamp' // v8 and earlier
(dataVal && dataVal._methodName) === 'serverTimestamp' ||
(dataVal && dataVal._methodName) === 'FieldValue.serverTimestamp' // v8 and earlier
) {
return firestoreStatics.FieldValue.serverTimestamp();
}
if (
dataVal?._methodName === 'deleteField' ||
dataVal?._methodName === 'FieldValue.delete' // v8 and earlier
(dataVal && dataVal._methodName) === 'deleteField' ||
(dataVal && dataVal._methodName) === 'FieldValue.delete' // v8 and earlier
) {
return firestoreStatics.FieldValue.delete();
}
/* eslint-enable no-underscore-dangle */
if (
typeof dataVal?.seconds === 'number' &&
typeof dataVal?.nanoseconds === 'number'
typeof (dataVal && dataVal.seconds) === 'number' &&
typeof (dataVal && dataVal.nanoseconds) === 'number'
) {
return new firestoreStatics.Timestamp(dataVal.seconds, dataVal.nanoseconds);
}
if (
typeof dataVal?.latitude === 'number' &&
typeof dataVal?.longitude === 'number'
typeof (dataVal && dataVal.latitude) === 'number' &&
typeof (dataVal && dataVal.longitude) === 'number'
) {
return new firestoreStatics.GeoPoint(dataVal.latitude, dataVal.longitude);
}
Expand Down Expand Up @@ -239,15 +239,20 @@ export async function callFirestore(
) as any
).get();

if (snap?.docs?.length && typeof snap.docs.map === 'function') {
if (
snap &&
snap.docs &&
snap.docs.length &&
typeof snap.docs.map === 'function'
) {
return snap.docs.map((docSnap: FirebaseFirestore.DocumentSnapshot) => ({
...docSnap.data(),
id: docSnap.id,
}));
}
// Falling back to null in the case of falsey value prevents Cypress error with message:
// "You must return a promise, a value, or null to indicate that the task was handled."
return (typeof snap?.data === 'function' && snap.data()) || null;
return (typeof (snap && snap.data) === 'function' && snap.data()) || null;
}

if (action === 'delete') {
Expand Down Expand Up @@ -285,7 +290,8 @@ export async function callFirestore(
data,
// Use static option if passed (tests), otherwise fallback to statics on adminInstance
// Tests do not have statics since they are using @firebase/testing
options?.statics || (adminInstance.firestore as typeof firestore),
(options && options.statics) ||
(adminInstance.firestore as typeof firestore),
);

if (action === 'set') {
Expand All @@ -294,8 +300,10 @@ export async function callFirestore(
.doc(actionPath)
.set(
dataToSet,
options?.merge
? ({ merge: options?.merge } as FirebaseFirestore.SetOptions)
options && options.merge
? ({
merge: options && options.merge,
} as FirebaseFirestore.SetOptions)
: (undefined as any),
);
}
Expand Down Expand Up @@ -331,7 +339,9 @@ export function createCustomToken(
settings?: any,
): Promise<string> {
// Use custom claims or default to { isTesting: true }
const customClaims = settings?.customClaims || { isTesting: true };
const customClaims = (settings && settings.customClaims) || {
isTesting: true,
};

// Create auth token
return getAuth(adminInstance, settings.tenantId).createCustomToken(
Expand Down

0 comments on commit b790023

Please sign in to comment.