Skip to content

Firestore: Fix 'where indexeddb is not available' tests that were broken by persistence changes #7414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/firestore/test/integration/browser/indexeddb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import {
} from '../util/firebase_export';
import {
IndexedDbPersistenceMode,
MemoryEagerPersistenceMode,
isPersistenceAvailable,
withTestDb
withTestDb,
PERSISTENCE_MODE_UNSPECIFIED
} from '../util/helpers';

describe('where indexeddb is not available: ', () => {
Expand All @@ -41,7 +41,7 @@ describe('where indexeddb is not available: ', () => {
it('fails with code unimplemented', () => {
// withTestDb will fail the test if persistence is requested but it fails
// so we'll enable persistence here instead.
return withTestDb(new MemoryEagerPersistenceMode(), db => {
return withTestDb(PERSISTENCE_MODE_UNSPECIFIED, db => {
return enableIndexedDbPersistence(db).then(
() => expect.fail('enablePersistence should not have succeeded!'),
(error: FirestoreError) => {
Expand All @@ -52,7 +52,7 @@ describe('where indexeddb is not available: ', () => {
});

it('falls back without requiring a wait for the promise', () => {
return withTestDb(new MemoryEagerPersistenceMode(), db => {
return withTestDb(PERSISTENCE_MODE_UNSPECIFIED, db => {
const persistenceFailedPromise = enableIndexedDbPersistence(db).catch(
(err: FirestoreError) => {
expect(err.code).to.equal('unimplemented');
Expand All @@ -67,7 +67,7 @@ describe('where indexeddb is not available: ', () => {
});
});

it('fails back to memory cache with initializeFirestore too', () => {
it('falls back to memory cache with initializeFirestore too', () => {
// withTestDb will fail the test if persistence is requested but it fails
// so we'll enable persistence here instead.
return withTestDb(new IndexedDbPersistenceMode(), db => {
Expand Down
40 changes: 25 additions & 15 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ export class IndexedDbPersistenceMode implements PersistenceMode {
}
}

// An alternative to a `PersistenceMode` object that indicates that no
// persistence mode should be specified, and instead the implicit default
// should be used.
export const PERSISTENCE_MODE_UNSPECIFIED = Symbol(
'PERSISTENCE_MODE_UNSPECIFIED'
);

function isIeOrEdge(): boolean {
if (!window.navigator) {
return false;
Expand Down Expand Up @@ -233,7 +240,7 @@ export function toIds(docSet: QuerySnapshot): string[] {
}

export function withTestDb(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
fn: (db: Firestore) => Promise<void>
): Promise<void> {
return withTestDbs(persistence, 1, ([db]) => {
Expand All @@ -243,7 +250,7 @@ export function withTestDb(

/** Runs provided fn with a db for an alternate project id. */
export function withAlternateTestDb(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
fn: (db: Firestore) => Promise<void>
): Promise<void> {
return withTestDbsSettings(
Expand All @@ -258,7 +265,7 @@ export function withAlternateTestDb(
}

export function withTestDbs(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
numDbs: number,
fn: (db: Firestore[]) => Promise<void>
): Promise<void> {
Expand All @@ -271,7 +278,7 @@ export function withTestDbs(
);
}
export async function withTestDbsSettings<T>(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
projectId: string,
settings: PrivateSettings,
numDbs: number,
Expand All @@ -284,10 +291,10 @@ export async function withTestDbsSettings<T>(
const dbs: Firestore[] = [];

for (let i = 0; i < numDbs; i++) {
const newSettings = {
...settings,
localCache: persistence.asLocalCacheFirestoreSettings()
};
const newSettings = { ...settings };
if (persistence !== PERSISTENCE_MODE_UNSPECIFIED) {
newSettings.localCache = persistence.asLocalCacheFirestoreSettings();
}
const db = newTestFirestore(newTestApp(projectId), newSettings);
dbs.push(db);
}
Expand All @@ -297,7 +304,10 @@ export async function withTestDbsSettings<T>(
} finally {
for (const db of dbs) {
await terminate(db);
if (persistence.storage === 'indexeddb') {
if (
persistence !== PERSISTENCE_MODE_UNSPECIFIED &&
persistence.storage === 'indexeddb'
) {
await clearIndexedDbPersistence(db);
}
}
Expand Down Expand Up @@ -340,7 +350,7 @@ export async function withNamedTestDbsOrSkipUnlessUsingEmulator(
}

export function withTestDoc(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
fn: (doc: DocumentReference, db: Firestore) => Promise<void>
): Promise<void> {
return withTestDb(persistence, db => {
Expand All @@ -349,7 +359,7 @@ export function withTestDoc(
}

export function withTestDocAndSettings(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
settings: PrivateSettings,
fn: (doc: DocumentReference) => Promise<void>
): Promise<void> {
Expand All @@ -370,7 +380,7 @@ export function withTestDocAndSettings(
// `withTestDoc(..., docRef => { setDoc(docRef, initialData) ...});` that
// otherwise is quite common.
export function withTestDocAndInitialData(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
initialData: DocumentData | null,
fn: (doc: DocumentReference, db: Firestore) => Promise<void>
): Promise<void> {
Expand Down Expand Up @@ -405,15 +415,15 @@ export async function withRetry<T>(
}

export function withTestCollection<T>(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
docs: { [key: string]: DocumentData },
fn: (collection: CollectionReference, db: Firestore) => Promise<T>
): Promise<T> {
return withTestCollectionSettings(persistence, DEFAULT_SETTINGS, docs, fn);
}

export function withEmptyTestCollection(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
fn: (collection: CollectionReference, db: Firestore) => Promise<void>
): Promise<void> {
return withTestCollection(persistence, {}, fn);
Expand All @@ -422,7 +432,7 @@ export function withEmptyTestCollection(
// TODO(mikelehen): Once we wipe the database between tests, we can probably
// return the same collection every time.
export function withTestCollectionSettings<T>(
persistence: PersistenceMode,
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
settings: PrivateSettings,
docs: { [key: string]: DocumentData },
fn: (collection: CollectionReference, db: Firestore) => Promise<T>
Expand Down