Skip to content

Commit 7afbb0d

Browse files
committed
cleanup
1 parent f58daf1 commit 7afbb0d

File tree

5 files changed

+85
-101
lines changed

5 files changed

+85
-101
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,8 @@ apiDescribe('GetOptions', persistence => {
496496
});
497497
});
498498

499-
// We need the deleted doc to stay in cache, so only run this test with
500-
// persistence with LRU garbage collection (rather than eager GC).
499+
// We need the deleted doc to stay in cache, so only run this test when the
500+
// local cache is configured with LRU GC (as opposed to eager GC).
501501
// eslint-disable-next-line no-restricted-properties,
502502
(persistence.gc === 'lru' ? it : it.skip)(
503503
'get deleted doc while offline with source=cache',

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

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,8 @@ apiDescribe('Queries', persistence => {
13331333
});
13341334
});
13351335

1336-
// OR Query tests only run when the SDK is configured with persistence that
1337-
// uses LRU garbage collection (rather than eager garbage collection) because
1336+
// OR Query tests only run when the SDK's local cache is configured to use
1337+
// LRU garbage collection (rather than eager garbage collection) because
13381338
// they validate that the result from server and cache match.
13391339
// eslint-disable-next-line no-restricted-properties
13401340
(persistence.gc === 'lru' ? describe : describe.skip)('OR Queries', () => {
@@ -1647,8 +1647,8 @@ apiDescribe('Queries', persistence => {
16471647
});
16481648
});
16491649

