Skip to content

Commit fbf3b35

Browse files
committed
fixed unit tests
1 parent 90cc022 commit fbf3b35

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed

packages/firestore/src/api/index_configuration.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
import { firestoreClientSetIndexConfiguration } from '../core/firestore_client';
1919
import { fieldPathFromDotSeparatedString } from '../lite-api/user_data_reader';
20+
import {
21+
getPersistentCacheIndexManager,
22+
registerFieldIndexManagementApi
23+
} from './persistent_cache_index_manager';
2024
import {
2125
FieldIndex,
2226
IndexKind,
@@ -171,19 +175,21 @@ export function setIndexConfiguration(
171175
firestore: Firestore,
172176
jsonOrConfiguration: string | IndexConfiguration
173177
): Promise<void> {
174-
firestore = cast(firestore, Firestore);
175-
const client = ensureFirestoreConfigured(firestore);
176-
if (
177-
!client._uninitializedComponentsProvider ||
178-
client._uninitializedComponentsProvider?._offlineKind === 'memory'
179-
) {
178+
const persistentCacheIndexManager = getPersistentCacheIndexManager(firestore);
179+
if (!persistentCacheIndexManager) {
180180
// PORTING NOTE: We don't return an error if the user has not enabled
181181
// persistence since `enableIndexeddbPersistence()` can fail on the Web.
182182
logWarn('Cannot enable indexes when persistence is disabled');
183183
return Promise.resolve();
184184
}
185+
186+
registerFieldIndexManagementApi(persistentCacheIndexManager);
187+
185188
const parsedIndexes = parseIndexes(jsonOrConfiguration);
186-
return firestoreClientSetIndexConfiguration(client, parsedIndexes);
189+
return firestoreClientSetIndexConfiguration(
190+
persistentCacheIndexManager._client,
191+
parsedIndexes
192+
);
187193
}
188194

189195
export function parseIndexes(

packages/firestore/src/api/persistent_cache_index_manager.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ export function getPersistentCacheIndexManager(
7171
return instance;
7272
}
7373

74+
export function registerFieldIndexManagementApi(
75+
indexManager: PersistentCacheIndexManager
76+
): FieldIndexManagementApiImpl {
77+
if (!indexManager._fieldIndexManagementApi) {
78+
indexManager._fieldIndexManagementApi = new FieldIndexManagementApiImpl();
79+
}
80+
return indexManager._fieldIndexManagementApi;
81+
}
82+
7483
/**
7584
* Enables the SDK to create persistent cache indexes automatically for local
7685
* query execution when the SDK believes cache indexes can help improve
@@ -84,24 +93,19 @@ export function enablePersistentCacheIndexAutoCreation(
8493
const client: FirestoreClient = indexManager._client;
8594
client.verifyNotTerminated();
8695

87-
if (!indexManager._fieldIndexManagementApi) {
88-
indexManager._fieldIndexManagementApi = new FieldIndexManagementApiImpl();
89-
}
96+
const fieldIndexManagementApi = registerFieldIndexManagementApi(indexManager);
9097

91-
indexManager._fieldIndexManagementApi.indexAutoCreationEnabled = true;
98+
fieldIndexManagementApi.indexAutoCreationEnabled = true;
9299

93100
if (indexManager._fieldIndexManagementApiInstallPromise) {
94101
return;
95102
}
96103

97104
indexManager._fieldIndexManagementApiInstallPromise =
98-
firestoreClientSetFieldIndexManagementApi(
99-
client,
100-
indexManager._fieldIndexManagementApi
101-
)
102-
.then(_ => {
103-
logDebug('enabling persistent cache index auto creation succeeded');
104-
})
105+
firestoreClientSetFieldIndexManagementApi(client, fieldIndexManagementApi)
106+
.then(_ =>
107+
logDebug('enabling persistent cache index auto creation succeeded')
108+
)
105109
.catch(error =>
106110
logWarn('enabling persistent cache index auto creation failed', error)
107111
);

packages/firestore/src/index/field_index_management_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { LocalDocumentsView } from '../local/local_documents_view';
2323
* limitations under the License.
2424
*/
2525
export interface FieldIndexManagementApi {
26+
indexAutoCreationEnabled: boolean;
27+
2628
createCacheIndexes(
2729
transaction: PersistenceTransaction,
2830
indexManager: IndexManager,

packages/firestore/src/local/query_engine.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ export class QueryEngine {
144144
return this.executeFullCollectionScan(transaction, query, context).next(
145145
result => {
146146
queryResult.result = result;
147-
if (this.fieldIndexManagementApi) {
147+
if (
148+
this.fieldIndexManagementApi?.indexAutoCreationEnabled === true
149+
) {
148150
return this.fieldIndexManagementApi.createCacheIndexes(
149151
transaction,
150152
this.indexManager,

packages/firestore/test/unit/local/local_store_indexeddb.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class AsyncLocalStoreTester {
179179
}
180180

181181
async configureFieldsIndexes(...indexes: FieldIndex[]): Promise<void> {
182+
localStoreGetOrSetFieldIndexManagementApi(
183+
this.localStore,
184+
() => new FieldIndexManagementApiImpl()
185+
);
182186
await localStoreConfigureFieldIndexes(this.localStore, indexes);
183187
}
184188

packages/firestore/test/unit/local/query_engine.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ function genericQueryEngineTest(
286286
);
287287
queryEngine.initialize(localDocuments, underlyingIndexManager);
288288

289+
queryEngine.fieldIndexManagementApi = new FieldIndexManagementApiImpl();
290+
289291
indexManager = new TestIndexManager(persistence, underlyingIndexManager);
290292
});
291293

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ import {
7878
DbPrimaryClientStore
7979
} from '../../../src/local/indexeddb_sentinels';
8080
import { LocalStore } from '../../../src/local/local_store';
81-
import { localStoreConfigureFieldIndexes } from '../../../src/local/local_store_impl';
81+
import {
82+
localStoreConfigureFieldIndexes,
83+
localStoreGetOrSetFieldIndexManagementApi
84+
} from '../../../src/local/local_store_impl';
8285
import { LruGarbageCollector } from '../../../src/local/lru_garbage_collector';
8386
import { MemoryLruDelegate } from '../../../src/local/memory_persistence';
8487
import {
@@ -178,6 +181,7 @@ import {
178181
QueryEvent,
179182
SharedWriteTracker
180183
} from './spec_test_components';
184+
import { FieldIndexManagementApiImpl } from '../../../src/index/field_index_management';
181185

182186
use(chaiExclude);
183187

@@ -583,6 +587,10 @@ abstract class TestRunner {
583587
): Promise<void> {
584588
return this.queue.enqueue(async () => {
585589
const parsedIndexes = parseIndexes(jsonOrConfiguration);
590+
localStoreGetOrSetFieldIndexManagementApi(
591+
this.localStore,
592+
() => new FieldIndexManagementApiImpl()
593+
);
586594
return localStoreConfigureFieldIndexes(this.localStore, parsedIndexes);
587595
});
588596
}

0 commit comments

Comments
 (0)