Skip to content

Commit

Permalink
Refactor firestore test more
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Dec 12, 2024
1 parent f3fe29c commit 52eee74
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 218 deletions.
2 changes: 1 addition & 1 deletion test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/
import {default as config, resolvedAuthEmulatorPort} from './config';
import {initializeApp, FirebaseApp, deleteApp} from 'firebase/app';
import {initializeApp, FirebaseApp} from 'firebase/app';
import {getAuth, Auth, connectAuthEmulator, signInAnonymously} from 'firebase/auth';
import {authState} from '../dist/auth';
import {skip, take} from 'rxjs/operators';
Expand Down
16 changes: 8 additions & 8 deletions test/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from "cross-fetch";
import fetch from 'cross-fetch';

export default {
apiKey: 'AIzaSyCD1LqWoxivr0hu7YJ_xF6WyAT4_l-Aw0I',
Expand All @@ -13,14 +13,14 @@ export default {

const resolvedEmulatorHubResponse = (async () => {
await new Promise((resolve) => setTimeout(resolve, 1_000));
if (!process.env.FIREBASE_EMULATOR_HUB) throw "$FIREBASE_EMULATOR_HUB not found";
if (!process.env.FIREBASE_EMULATOR_HUB) throw new Error('$FIREBASE_EMULATOR_HUB not found');
const response = await fetch(`http://${process.env.FIREBASE_EMULATOR_HUB}/emulators`);
if (!response.ok) throw "Unable to fetch emulator hub REST api.";
if (!response.ok) throw new Error('Unable to fetch emulator hub REST api.');
return await response.json();
})();

export const resolvedAuthEmulatorPort = resolvedEmulatorHubResponse.then(it => it.auth.port);
export const resolvedDatabaseEmulatorPort = resolvedEmulatorHubResponse.then(it => it.database.port);
export const resolvedFirestoreEmulatorPort = resolvedEmulatorHubResponse.then(it => it.firestore.port);
export const resolvedStorageEmulatorPort = resolvedEmulatorHubResponse.then(it => it.storage.port);
export const resolvedFunctionsEmulatorPort = resolvedEmulatorHubResponse.then(it => it.functions.port);
export const resolvedAuthEmulatorPort = resolvedEmulatorHubResponse.then((it) => it.auth.port);
export const resolvedDatabaseEmulatorPort = resolvedEmulatorHubResponse.then((it) => it.database.port);
export const resolvedFirestoreEmulatorPort = resolvedEmulatorHubResponse.then((it) => it.firestore.port);
export const resolvedStorageEmulatorPort = resolvedEmulatorHubResponse.then((it) => it.storage.port);
export const resolvedFunctionsEmulatorPort = resolvedEmulatorHubResponse.then((it) => it.functions.port);
24 changes: 11 additions & 13 deletions test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,8 @@ describe('RxFire Database', () => {
const itemsObj = batch(items);

describe('events', () => {

// TODO figure this out
describe("FLAKY", () => {

describe('FLAKY', () => {
jest.retryTimes(2, {logErrorsBeforeRetry: true});

/**
Expand All @@ -296,17 +294,16 @@ describe('RxFire Database', () => {
it('should stream value at first', (done) => {
const someRef = builtRef(rando());
const obs = list(someRef);
set(someRef, itemsObj).then((it) => {
set(someRef, itemsObj).then(() => {
obs
.pipe(take(1))
.subscribe((changes) => {
const data = changes.map((change) => change.snapshot.val());
expect(data).toEqual(items);
done();
});
.pipe(take(1))
.subscribe((changes) => {
const data = changes.map((change) => change.snapshot.val());
expect(data).toEqual(items);
done();
});
}, done.fail);
});

});

/**
Expand Down Expand Up @@ -458,7 +455,7 @@ describe('RxFire Database', () => {
it('should process a new child_moved event', (done) => {
const aref = builtRef(rando());
list(aref, {events: [ListenEvent.added, ListenEvent.moved]})
.pipe(skip(2))
.pipe(skip(2), take(1))
.subscribe((changes) => {
const data = changes.map((change) => change.snapshot.val());
// We moved the first item to the last item, so we check that
Expand Down Expand Up @@ -598,6 +595,7 @@ describe('RxFire Database', () => {
const aref = builtRef(rando());
let count = 0;
const sub = listVal(aref)
.pipe(take(2))
.subscribe((data) => {
if (count == 0) {
expect(data).toEqual([]);
Expand Down Expand Up @@ -728,7 +726,7 @@ describe('RxFire Database', () => {
it('objectVal should behave the same as snap.val() when an object doesn\'t exist', (done) => {
const nonExistentRef = builtRef(rando());
set(nonExistentRef, null);
const obs = objectVal(nonExistentRef);
const obs = objectVal(nonExistentRef).pipe(take(1));

get(nonExistentRef).then((snap) => {
obs.subscribe((val) => {
Expand Down
19 changes: 10 additions & 9 deletions test/firestore-lite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import {
collectionCountSnap,
collectionCount,
} from '../dist/firestore/lite';
import {map} from 'rxjs/operators';
import {map, take} from 'rxjs/operators';
import {default as TEST_PROJECT, resolvedFirestoreEmulatorPort} from './config';
import {doc as firestoreDoc, getDocs, collection as firestoreCollection, getDoc, Firestore as FirebaseFirestore, CollectionReference, getFirestore, DocumentReference, connectFirestoreEmulator, doc, setDoc, collection as baseCollection, QueryDocumentSnapshot, addDoc} from 'firebase/firestore/lite';
import {initializeApp, deleteApp, FirebaseApp} from 'firebase/app';
import {initializeApp, FirebaseApp} from 'firebase/app';

const createId = (): string => Math.random().toString(36).substring(5);

Expand Down Expand Up @@ -95,7 +95,7 @@ describe('RxFire firestore/lite', () => {
const {colRef, expectedNames} = await seedTest(firestore);

collection(colRef)
.pipe(map((docs) => docs.map((doc) => doc.data().name)))
.pipe(map((docs) => docs.map((doc) => doc.data().name)), take(1))
.subscribe((names) => {
expect(names).toEqual(expectedNames);
});
Expand Down Expand Up @@ -131,6 +131,7 @@ describe('RxFire firestore/lite', () => {
}

collection(Folk.collection)
.pipe(take(1))
.subscribe((docs) => {
const names = docs.map((doc) => doc.data()?.name);
const classes = docs.map((doc) => doc.data()?.constructor?.name);
Expand All @@ -150,7 +151,7 @@ describe('RxFire firestore/lite', () => {
// const unwrapped = collection(colRef).pipe(unwrap('userId'));
const unwrapped = collectionData(colRef, {idField: 'userId'});

unwrapped.subscribe((val) => {
unwrapped.pipe(take(1)).subscribe((val) => {
const expectedDoc = {
name: 'David',
userId: 'david',
Expand All @@ -166,7 +167,7 @@ describe('RxFire firestore/lite', () => {
// const unwrapped = doc(davidDoc).pipe(unwrap('UID'));
const unwrapped = docData(davidDoc, {idField: 'UID'});

unwrapped.subscribe((val) => {
unwrapped.pipe(take(1)).subscribe((val) => {
const expectedDoc = {
name: 'David',
UID: 'david',
Expand All @@ -193,7 +194,7 @@ describe('RxFire firestore/lite', () => {
const unwrapped = docData(nonExistentDoc);

getDoc(nonExistentDoc).then((snap) => {
unwrapped.subscribe((val) => {
unwrapped.pipe(take(1)).subscribe((val) => {
expect(val).toEqual(snap.data());
});
});
Expand All @@ -207,7 +208,7 @@ describe('RxFire firestore/lite', () => {
const unwrapped = collectionData(nonExistentCollection);

getDocs(nonExistentCollection).then((snap) => {
unwrapped.subscribe((val) => {
unwrapped.pipe(take(1)).subscribe((val) => {
expect(val).toEqual(snap.docs);
done();
});
Expand All @@ -224,7 +225,7 @@ describe('RxFire firestore/lite', () => {
];
await Promise.all(entries);

collectionCountSnap(colRef).subscribe((snap) => {
collectionCountSnap(colRef).pipe(take(1)).subscribe((snap) => {
expect(snap.data().count).toEqual(entries.length);
});
});
Expand All @@ -240,7 +241,7 @@ describe('RxFire firestore/lite', () => {
];
await Promise.all(entries);

collectionCount(colRef).subscribe((count) => {
collectionCount(colRef).pipe(take(1)).subscribe((count) => {
expect(count).toEqual(entries.length);
});
});
Expand Down
Loading

0 comments on commit 52eee74

Please sign in to comment.