1650-
// OR Query tests only run when the SDK is configured with persistence that
1651-
// uses LRU garbage collection (rather than eager garbage collection) because
1650+
// OR Query tests only run when the SDK's local cache is configured to use
1651+
// LRU garbage collection (rather than eager garbage collection) because
16521652
// they validate that the result from server and cache match. Additionally,
16531653
// these tests must be skipped if running against production because it
16541654
// results in a 'missing index' error. The Firestore Emulator, however, does
@@ -2034,49 +2034,46 @@ apiDescribe('Queries', persistence => {
20342034
);
20352035

20362036
// Reproduces https://github.com/firebase/firebase-js-sdk/issues/5873
2037-
describe(
2038-
'Caching empty results',
2039-
() => {
2040-
it('can raise initial snapshot from cache, even if it is empty', () => {
2041-
// Use persistence with LRU garbage collection so that the cached resume
2042-
// token and document data do not get cleared.
2043-
return withTestCollection(persistence.toLruGc(), {}, async coll => {
2044-
const snapshot1 = await getDocs(coll); // Populate the cache.
2045-
expect(snapshot1.metadata.fromCache).to.be.false;
2046-
expect(toDataArray(snapshot1)).to.deep.equal([]); // Precondition check.
2047-
2048-
// Add a snapshot listener whose first event should be raised from cache.
2049-
const storeEvent = new EventsAccumulator<QuerySnapshot>();
2050-
onSnapshot(coll, storeEvent.storeEvent);
2051-
const snapshot2 = await storeEvent.awaitEvent();
2052-
expect(snapshot2.metadata.fromCache).to.be.true;
2053-
expect(toDataArray(snapshot2)).to.deep.equal([]);
2054-
});
2037+
describe('Caching empty results', () => {
2038+
it('can raise initial snapshot from cache, even if it is empty', () => {
2039+
// Use persistence with LRU garbage collection so the resume token and
2040+
// document data do not get prematurely deleted from the local cache.
2041+
return withTestCollection(persistence.toLruGc(), {}, async coll => {
2042+
const snapshot1 = await getDocs(coll); // Populate the cache.
2043+
expect(snapshot1.metadata.fromCache).to.be.false;
2044+
expect(toDataArray(snapshot1)).to.deep.equal([]); // Precondition check.
2045+
2046+
// Add a snapshot listener whose first event should be raised from cache.
2047+
const storeEvent = new EventsAccumulator<QuerySnapshot>();
2048+
onSnapshot(coll, storeEvent.storeEvent);
2049+
const snapshot2 = await storeEvent.awaitEvent();
2050+
expect(snapshot2.metadata.fromCache).to.be.true;
2051+
expect(toDataArray(snapshot2)).to.deep.equal([]);
20552052
});
2053+
});
20562054

2057-
it('can raise initial snapshot from cache, even if it has become empty', () => {
2058-
const testDocs = {
2059-
a: { key: 'a' }
2060-
};
2061-
// Use persistence with LRU garbage collection so that the cached resume
2062-
// token and document data do not get cleared.
2063-
return withTestCollection(persistence.toLruGc(), testDocs, async coll => {
2064-
// Populate the cache.
2065-
const snapshot1 = await getDocs(coll);
2066-
expect(snapshot1.metadata.fromCache).to.be.false;
2067-
expect(toDataArray(snapshot1)).to.deep.equal([{ key: 'a' }]);
2068-
// Empty the collection.
2069-
void deleteDoc(doc(coll, 'a'));
2070-
2071-
const storeEvent = new EventsAccumulator<QuerySnapshot>();
2072-
onSnapshot(coll, storeEvent.storeEvent);
2073-
const snapshot2 = await storeEvent.awaitEvent();
2074-
expect(snapshot2.metadata.fromCache).to.be.true;
2075-
expect(toDataArray(snapshot2)).to.deep.equal([]);
2076-
});
2055+
it('can raise initial snapshot from cache, even if it has become empty', () => {
2056+
const testDocs = {
2057+
a: { key: 'a' }
2058+
};
2059+
// Use persistence with LRU garbage collection so the resume token and
2060+
// document data do not get prematurely deleted from the local cache.
2061+
return withTestCollection(persistence.toLruGc(), testDocs, async coll => {
2062+
// Populate the cache.
2063+
const snapshot1 = await getDocs(coll);
2064+
expect(snapshot1.metadata.fromCache).to.be.false;
2065+
expect(toDataArray(snapshot1)).to.deep.equal([{ key: 'a' }]);
2066+
// Empty the collection.
2067+
void deleteDoc(doc(coll, 'a'));
2068+
2069+
const storeEvent = new EventsAccumulator<QuerySnapshot>();
2070+
onSnapshot(coll, storeEvent.storeEvent);
2071+
const snapshot2 = await storeEvent.awaitEvent();
2072+
expect(snapshot2.metadata.fromCache).to.be.true;
2073+
expect(toDataArray(snapshot2)).to.deep.equal([]);
20772074
});
2078-
}
2079-
);
2075+
});
2076+
});
20802077

20812078
it('resuming a query should use bloom filter to avoid full requery', async () => {
20822079
// Prepare the names and contents of the 100 documents to create.
@@ -2142,10 +2139,10 @@ apiDescribe('Queries', persistence => {
21422139
);
21432140
}
21442141

2145-
// Skip the verification of the existence filter mismatch when persistence
2146-
// uses eager garbage collection because with eager GC there is no resume
2147-
// token specified in the subsequent call to getDocs(), and, therefore,
2148-
// Watch will _not_ send an existence filter.
2142+
// Skip the verification of the existence filter mismatch when the local
2143+
// cache is configured to use eager garbage collection because with eager
2144+
// GC there is no resume token specified in the subsequent call to
2145+
// getDocs(), and, therefore, Watch will _not_ send an existence filter.
21492146
// TODO(b/272754156) Re-write this test using a snapshot listener instead
21502147
// of calls to getDocs() and remove this check for disabled persistence.
21512148
if (persistence.gc === 'eager') {

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ import {
5656
} from '../util/firebase_export';
5757
import {
5858
apiDescribe,
59+
PersistenceMode,
5960
withAlternateTestDb,
6061
withTestCollection,
61-
withTestDb,
62-
PersistenceMode
62+
withTestDb
6363
} from '../util/helpers';
6464
import { ALT_PROJECT_ID, DEFAULT_PROJECT_ID } from '../util/settings';
6565

@@ -133,20 +133,8 @@ class TestClass {
133133
constructor(readonly property: string) {}
134134
}
135135

136-
apiDescribe.only('Validation:', persistence => {
136+
apiDescribe('Validation:', persistence => {
137137
describe('FirestoreSettings', () => {
138-
validationIt(
139-
persistence,
140-
'precondition check: verify the Firestore settings are not frozen',
141-
async db => {
142-
// This should not throw an exception. This test just verifies that the
143-
// Firestore instance's initial state does not have its settings frozen,
144-
// because if it did then the following tests would not be testing the
145-
// behavior they intend to test.
146-
connectFirestoreEmulator(db, 'something-else.example.com', 8080)
147-
}
148-
);
149-
150138
validationIt(
151139
persistence,
152140
'disallows changing settings after use',
@@ -175,15 +163,19 @@ apiDescribe.only('Validation:', persistence => {
175163
});
176164
});
177165

178-
validationIt(persistence, 'useEmulator can set host and port', () => {
179-
const db = newTestFirestore(newTestApp('test-project'));
180-
// Verify that this doesn't throw.
181-
connectFirestoreEmulator(db, 'localhost', 9000);
182-
});
166+
validationIt(
167+
persistence,
168+
'connectFirestoreEmulator() can set host and port',
169+
() => {
170+
const db = newTestFirestore(newTestApp('test-project'));
171+
// Verify that this doesn't throw.
172+
connectFirestoreEmulator(db, 'localhost', 9000);
173+
}
174+
);
183175

184176
validationIt(
185177
persistence,
186-
'disallows calling useEmulator after use',
178+
'disallows calling connectFirestoreEmulator() after use',
187179
async db => {
188180
const errorMsg =
189181
'Firestore has already been started and its settings can no longer be changed.';
@@ -197,7 +189,7 @@ apiDescribe.only('Validation:', persistence => {
197189

198190
validationIt(
199191
persistence,
200-
'useEmulator can set mockUserToken object',
192+
'connectFirestoreEmulator() can set mockUserToken object',
201193
() => {
202194
const db = newTestFirestore(newTestApp('test-project'));
203195
// Verify that this doesn't throw.
@@ -209,7 +201,7 @@ apiDescribe.only('Validation:', persistence => {
209201

210202
validationIt(
211203
persistence,
212-
'useEmulator can set mockUserToken string',
204+
'connectFirestoreEmulator() can set mockUserToken string',
213205
() => {
214206
const db = newTestFirestore(newTestApp('test-project'));
215207
// Verify that this doesn't throw.
@@ -235,19 +227,11 @@ apiDescribe.only('Validation:', persistence => {
235227
});
236228

237229
describe('Firestore', () => {
238-
validationIt(
239-
persistence,
240-
'allows calling enableIndexedDbPersistence() when persistence configured in the settings',
241-
async db => {
242-
await enableIndexedDbPersistence(db);
243-
}
244-
);
245-
246230
validationIt(
247231
persistence,
248232
'disallows calling enableIndexedDbPersistence() after use',
249233
db => {
250-
doc(db, 'foo/bar');
234+
//doc(db, 'foo/bar');
251235
expect(() => enableIndexedDbPersistence(db)).to.throw(
252236
'SDK cache is already specified.'
253237
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use(chaiAsPromised);
3535

3636
apiDescribe('Database (with internal API)', persistence => {
3737
// eslint-disable-next-line no-restricted-properties
38-
(persistence.gc === 'lru' ? it : it.skip)(
38+
(persistence.storage === 'indexeddb' ? it : it.skip)(
3939
'will reject the promise if clear persistence fails',
4040
async () => {
4141
await withTestDoc(persistence, async (docRef, firestore) => {

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface PersistenceMode {
7171
* Creates and returns a new "local cache" object corresponding to this
7272
* persistence type.
7373
*/
74-
toLocalCache(): MemoryLocalCache | PersistentLocalCache;
74+
asLocalCacheFirestoreSettings(): MemoryLocalCache | PersistentLocalCache;
7575
}
7676

7777
export class MemoryEagerPersistenceMode implements PersistenceMode {
@@ -87,7 +87,7 @@ export class MemoryEagerPersistenceMode implements PersistenceMode {
8787
return new MemoryLruPersistenceMode();
8888
}
8989

90-
toLocalCache(): MemoryLocalCache {
90+
asLocalCacheFirestoreSettings(): MemoryLocalCache {
9191
return memoryLocalCache({
9292
garbageCollector: memoryEagerGarbageCollector()
9393
});
@@ -107,7 +107,7 @@ export class MemoryLruPersistenceMode implements PersistenceMode {
107107
return new MemoryLruPersistenceMode();
108108
}
109109

110-
toLocalCache(): MemoryLocalCache {
110+
asLocalCacheFirestoreSettings(): MemoryLocalCache {
111111
return memoryLocalCache({ garbageCollector: memoryLruGarbageCollector() });
112112
}
113113
}
@@ -125,9 +125,12 @@ export class IndexedDbPersistenceMode implements PersistenceMode {
125125
return new IndexedDbPersistenceMode();
126126
}
127127

128-
toLocalCache(): PersistentLocalCache {
129-
if (this.gc != 'lru') {
130-
throw new Error(`unsupported gc: ${this.gc}`);
128+
asLocalCacheFirestoreSettings(): PersistentLocalCache {
129+
if (this.gc !== 'lru') {
130+
throw new Error(
131+
`PersistentLocalCache does not support the given ` +
132+
`garbage collector: ${this.gc}`
133+
);
131134
}
132135
return persistentLocalCache();
133136
}
@@ -173,11 +176,11 @@ function apiDescribeInternal(
173176
}
174177

175178
for (const persistenceMode of persistenceModes) {
176-
// Freeze the persistence mode so that tests don't modify the persistence
177-
// mode, which is shared between tests.
178-
const frozenPersistenceMode = Object.freeze(persistenceMode);
179179
describeFn(`(Persistence=${persistenceMode.name}) ${message}`, () =>
180-
testSuite(frozenPersistenceMode)
180+
// Freeze the properties of the `PersistenceMode` object specified to the
181+
// test suite so that it cannot (accidentally or intentionally) change
182+
// its properties, and affect all subsequent test suites.
183+
testSuite(Object.freeze(persistenceMode))
181184
);
182185
}
183186
}
@@ -230,7 +233,7 @@ export function toIds(docSet: QuerySnapshot): string[] {
230233
}
231234

232235
export function withTestDb(
233-
persistence: PersistenceMode | null,
236+
persistence: PersistenceMode,
234237
fn: (db: Firestore) => Promise<void>
235238
): Promise<void> {
236239
return withTestDbs(persistence, 1, ([db]) => {
@@ -255,7 +258,7 @@ export function withAlternateTestDb(
255258
}
256259

257260
export function withTestDbs(
258-
persistence: PersistenceMode | null,
261+
persistence: PersistenceMode,
259262
numDbs: number,
260263
fn: (db: Firestore[]) => Promise<void>
261264
): Promise<void> {
@@ -268,7 +271,7 @@ export function withTestDbs(
268271
);
269272
}
270273
export async function withTestDbsSettings<T>(
271-
persistence: PersistenceMode | null,
274+
persistence: PersistenceMode,
272275
projectId: string,
273276
settings: PrivateSettings,
274277
numDbs: number,
@@ -281,10 +284,10 @@ export async function withTestDbsSettings<T>(
281284
const dbs: Firestore[] = [];
282285

283286
for (let i = 0; i < numDbs; i++) {
284-
const newSettings = { ...settings };
285-
if (persistence !== null) {
286-
newSettings.localCache = persistence.toLocalCache();
287-
}
287+
const newSettings = {
288+
...settings,
289+
localCache: persistence.asLocalCacheFirestoreSettings()
290+
};
288291
const db = newTestFirestore(newTestApp(projectId), newSettings);
289292
dbs.push(db);
290293
}
@@ -318,7 +321,7 @@ export async function withNamedTestDbsOrSkipUnlessUsingEmulator(
318321
for (const dbName of dbNames) {
319322
const newSettings = {
320323
...DEFAULT_SETTINGS,
321-
localCache: persistence.toLocalCache()
324+
localCache: persistence.asLocalCacheFirestoreSettings()
322325
};
323326
const db = newTestFirestore(app, newSettings, dbName);
324327
dbs.push(db);

0 commit comments

Comments
 (0)