Skip to content

Commit bb43ba4

Browse files
committed
Updated integration tests
1 parent 753fed7 commit bb43ba4

File tree

2 files changed

+31
-44
lines changed

2 files changed

+31
-44
lines changed

packages/database/test/exp/integration.test.ts

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import {
4545
DATABASE_URL,
4646
getFreshRepo,
4747
getRWRefs,
48-
waitFor
48+
waitFor,
49+
writeAndValidate
4950
} from '../helpers/util';
5051

5152
use(chaiAsPromised);
@@ -351,27 +352,6 @@ describe('Database@exp Tests', () => {
351352
goOnline(db);
352353
});
353354

354-
// TODO(mtewani): Remove this. This shouldn't work, right? A child level listener doesn't fulfill the full serverCache.
355-
// it.only('resolves get to serverCache when the database is offline and using a child-level listener', async () => {
356-
// const db = getDatabase(defaultApp);
357-
// const { readerRef, writerRef } = getRWRefs(db);
358-
// const toWrite = {
359-
// test: 'ghi'
360-
// };
361-
// const ec = EventAccumulatorFactory.waitsForExactCount(1);
362-
// await(
363-
// set(writerRef, toWrite)
364-
// );
365-
// onValue(child(readerRef, 'test'), (snapshot) => {
366-
// ec.addEvent(snapshot);
367-
// });
368-
// await ec.promise; // TODO: check value of this.
369-
// goOffline(db);
370-
// const result = await get(readerRef);
371-
// expect(result.val()).to.deep.eq(toWrite);
372-
// goOnline(db);
373-
// });
374-
375355
it('only fires listener once when calling get with limitTo', async () => {
376356
const db = getDatabase(defaultApp);
377357
const { readerRef, writerRef } = getRWRefs(db);
@@ -380,12 +360,7 @@ describe('Database@exp Tests', () => {
380360
child1: 'test1',
381361
child2: 'test2'
382362
};
383-
await set(writerRef, toWrite);
384-
onValue(readerRef, snapshot => {
385-
ec.addEvent(snapshot);
386-
});
387-
const [snap] = await ec.promise;
388-
expect(snap.val()).to.deep.eq(toWrite);
363+
writeAndValidate(writerRef, readerRef, toWrite, ec);
389364
const q = query(readerRef, limitToFirst(1));
390365
const snapshot = await get(q);
391366
const expected = {
@@ -402,14 +377,8 @@ describe('Database@exp Tests', () => {
402377
child2: 'test2',
403378
child3: 'test3'
404379
};
405-
await set(writerRef, toWrite);
406380
const ec = EventAccumulatorFactory.waitsForExactCount(1);
407-
onValue(readerRef, snapshot => {
408-
ec.addEvent(snapshot);
409-
});
410-
const events = await ec.promise;
411-
expect(events.length).to.eq(1);
412-
expect(events[0].val()).to.deep.eq(toWrite);
381+
writeAndValidate(writerRef, readerRef, toWrite, ec);
413382
const otherChildrenQuery = query(
414383
readerRef,
415384
orderByKey(),
@@ -437,14 +406,13 @@ describe('Database@exp Tests', () => {
437406
ec.addEvent(snapshot);
438407
});
439408
const events = await ec.promise;
440-
expect(events.length).to.eq(1); // TODO(mtewani): can get rid of this.
441409
const expected = { child1: 'test1' };
442410
expect(events[0].val()).to.deep.eq(expected);
443411
const snapshot = await get(readerRef);
444412
expect(snapshot.val()).to.deep.eq(toWrite);
445413
});
446414

447-
it('should test start with get with listener only fires once', async () => {
415+
it('should test startAt get with listener only fires once', async () => {
448416
const db = getDatabase(defaultApp);
449417
const { readerRef, writerRef } = getRWRefs(db);
450418
const expected = {
@@ -453,12 +421,7 @@ describe('Database@exp Tests', () => {
453421
child3: 'test3'
454422
};
455423
const ec = EventAccumulatorFactory.waitsForExactCount(1);
456-
await set(writerRef, expected); // TODO(mtewani): Can extract this out to something like writeAndValidate()
457-
onValue(readerRef, snapshot => {
458-
ec.addEvent(snapshot);
459-
});
460-
const [snap] = await ec.promise;
461-
expect(snap.val()).to.deep.eq(expected);
424+
writeAndValidate(writerRef, readerRef, expected, ec);
462425
const q = query(readerRef, orderByKey(), startAt('child2'));
463426
const snapshot = await get(q);
464427
const expectedQRes = {

packages/database/test/helpers/util.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@
1717

1818
import { FirebaseApp, initializeApp } from '@firebase/app';
1919
import { uuidv4 } from '@firebase/util';
20+
import { expect } from 'chai';
2021

21-
import { Database, getDatabase, ref } from '../../src';
22+
import {
23+
Database,
24+
DatabaseReference,
25+
getDatabase,
26+
onValue,
27+
ref,
28+
set
29+
} from '../../src';
2230
import { ConnectionTarget } from '../../src/api/test_access';
2331
import { Path } from '../../src/core/util/Path';
32+
import { EventAccumulator } from './EventAccumulator';
2433

2534
// eslint-disable-next-line @typescript-eslint/no-require-imports
2635
export const TEST_PROJECT = require('../../../../config/project.json');
@@ -105,3 +114,18 @@ export function getRWRefs(db: Database) {
105114
const writerRef = getFreshRepo(readerRef._path);
106115
return { readerRef, writerRef };
107116
}
117+
118+
// Validates that the ref was successfully written to.
119+
export async function writeAndValidate(
120+
writerRef: DatabaseReference,
121+
readerRef: DatabaseReference,
122+
toWrite: unknown,
123+
ec: EventAccumulator
124+
) {
125+
await set(writerRef, toWrite);
126+
onValue(readerRef, snapshot => {
127+
ec.addEvent(snapshot);
128+
});
129+
const [snap] = await ec.promise;
130+
expect(snap.val()).to.deep.eq(toWrite);
131+
}

0 commit comments

Comments
 (0)