Skip to content

Commit c628e6a

Browse files
committed
Fix some test failures
1 parent 48d5077 commit c628e6a

File tree

9 files changed

+43
-27
lines changed

9 files changed

+43
-27
lines changed

packages/firestore/src/api/cache_config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MemoryLocalCacheImpl implements MemoryLocalCache {
3939
this._offlineComponentProvider = new MemoryOfflineComponentProvider();
4040
}
4141

42-
toJSON() {
42+
toJSON(): {} {
4343
return { kind: this.kind };
4444
}
4545
}
@@ -68,7 +68,7 @@ class IndexedDbLocalCacheImpl implements IndexedDbLocalCache {
6868
this._offlineComponentProvider = tabManager._offlineComponentProvider!;
6969
}
7070

71-
toJSON() {
71+
toJSON(): {} {
7272
return { kind: this.kind };
7373
}
7474
}
@@ -110,7 +110,7 @@ class SingleTabManagerImpl implements IndexedDbSingleTabManager {
110110

111111
constructor(private forceOwnership?: boolean) {}
112112

113-
toJSON() {
113+
toJSON(): {} {
114114
return { kind: this.kind };
115115
}
116116

@@ -139,7 +139,7 @@ class MultiTabManagerImpl implements IndexedDbMultipleTabManager {
139139
_onlineComponentProvider?: OnlineComponentProvider;
140140
_offlineComponentProvider?: OfflineComponentProvider;
141141

142-
toJSON() {
142+
toJSON(): {} {
143143
return { kind: this.kind };
144144
}
145145

packages/firestore/src/api/database.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export function configureFirestore(firestore: Firestore): void {
288288
settings.cache?._onlineComponentProvider
289289
) {
290290
firestore._firestoreClient.uninitializedComponentsProvider = {
291+
offlineKind: settings.cache.kind,
291292
offline: settings.cache._offlineComponentProvider,
292293
online: settings.cache._onlineComponentProvider
293294
};
@@ -327,7 +328,10 @@ export function enableIndexedDbPersistence(
327328

328329
const client = ensureFirestoreConfigured(firestore);
329330
if (client.uninitializedComponentsProvider) {
330-
throw new FirestoreError(Code.INVALID_ARGUMENT, 'Already specified.');
331+
throw new FirestoreError(
332+
Code.FAILED_PRECONDITION,
333+
'SDK cache is already specified.'
334+
);
331335
}
332336

333337
const settings = firestore._freezeSettings();
@@ -376,7 +380,10 @@ export function enableMultiTabIndexedDbPersistence(
376380

377381
const client = ensureFirestoreConfigured(firestore);
378382
if (client.uninitializedComponentsProvider) {
379-
throw new FirestoreError(Code.INVALID_ARGUMENT, 'Already specified.');
383+
throw new FirestoreError(
384+
Code.FAILED_PRECONDITION,
385+
'SDK cache is already specified.'
386+
);
380387
}
381388

382389
const settings = firestore._freezeSettings();

packages/firestore/src/api/index_configuration.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { getLocalStore } from '../core/firestore_client';
18+
import { firestoreClientSetIndexConfiguration } from '../core/firestore_client';
1919
import { fieldPathFromDotSeparatedString } from '../lite-api/user_data_reader';
20-
import { localStoreConfigureFieldIndexes } from '../local/local_store_impl';
2120
import {
2221
FieldIndex,
2322
IndexKind,
@@ -151,17 +150,17 @@ export function setIndexConfiguration(
151150
): Promise<void> {
152151
firestore = cast(firestore, Firestore);
153152
const client = ensureFirestoreConfigured(firestore);
154-
155-
// PORTING NOTE: We don't return an error if the user has not enabled
156-
// persistence since `enableIndexeddbPersistence()` can fail on the Web.
157-
if (!client.offlineComponents?.indexBackfillerScheduler) {
153+
if (
154+
!client.uninitializedComponentsProvider ||
155+
client.uninitializedComponentsProvider?.offlineKind === 'memory'
156+
) {
157+
// PORTING NOTE: We don't return an error if the user has not enabled
158+
// persistence since `enableIndexeddbPersistence()` can fail on the Web.
158159
logWarn('Cannot enable indexes when persistence is disabled');
159160
return Promise.resolve();
160161
}
161162
const parsedIndexes = parseIndexes(jsonOrConfiguration);
162-
return getLocalStore(client).then(localStore =>
163-
localStoreConfigureFieldIndexes(localStore, parsedIndexes)
164-
);
163+
return firestoreClientSetIndexConfiguration(client, parsedIndexes);
165164
}
166165

167166
export function parseIndexes(

packages/firestore/src/core/firestore_client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { User } from '../auth/user';
3131
import { Query as LiteQuery } from '../lite-api/reference';
3232
import { LocalStore } from '../local/local_store';
3333
import {
34+
localStoreConfigureFieldIndexes,
3435
localStoreExecuteQuery,
3536
localStoreGetNamedQuery,
3637
localStoreHandleUserChange,
@@ -39,6 +40,7 @@ import {
3940
import { Persistence } from '../local/persistence';
4041
import { Document } from '../model/document';
4142
import { DocumentKey } from '../model/document_key';
43+
import { FieldIndex } from '../model/field_index';
4244
import { Mutation } from '../model/mutation';
4345
import { toByteStreamReader } from '../platform/byte_stream_reader';
4446
import { newSerializer, newTextEncoder } from '../platform/serializer';
@@ -114,6 +116,7 @@ export class FirestoreClient {
114116
) => Promise<void> = () => Promise.resolve();
115117
uninitializedComponentsProvider?: {
116118
offline: OfflineComponentProvider;
119+
offlineKind: 'memory' | 'indexeddb';
117120
online: OnlineComponentProvider;
118121
};
119122

@@ -768,3 +771,15 @@ function createBundleReader(
768771
}
769772
return newBundleReader(toByteStreamReader(content), serializer);
770773
}
774+
775+
export function firestoreClientSetIndexConfiguration(
776+
client: FirestoreClient,
777+
indexes: FieldIndex[]
778+
): Promise<void> {
779+
return client.asyncQueue.enqueue(async () => {
780+
return localStoreConfigureFieldIndexes(
781+
await getLocalStore(client),
782+
indexes
783+
);
784+
});
785+
}

packages/firestore/src/lite-api/settings.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../local/lru_garbage_collector_impl';
2525
import { Code, FirestoreError } from '../util/error';
2626
import { validateIsNotUsedTogether } from '../util/input_validation';
27-
import { logWarn } from '../util/log';
2827

2928
// settings() defaults:
3029
export const DEFAULT_HOST = 'firestore.googleapis.com';

packages/firestore/test/integration/api/database.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ apiDescribe('Database', (persistence: boolean) => {
12141214
'cannot clear persistence if the client has been initialized',
12151215
async () => {
12161216
await withTestDoc(persistence, async (docRef, firestore) => {
1217+
await setDoc(docRef, {});
12171218
const expectedError =
12181219
'Persistence can only be cleared before a Firestore instance is ' +
12191220
'initialized or after it is terminated.';

packages/firestore/test/integration/api/validation.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ apiDescribe('Validation:', (persistence: boolean) => {
240240
doc(db, 'foo/bar');
241241
}
242242
expect(() => enableIndexedDbPersistence(db)).to.throw(
243-
'Firestore has already been started and persistence can no ' +
244-
'longer be enabled. You can only enable persistence before ' +
245-
'calling any other methods on a Firestore object.'
243+
'SDK cache is already specified.'
246244
);
247245
}
248246
);

packages/firestore/test/integration/api_internal/idle_timeout.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { apiDescribe, withTestDb } from '../util/helpers';
2222
import { asyncQueue } from '../util/internal_helpers';
2323

2424
apiDescribe('Idle Timeout', (persistence: boolean) => {
25-
it.only('can write document after idle timeout', () => {
25+
it('can write document after idle timeout', () => {
2626
return withTestDb(persistence, db => {
2727
const docRef = doc(collection(db, 'test-collection'));
2828
return setDoc(docRef, { foo: 'bar' })

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
import { isIndexedDBAvailable } from '@firebase/util';
1919

20-
import {
21-
indexedDbLocalCache,
22-
memoryLocalCache
23-
} from '../../../src/api/cache_config';
24-
import { logWarn } from '../../../src/util/log';
20+
import { indexedDbLocalCache } from '../../../src/api/cache_config';
2521

2622
import {
2723
collection,
@@ -191,10 +187,11 @@ export async function withTestDbsSettings(
191187

192188
for (let i = 0; i < numDbs; i++) {
193189
// logWarn(`set persistence from helper: ${persistence}`);
190+
const newSettings = { ...settings };
194191
if (persistence) {
195-
settings.cache = indexedDbLocalCache();
192+
newSettings.cache = indexedDbLocalCache();
196193
}
197-
const db = newTestFirestore(newTestApp(projectId), settings);
194+
const db = newTestFirestore(newTestApp(projectId), newSettings);
198195
dbs.push(db);
199196
}
200197

0 commit comments

Comments
 (0)