Skip to content

Commit acf9b25

Browse files
committed
Fix 'where indexeddb is not available' tests that were broken by #7404
1 parent 6674fad commit acf9b25

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

packages/firestore/test/integration/browser/indexeddb.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import {
2626
} from '../util/firebase_export';
2727
import {
2828
IndexedDbPersistenceMode,
29-
MemoryEagerPersistenceMode,
3029
isPersistenceAvailable,
31-
withTestDb
30+
withTestDb,
31+
PERSISTENCE_MODE_UNSPECIFIED
3232
} from '../util/helpers';
3333

34-
describe('where indexeddb is not available: ', () => {
34+
describe.only('where indexeddb is not available: ', () => {
3535
// Only test on platforms where persistence is *not* available (e.g. Edge,
3636
// Node.JS).
3737
if (isPersistenceAvailable()) {
@@ -41,7 +41,7 @@ describe('where indexeddb is not available: ', () => {
4141
it('fails with code unimplemented', () => {
4242
// withTestDb will fail the test if persistence is requested but it fails
4343
// so we'll enable persistence here instead.
44-
return withTestDb(new MemoryEagerPersistenceMode(), db => {
44+
return withTestDb(PERSISTENCE_MODE_UNSPECIFIED, db => {
4545
return enableIndexedDbPersistence(db).then(
4646
() => expect.fail('enablePersistence should not have succeeded!'),
4747
(error: FirestoreError) => {
@@ -52,7 +52,7 @@ describe('where indexeddb is not available: ', () => {
5252
});
5353

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

70-
it('fails back to memory cache with initializeFirestore too', () => {
70+
it('falls back to memory cache with initializeFirestore too', () => {
7171
// withTestDb will fail the test if persistence is requested but it fails
7272
// so we'll enable persistence here instead.
7373
return withTestDb(new IndexedDbPersistenceMode(), db => {

packages/firestore/test/integration/util/helpers.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ export class IndexedDbPersistenceMode implements PersistenceMode {
136136
}
137137
}
138138

139+
// An alternative to a `PersistenceMode` object that indicates that no
140+
// persistence mode should be specified, and instead the implicit default
141+
// should be used.
142+
export const PERSISTENCE_MODE_UNSPECIFIED = Symbol(
143+
'PERSISTENCE_MODE_UNSPECIFIED'
144+
);
145+
139146
function isIeOrEdge(): boolean {
140147
if (!window.navigator) {
141148
return false;
@@ -233,7 +240,7 @@ export function toIds(docSet: QuerySnapshot): string[] {
233240
}
234241

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

244251
/** Runs provided fn with a db for an alternate project id. */
245252
export function withAlternateTestDb(
246-
persistence: PersistenceMode,
253+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
247254
fn: (db: Firestore) => Promise<void>
248255
): Promise<void> {
249256
return withTestDbsSettings(
@@ -258,7 +265,7 @@ export function withAlternateTestDb(
258265
}
259266

260267
export function withTestDbs(
261-
persistence: PersistenceMode,
268+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
262269
numDbs: number,
263270
fn: (db: Firestore[]) => Promise<void>
264271
): Promise<void> {
@@ -271,7 +278,7 @@ export function withTestDbs(
271278
);
272279
}
273280
export async function withTestDbsSettings<T>(
274-
persistence: PersistenceMode,
281+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
275282
projectId: string,
276283
settings: PrivateSettings,
277284
numDbs: number,
@@ -284,10 +291,10 @@ export async function withTestDbsSettings<T>(
284291
const dbs: Firestore[] = [];
285292

286293
for (let i = 0; i < numDbs; i++) {
287-
const newSettings = {
288-
...settings,
289-
localCache: persistence.asLocalCacheFirestoreSettings()
290-
};
294+
const newSettings = { ...settings };
295+
if (persistence !== PERSISTENCE_MODE_UNSPECIFIED) {
296+
newSettings.localCache = persistence.asLocalCacheFirestoreSettings();
297+
}
291298
const db = newTestFirestore(newTestApp(projectId), newSettings);
292299
dbs.push(db);
293300
}
@@ -297,7 +304,10 @@ export async function withTestDbsSettings<T>(
297304
} finally {
298305
for (const db of dbs) {
299306
await terminate(db);
300-
if (persistence.storage === 'indexeddb') {
307+
if (
308+
persistence !== PERSISTENCE_MODE_UNSPECIFIED &&
309+
persistence.storage === 'indexeddb'
310+
) {
301311
await clearIndexedDbPersistence(db);
302312
}
303313
}
@@ -340,7 +350,7 @@ export async function withNamedTestDbsOrSkipUnlessUsingEmulator(
340350
}
341351

342352
export function withTestDoc(
343-
persistence: PersistenceMode,
353+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
344354
fn: (doc: DocumentReference, db: Firestore) => Promise<void>
345355
): Promise<void> {
346356
return withTestDb(persistence, db => {
@@ -349,7 +359,7 @@ export function withTestDoc(
349359
}
350360

351361
export function withTestDocAndSettings(
352-
persistence: PersistenceMode,
362+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
353363
settings: PrivateSettings,
354364
fn: (doc: DocumentReference) => Promise<void>
355365
): Promise<void> {
@@ -370,7 +380,7 @@ export function withTestDocAndSettings(
370380
// `withTestDoc(..., docRef => { setDoc(docRef, initialData) ...});` that
371381
// otherwise is quite common.
372382
export function withTestDocAndInitialData(
373-
persistence: PersistenceMode,
383+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
374384
initialData: DocumentData | null,
375385
fn: (doc: DocumentReference, db: Firestore) => Promise<void>
376386
): Promise<void> {
@@ -405,15 +415,15 @@ export async function withRetry<T>(
405415
}
406416

407417
export function withTestCollection<T>(
408-
persistence: PersistenceMode,
418+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
409419
docs: { [key: string]: DocumentData },
410420
fn: (collection: CollectionReference, db: Firestore) => Promise<T>
411421
): Promise<T> {
412422
return withTestCollectionSettings(persistence, DEFAULT_SETTINGS, docs, fn);
413423
}
414424

415425
export function withEmptyTestCollection(
416-
persistence: PersistenceMode,
426+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
417427
fn: (collection: CollectionReference, db: Firestore) => Promise<void>
418428
): Promise<void> {
419429
return withTestCollection(persistence, {}, fn);
@@ -422,7 +432,7 @@ export function withEmptyTestCollection(
422432
// TODO(mikelehen): Once we wipe the database between tests, we can probably
423433
// return the same collection every time.
424434
export function withTestCollectionSettings<T>(
425-
persistence: PersistenceMode,
435+
persistence: PersistenceMode | typeof PERSISTENCE_MODE_UNSPECIFIED,
426436
settings: PrivateSettings,
427437
docs: { [key: string]: DocumentData },
428438
fn: (collection: CollectionReference, db: Firestore) => Promise<T>

0 commit comments

Comments
 (0)