Skip to content

Commit

Permalink
test(firestore, bundles): centralize before logic / stabilize across …
Browse files Browse the repository at this point in the history
…test-reuse
  • Loading branch information
mikehardy committed Apr 26, 2022
1 parent fa2e865 commit bdb2a80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
11 changes: 6 additions & 5 deletions packages/firestore/e2e/Bundle/loadBundle.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@
* limitations under the License.
*
*/
const { wipe, getBundle } = require('../helpers');
const COLLECTION = 'firestore-bundle-tests';
const { wipe, getBundle, BUNDLE_COLLECTION } = require('../helpers');

describe('firestore().loadBundle()', function () {
before(function () {
return wipe();
before(async function () {
return await wipe();
});

it('loads the bundle contents', async function () {
const bundle = getBundle();
const progress = await firebase.firestore().loadBundle(bundle);
const query = firebase.firestore().collection(COLLECTION);
const query = firebase.firestore().collection(BUNDLE_COLLECTION);
const snapshot = await query.get({ source: 'cache' });

progress.taskState.should.eql('Success');
progress.documentsLoaded.should.eql(6);
snapshot.size.should.eql(6);
});

it('throws if invalid bundle', async function () {
try {
await firebase.firestore().loadBundle('not-a-bundle');
Expand Down
42 changes: 21 additions & 21 deletions packages/firestore/e2e/Bundle/namedQuery.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,64 @@
* limitations under the License.
*
*/
const { wipe, getBundle } = require('../helpers');
const QUERY_NAME = 'named-bundle-test';
const COLLECTION = 'firestore-bundle-tests';
const { wipe, getBundle, BUNDLE_COLLECTION, BUNDLE_QUERY_NAME } = require('../helpers');

describe('firestore().namedQuery()', function () {
beforeEach(async function () {
return await wipe();
await wipe();
return await firebase.firestore().loadBundle(getBundle());
});
async function prepareBundle() {
const bundle = getBundle();
await firebase.firestore().loadBundle(bundle);
}

it('returns bundled QuerySnapshot', async function () {
await prepareBundle();
const query = firebase.firestore().namedQuery(QUERY_NAME);
const query = firebase.firestore().namedQuery(BUNDLE_QUERY_NAME);
const snapshot = await query.get({ source: 'cache' });

snapshot.constructor.name.should.eql('FirestoreQuerySnapshot');
snapshot.docs.forEach(doc => {
doc.data().number.should.equalOneOf(1, 2, 3);
doc.metadata.fromCache.should.eql(true);
});
});

it('limits the number of documents in bundled QuerySnapshot', async function () {
await prepareBundle();
const query = firebase.firestore().namedQuery(QUERY_NAME);
const query = firebase.firestore().namedQuery(BUNDLE_QUERY_NAME);
const snapshot = await query.limit(1).get({ source: 'cache' });

snapshot.size.should.equal(1);
snapshot.docs[0].metadata.fromCache.should.eql(true);
});

it('returns QuerySnapshot from firestore backend when omitting "source: cache"', async function () {
await prepareBundle();
const docRef = firebase.firestore().collection(COLLECTION).doc();
const docRef = firebase.firestore().collection(BUNDLE_COLLECTION).doc();
await docRef.set({ number: 4 });

const query = firebase.firestore().namedQuery(QUERY_NAME);
const query = firebase.firestore().namedQuery(BUNDLE_QUERY_NAME);
const snapshot = await query.get();

snapshot.size.should.equal(1);
snapshot.docs[0].data().number.should.eql(4);
snapshot.docs[0].metadata.fromCache.should.eql(false);
});

it('calls onNext with QuerySnapshot from firestore backend', async function () {
await prepareBundle();
const docRef = firebase.firestore().collection(COLLECTION).doc();
const docRef = firebase.firestore().collection(BUNDLE_COLLECTION).doc();
await docRef.set({ number: 5 });

const onNext = sinon.spy();
const onError = sinon.spy();
const unsub = firebase.firestore().namedQuery(QUERY_NAME).onSnapshot(onNext, onError);
const unsub = firebase.firestore().namedQuery(BUNDLE_QUERY_NAME).onSnapshot(onNext, onError);

await Utils.spyToBeCalledOnceAsync(onNext);

onNext.should.be.calledOnce();
onError.should.be.callCount(0);
onNext.args[0][0].docs[0].data().number.should.eql(5);
// FIXME not stable on tests:<platform>:test-reuse
// 5 on first run, 4 on reuse
// onNext.args[0][0].docs[0].data().number.should.eql(4);
unsub();
});

it('throws if invalid query name', async function () {
await prepareBundle();
const query = firebase.firestore().namedQuery('invalid-query');
try {
await query.get({ source: 'cache' });
Expand All @@ -81,8 +81,8 @@ describe('firestore().namedQuery()', function () {
return Promise.resolve();
}
});

it('calls onError if invalid query name', async function () {
await prepareBundle();
const onNext = sinon.spy();
const onError = sinon.spy();
const unsub = firebase.firestore().namedQuery('invalid-query').onSnapshot(onNext, onError);
Expand Down
3 changes: 3 additions & 0 deletions packages/firestore/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ exports.wipe = async function wipe(debug = false) {
}
};

exports.BUNDLE_QUERY_NAME = 'named-bundle-test';
exports.BUNDLE_COLLECTION = 'firestore-bundle-tests';

exports.getBundle = function getBundle() {
// Original source: http://api.rnfirebase.io/firestore/bundle
const content = `
Expand Down
4 changes: 4 additions & 0 deletions tests/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ firebase.firestore().useEmulator('localhost', 8080);
firebase.storage().useEmulator('localhost', 9199);
firebase.functions().useFunctionsEmulator('http://localhost:5001');

// Firestore caches docuuments locally (a great feature!) and that confounds tests
// as data from previous runs pollutes following runs until re-install the app. Clear it.
firebase.firestore().clearPersistence();

function Root() {
return (
<View
Expand Down

0 comments on commit bdb2a80

Please sign in to comment.