Skip to content

Commit 7e45723

Browse files
authored
Merge 5138e8a into f2fb56f
2 parents f2fb56f + 5138e8a commit 7e45723

11 files changed

+658
-585
lines changed

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

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -160,80 +160,84 @@ apiDescribe('Array Transforms:', persistence => {
160160
* Unlike the withTestSetup() tests above, these tests intentionally avoid
161161
* having any ongoing listeners so that we can test what gets stored in the
162162
* offline cache based purely on the write acknowledgement (without receiving
163-
* an updated document via watch). As such they also rely on persistence
164-
* being enabled so documents remain in the cache after the write.
163+
* an updated document via watch). As such they also rely on persistence with
164+
* LRU garbage collection (rather than eager garbage collection) so documents
165+
* remain in the cache after the write.
165166
*/
166167
// eslint-disable-next-line no-restricted-properties
167-
(persistence ? describe : describe.skip)('Server Application: ', () => {
168-
it('set() with no cached base doc', async () => {
169-
await withTestDoc(persistence, async docRef => {
170-
await setDoc(docRef, { array: arrayUnion(1, 2) });
171-
const snapshot = await getDocFromCache(docRef);
172-
expect(snapshot.data()).to.deep.equal({ array: [1, 2] });
173-
});
174-
});
175-
176-
it('update() with no cached base doc', async () => {
177-
let path: string | null = null;
178-
// Write an initial document in an isolated Firestore instance so it's not
179-
// stored in our cache
180-
await withTestDoc(persistence, async docRef => {
181-
path = docRef.path;
182-
await setDoc(docRef, { array: [42] });
168+
(persistence.gc === 'lru' ? describe : describe.skip)(
169+
'Server Application: ',
170+
() => {
171+
it('set() with no cached base doc', async () => {
172+
await withTestDoc(persistence, async docRef => {
173+
await setDoc(docRef, { array: arrayUnion(1, 2) });
174+
const snapshot = await getDocFromCache(docRef);
175+
expect(snapshot.data()).to.deep.equal({ array: [1, 2] });
176+
});
183177
});
184178

185-
await withTestDb(persistence, async db => {
186-
const docRef = doc(db, path!);
187-
await updateDoc(docRef, { array: arrayUnion(1, 2) });
188-
189-
// Nothing should be cached since it was an update and we had no base
190-
// doc.
191-
let errCaught = false;
192-
try {
193-
await getDocFromCache(docRef);
194-
} catch (err) {
195-
expect((err as FirestoreError).code).to.equal('unavailable');
196-
errCaught = true;
197-
}
198-
expect(errCaught).to.be.true;
179+
it('update() with no cached base doc', async () => {
180+
let path: string | null = null;
181+
// Write an initial document in an isolated Firestore instance so it's not
182+
// stored in our cache
183+
await withTestDoc(persistence, async docRef => {
184+
path = docRef.path;
185+
await setDoc(docRef, { array: [42] });
186+
});
187+
188+
await withTestDb(persistence, async db => {
189+
const docRef = doc(db, path!);
190+
await updateDoc(docRef, { array: arrayUnion(1, 2) });
191+
192+
// Nothing should be cached since it was an update and we had no base
193+
// doc.
194+
let errCaught = false;
195+
try {
196+
await getDocFromCache(docRef);
197+
} catch (err) {
198+
expect((err as FirestoreError).code).to.equal('unavailable');
199+
errCaught = true;
200+
}
201+
expect(errCaught).to.be.true;
202+
});
199203
});
200-
});
201204

202-
it('set(..., {merge}) with no cached based doc', async () => {
203-
let path: string | null = null;
204-
// Write an initial document in an isolated Firestore instance so it's not
205-
// stored in our cache
206-
await withTestDoc(persistence, async docRef => {
207-
path = docRef.path;
208-
await setDoc(docRef, { array: [42] });
205+
it('set(..., {merge}) with no cached based doc', async () => {
206+
let path: string | null = null;
207+
// Write an initial document in an isolated Firestore instance so it's not
208+
// stored in our cache
209+
await withTestDoc(persistence, async docRef => {
210+
path = docRef.path;
211+
await setDoc(docRef, { array: [42] });
212+
});
213+
214+
await withTestDb(persistence, async db => {
215+
const docRef = doc(db, path!);
216+
await setDoc(docRef, { array: arrayUnion(1, 2) }, { merge: true });
217+
218+
// Document will be cached but we'll be missing 42.
219+
const snapshot = await getDocFromCache(docRef);
220+
expect(snapshot.data()).to.deep.equal({ array: [1, 2] });
221+
});
209222
});
210223

211-
await withTestDb(persistence, async db => {
212-
const docRef = doc(db, path!);
213-
await setDoc(docRef, { array: arrayUnion(1, 2) }, { merge: true });
214-
215-
// Document will be cached but we'll be missing 42.
216-
const snapshot = await getDocFromCache(docRef);
217-
expect(snapshot.data()).to.deep.equal({ array: [1, 2] });
224+
it('update() with cached base doc using arrayUnion()', async () => {
225+
await withTestDoc(persistence, async docRef => {
226+
await setDoc(docRef, { array: [42] });
227+
await updateDoc(docRef, { array: arrayUnion(1, 2) });
228+
const snapshot = await getDocFromCache(docRef);
229+
expect(snapshot.data()).to.deep.equal({ array: [42, 1, 2] });
230+
});
218231
});
219-
});
220232

221-
it('update() with cached base doc using arrayUnion()', async () => {
222-
await withTestDoc(persistence, async docRef => {
223-
await setDoc(docRef, { array: [42] });
224-
await updateDoc(docRef, { array: arrayUnion(1, 2) });
225-
const snapshot = await getDocFromCache(docRef);
226-
expect(snapshot.data()).to.deep.equal({ array: [42, 1, 2] });
233+
it('update() with cached base doc using arrayRemove()', async () => {
234+
await withTestDoc(persistence, async docRef => {
235+
await setDoc(docRef, { array: [42, 1, 2] });
236+
await updateDoc(docRef, { array: arrayRemove(1, 2) });
237+
const snapshot = await getDocFromCache(docRef);
238+
expect(snapshot.data()).to.deep.equal({ array: [42] });
239+
});
227240
});
228-
});
229-
230-
it('update() with cached base doc using arrayRemove()', async () => {
231-
await withTestDoc(persistence, async docRef => {
232-
await setDoc(docRef, { array: [42, 1, 2] });
233-
await updateDoc(docRef, { array: arrayRemove(1, 2) });
234-
const snapshot = await getDocFromCache(docRef);
235-
expect(snapshot.data()).to.deep.equal({ array: [42] });
236-
});
237-
});
238-
});
241+
}
242+
);
239243
});

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

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,13 @@ import {
6767
} from '../util/firebase_export';
6868
import {
6969
apiDescribe,
70-
withEnsuredLruGcTestDb,
7170
withTestCollection,
7271
withTestDbsSettings,
7372
withTestDb,
7473
withTestDbs,
7574
withTestDoc,
7675
withTestDocAndInitialData,
77-
withNamedTestDbsOrSkipUnlessUsingEmulator,
78-
withEnsuredEagerGcTestDb
76+
withNamedTestDbsOrSkipUnlessUsingEmulator
7977
} from '../util/helpers';
8078
import { DEFAULT_SETTINGS, DEFAULT_PROJECT_ID } from '../util/settings';
8179

@@ -153,29 +151,32 @@ apiDescribe('Database', persistence => {
153151
});
154152

155153
// eslint-disable-next-line no-restricted-properties
156-
(persistence ? it : it.skip)('can update an unknown document', () => {
157-
return withTestDbs(persistence, 2, async ([reader, writer]) => {
158-
const writerRef = doc(collection(writer, 'collection'));
159-
const readerRef = doc(collection(reader, 'collection'), writerRef.id);
160-
await setDoc(writerRef, { a: 'a' });
161-
await updateDoc(readerRef, { b: 'b' });
162-
await getDocFromCache(writerRef).then(
163-
doc => expect(doc.exists()).to.be.true
164-
);
165-
await getDocFromCache(readerRef).then(
166-
() => {
167-
expect.fail('Expected cache miss');
168-
},
169-
err => expect(err.code).to.be.equal('unavailable')
170-
);
171-
await getDoc(writerRef).then(doc =>
172-
expect(doc.data()).to.deep.equal({ a: 'a', b: 'b' })
173-
);
174-
await getDoc(readerRef).then(doc =>
175-
expect(doc.data()).to.deep.equal({ a: 'a', b: 'b' })
176-
);
177-
});
178-
});
154+
(persistence.gc === 'lru' ? it : it.skip)(
155+
'can update an unknown document',
156+
() => {
157+
return withTestDbs(persistence, 2, async ([reader, writer]) => {
158+
const writerRef = doc(collection(writer, 'collection'));
159+
const readerRef = doc(collection(reader, 'collection'), writerRef.id);
160+
await setDoc(writerRef, { a: 'a' });
161+
await updateDoc(readerRef, { b: 'b' });
162+
await getDocFromCache(writerRef).then(
163+
doc => expect(doc.exists()).to.be.true
164+
);
165+
await getDocFromCache(readerRef).then(
166+
() => {
167+
expect.fail('Expected cache miss');
168+
},
169+
err => expect(err.code).to.be.equal('unavailable')
170+
);
171+
await getDoc(writerRef).then(doc =>
172+
expect(doc.data()).to.deep.equal({ a: 'a', b: 'b' })
173+
);
174+
await getDoc(readerRef).then(doc =>
175+
expect(doc.data()).to.deep.equal({ a: 'a', b: 'b' })
176+
);
177+
});
178+
}
179+
);
179180

180181
it('can merge data with an existing document using set', () => {
181182
return withTestDoc(persistence, doc => {
@@ -1095,32 +1096,35 @@ apiDescribe('Database', persistence => {
10951096
});
10961097

10971098
// eslint-disable-next-line no-restricted-properties
1098-
(persistence ? it : it.skip)('offline writes are sent after restart', () => {
1099-
return withTestDoc(persistence, async (docRef, firestore) => {
1100-
const app = firestore.app;
1101-
const name = app.name;
1102-
const options = app.options;
1099+
(persistence.storage === 'indexeddb' ? it : it.skip)(
1100+
'offline writes are sent after restart',
1101+
() => {
1102+
return withTestDoc(persistence, async (docRef, firestore) => {
1103+
const app = firestore.app;
1104+
const name = app.name;
1105+
const options = app.options;
11031106

1104-
await disableNetwork(firestore);
1107+
await disableNetwork(firestore);
11051108

1106-
// We are merely adding to the cache.
1107-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
1108-
setDoc(docRef, { foo: 'bar' });
1109+
// We are merely adding to the cache.
1110+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
1111+
setDoc(docRef, { foo: 'bar' });
11091112

1110-
await deleteApp(app);
1113+
await deleteApp(app);
11111114

1112-
const firestore2 = newTestFirestore(
1113-
newTestApp(options.projectId!, name),
1114-
DEFAULT_SETTINGS
1115-
);
1116-
await enableIndexedDbPersistence(firestore2);
1117-
await waitForPendingWrites(firestore2);
1118-
const doc2 = await getDoc(doc(firestore2, docRef.path));
1115+
const firestore2 = newTestFirestore(
1116+
newTestApp(options.projectId!, name),
1117+
DEFAULT_SETTINGS
1118+
);
1119+
await enableIndexedDbPersistence(firestore2);
1120+
await waitForPendingWrites(firestore2);
1121+
const doc2 = await getDoc(doc(firestore2, docRef.path));
11191122

1120-
expect(doc2.exists()).to.be.true;
1121-
expect(doc2.metadata.hasPendingWrites).to.be.false;
1122-
});
1123-
});
1123+
expect(doc2.exists()).to.be.true;
1124+
expect(doc2.metadata.hasPendingWrites).to.be.false;
1125+
});
1126+
}
1127+
);
11241128

11251129
it('rejects subsequent method calls after terminate() is called', async () => {
11261130
return withTestDb(persistence, db => {
@@ -1141,7 +1145,7 @@ apiDescribe('Database', persistence => {
11411145
});
11421146

11431147
// eslint-disable-next-line no-restricted-properties
1144-
(persistence ? it : it.skip)(
1148+
(persistence.storage === 'indexeddb' ? it : it.skip)(
11451149
'maintains persistence after restarting app',
11461150
async () => {
11471151
await withTestDoc(persistence, async docRef => {
@@ -1164,7 +1168,7 @@ apiDescribe('Database', persistence => {
11641168
);
11651169

11661170
// eslint-disable-next-line no-restricted-properties
1167-
(persistence ? it : it.skip)(
1171+
(persistence.storage === 'indexeddb' ? it : it.skip)(
11681172
'can clear persistence if the client has been terminated',
11691173
async () => {
11701174
await withTestDoc(persistence, async (docRef, firestore) => {
@@ -1188,7 +1192,7 @@ apiDescribe('Database', persistence => {
11881192
);
11891193

11901194
// eslint-disable-next-line no-restricted-properties
1191-
(persistence ? it : it.skip)(
1195+
(persistence.storage === 'indexeddb' ? it : it.skip)(
11921196
'can clear persistence if the client has not been initialized',
11931197
async () => {
11941198
await withTestDoc(persistence, async docRef => {
@@ -1212,7 +1216,7 @@ apiDescribe('Database', persistence => {
12121216
);
12131217

12141218
// eslint-disable-next-line no-restricted-properties
1215-
(persistence ? it : it.skip)(
1219+
(persistence.storage === 'indexeddb' ? it : it.skip)(
12161220
'cannot clear persistence if the client has been initialized',
12171221
async () => {
12181222
await withTestDoc(persistence, async (docRef, firestore) => {
@@ -1757,7 +1761,7 @@ apiDescribe('Database', persistence => {
17571761

17581762
it('Cannot get document from cache with eager GC enabled.', () => {
17591763
const initialData = { key: 'value' };
1760-
return withEnsuredEagerGcTestDb(async db => {
1764+
return withTestDb(persistence.toEagerGc(), async db => {
17611765
const docRef = doc(collection(db, 'test-collection'));
17621766
await setDoc(docRef, initialData);
17631767
await expect(getDocFromCache(docRef)).to.be.rejectedWith('Failed to get');
@@ -1766,7 +1770,7 @@ apiDescribe('Database', persistence => {
17661770

17671771
it('Can get document from cache with Lru GC enabled.', () => {
17681772
const initialData = { key: 'value' };
1769-
return withEnsuredLruGcTestDb(persistence, async db => {
1773+
return withTestDb(persistence.toLruGc(), async db => {
17701774
const docRef = doc(collection(db, 'test-collection'));
17711775
await setDoc(docRef, initialData);
17721776
return getDocFromCache(docRef).then(doc => {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
apiDescribe,
3737
withTestCollection,
3838
withTestDocAndInitialData,
39-
withEnsuredLruGcTestDb
39+
withTestDb
4040
} from '../util/helpers';
4141

4242
apiDescribe('GetOptions', persistence => {
@@ -70,8 +70,8 @@ apiDescribe('GetOptions', persistence => {
7070

7171
it('get document while offline with default get options', () => {
7272
const initialData = { key: 'value' };
73-
// Use an instance with Gc turned on.
74-
return withEnsuredLruGcTestDb(persistence, async db => {
73+
// Use an instance with LRU GC.
74+
return withTestDb(persistence.toLruGc(), async db => {
7575
const docRef = doc(collection(db, 'test-collection'));
7676
await setDoc(docRef, initialData);
7777
return getDoc(docRef)
@@ -496,9 +496,10 @@ apiDescribe('GetOptions', persistence => {
496496
});
497497
});
498498

499-
// We need the deleted doc to stay in cache, so only run this with persistence.
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).
500501
// eslint-disable-next-line no-restricted-properties,
501-
(persistence ? it : it.skip)(
502+
(persistence.gc === 'lru' ? it : it.skip)(
502503
'get deleted doc while offline with source=cache',
503504
() => {
504505
return withTestDocAndInitialData(persistence, null, (docRef, db) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ apiDescribe('Index Configuration:', persistence => {
8080
it('bad JSON does not crash client', () => {
8181
return withTestDb(persistence, async db => {
8282
const action = (): Promise<void> => setIndexConfiguration(db, '{,}');
83-
if (persistence) {
83+
if (persistence.storage === 'indexeddb') {
8484
expect(action).to.throw(/Failed to parse JSON/);
8585
} else {
8686
// Silently do nothing. Parsing is not done and therefore no error is thrown.

0 commit comments

Comments
 (0)