Skip to content

Commit

Permalink
test(firestore): use getDoc in all e2e tests that use a document refe…
Browse files Browse the repository at this point in the history
…rence
  • Loading branch information
dhenneke authored and mikehardy committed May 8, 2024
1 parent e90782b commit 5d62408
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 195 deletions.
4 changes: 2 additions & 2 deletions packages/firestore/e2e/CollectionReference/add.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ describe('firestore.collection().add()', function () {
});

it('adds a new document', async function () {
const { getFirestore, collection, addDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, collection, addDoc, getDoc, deleteDoc } = firestoreModular;

const data = { foo: 'bar' };
const docRef = await addDoc(collection(getFirestore(), COLLECTION), data);
should.equal(docRef.constructor.name, 'FirestoreDocumentReference');
const docSnap = await getDocs(docRef);
const docSnap = await getDoc(docRef);
docSnap.data().should.eql(jet.contextify(data));
docSnap.exists.should.eql(true);
await deleteDoc(docRef);
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/e2e/DocumentReference/delete.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ describe('firestore.doc().delete()', function () {

describe('modular', function () {
it('deletes a document', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;

const ref = doc(getFirestore(), `${COLLECTION}/deleteme`);
await setDoc(ref, { foo: 'bar' });
const snapshot1 = await getDocs(ref);
const snapshot1 = await getDoc(ref);
snapshot1.id.should.equal('deleteme');
snapshot1.exists.should.equal(true);
await deleteDoc(ref);
const snapshot2 = await getDocs(ref);
const snapshot2 = await getDoc(ref);
snapshot2.id.should.equal('deleteme');
snapshot2.exists.should.equal(false);
});
Expand Down
12 changes: 6 additions & 6 deletions packages/firestore/e2e/DocumentReference/get.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,35 @@ describe('firestore.doc().get()', function () {

describe('modular', function () {
it('gets data from default source', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;

const ref = doc(getFirestore(), `${COLLECTION}/get`);
const data = { foo: 'bar', bar: 123 };
await setDoc(ref, data);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);
snapshot.data().should.eql(jet.contextify(data));
await deleteDoc(ref);
});

it('gets data from the server', async function () {
const { getFirestore, doc, setDoc, getDocsFromServer, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDocFromServer, deleteDoc } = firestoreModular;

const ref = doc(getFirestore(), `${COLLECTION}/get`);
const data = { foo: 'bar', bar: 123 };
await setDoc(ref, data);
const snapshot = await getDocsFromServer(ref);
const snapshot = await getDocFromServer(ref);
snapshot.data().should.eql(jet.contextify(data));
snapshot.metadata.fromCache.should.equal(false);
await deleteDoc(ref);
});

it('gets data from cache', async function () {
const { getFirestore, doc, setDoc, getDocsFromCache, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDocFromCache, deleteDoc } = firestoreModular;

const ref = doc(getFirestore(), `${COLLECTION}/get`);
const data = { foo: 'bar', bar: 123 };
await setDoc(ref, data);
const snapshot = await getDocsFromCache(ref);
const snapshot = await getDocFromCache(ref);
snapshot.data().should.eql(jet.contextify(data));
snapshot.metadata.fromCache.should.equal(true);
await deleteDoc(ref);
Expand Down
26 changes: 13 additions & 13 deletions packages/firestore/e2e/DocumentReference/set.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,49 +342,49 @@ describe('firestore.doc().set()', function () {
});

it('sets new data', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/set`);
const data1 = { foo: 'bar' };
const data2 = { foo: 'baz', bar: 123 };
await setDoc(ref, data1);
const snapshot1 = await getDocs(ref);
const snapshot1 = await getDoc(ref);
snapshot1.data().should.eql(jet.contextify(data1));
await setDoc(ref, data2);
const snapshot2 = await getDocs(ref);
const snapshot2 = await getDoc(ref);
snapshot2.data().should.eql(jet.contextify(data2));
await deleteDoc(ref);
});

it('merges all fields', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/merge`);
const data1 = { foo: 'bar' };
const data2 = { bar: 'baz' };
const merged = { ...data1, ...data2 };
await setDoc(ref, data1);
const snapshot1 = await getDocs(ref);
const snapshot1 = await getDoc(ref);
snapshot1.data().should.eql(jet.contextify(data1));
await setDoc(ref, data2, {
merge: true,
});
const snapshot2 = await getDocs(ref);
const snapshot2 = await getDoc(ref);
snapshot2.data().should.eql(jet.contextify(merged));
await deleteDoc(ref);
});

it('merges specific fields', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/merge`);
const data1 = { foo: '123', bar: 123, baz: '456' };
const data2 = { foo: '234', bar: 234, baz: '678' };
const merged = { foo: data1.foo, bar: data2.bar, baz: data2.baz };
await setDoc(ref, data1);
const snapshot1 = await getDocs(ref);
const snapshot1 = await getDoc(ref);
snapshot1.data().should.eql(jet.contextify(data1));
await setDoc(ref, data2, {
mergeFields: ['bar', new firebase.firestore.FieldPath('baz')],
});
const snapshot2 = await getDocs(ref);
const snapshot2 = await getDoc(ref);
snapshot2.data().should.eql(jet.contextify(merged));
await deleteDoc(ref);
});
Expand Down Expand Up @@ -428,7 +428,7 @@ describe('firestore.doc().set()', function () {
});

it('filters out undefined properties when setting enabled', async function () {
const { getFirestore, initializeFirestore, doc, setDoc, getDocs } = firestoreModular;
const { getFirestore, initializeFirestore, doc, setDoc, getDoc } = firestoreModular;
const db = getFirestore();
initializeFirestore(db.app, { ignoreUndefinedProperties: true });

Expand All @@ -438,7 +438,7 @@ describe('firestore.doc().set()', function () {
field2: undefined,
});

const snap = await getDocs(docRef);
const snap = await getDoc(docRef);
const snapData = snap.data();
if (!snapData) {
return Promise.reject(new Error('Snapshot not saved'));
Expand All @@ -449,7 +449,7 @@ describe('firestore.doc().set()', function () {
});

it('filters out nested undefined properties when setting enabled', async function () {
const { getFirestore, initializeFirestore, doc, setDoc, getDocs } = firestoreModular;
const { getFirestore, initializeFirestore, doc, setDoc, getDoc } = firestoreModular;
const db = getFirestore();
initializeFirestore(db.app, { ignoreUndefinedProperties: true });

Expand All @@ -467,7 +467,7 @@ describe('firestore.doc().set()', function () {
],
});

const snap = await getDocs(docRef);
const snap = await getDoc(docRef);
const snapData = snap.data();
if (!snapData) {
return Promise.reject(new Error('Snapshot not saved'));
Expand Down
12 changes: 6 additions & 6 deletions packages/firestore/e2e/DocumentReference/update.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,27 @@ describe('firestore.doc().update()', function () {
});

it('updates data with an object value', async function () {
const { getFirestore, doc, setDoc, getDocs, updateDoc, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, updateDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/update-obj`);
const value = Date.now();
const data1 = { foo: value };
const data2 = { foo: 'bar' };
await setDoc(ref, data1);
const snapshot1 = await getDocs(ref);
const snapshot1 = await getDoc(ref);
snapshot1.data().should.eql(jet.contextify(data1));
await updateDoc(ref, data2);
const snapshot2 = await getDocs(ref);
const snapshot2 = await getDoc(ref);
snapshot2.data().should.eql(jet.contextify(data2));
await deleteDoc(ref);
});

it('updates data with an key/value pairs', async function () {
const { getFirestore, doc, setDoc, getDocs, updateDoc, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, updateDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/update-obj`);
const value = Date.now();
const data1 = { foo: value, bar: value };
await setDoc(ref, data1);
const snapshot1 = await getDocs(ref);
const snapshot1 = await getDoc(ref);
snapshot1.data().should.eql(jet.contextify(data1));

await updateDoc(ref, 'foo', 'bar', 'bar', 'baz', 'foo1', 'bar1');
Expand All @@ -156,7 +156,7 @@ describe('firestore.doc().update()', function () {
bar: 'baz',
foo1: 'bar1',
};
const snapshot2 = await getDocs(ref);
const snapshot2 = await getDoc(ref);
snapshot2.data().should.eql(jet.contextify(expected));
await deleteDoc(ref);
});
Expand Down
16 changes: 8 additions & 8 deletions packages/firestore/e2e/DocumentSnapshot/data.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,28 @@ describe('firestore().doc() -> snapshot.data()', function () {

describe('modular', function () {
it('returns undefined if documet does not exist', async function () {
const { getFirestore, doc, getDocs } = firestoreModular;
const { getFirestore, doc, getDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/idonotexist`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);
should.equal(snapshot.data(), undefined);
});

it('returns an object if exists', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/getData`);
const data = { foo: 'bar' };
await setDoc(ref, data);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);
snapshot.data().should.eql(jet.contextify(data));
await deleteDoc(ref);
});

it('returns an object when document is empty', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/getData`);
const data = {};
await setDoc(ref, data);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);
snapshot.data().should.eql(jet.contextify(data));
await deleteDoc(ref);
});
Expand All @@ -176,7 +176,7 @@ describe('firestore().doc() -> snapshot.data()', function () {
// });

it('handles all data types', async function () {
const { getFirestore, doc, setDoc, getDocs, deleteDoc } = firestoreModular;
const { getFirestore, doc, setDoc, getDoc, deleteDoc } = firestoreModular;
const types = {
string: '123456',
stringEmpty: '',
Expand All @@ -203,7 +203,7 @@ describe('firestore().doc() -> snapshot.data()', function () {

const ref = doc(getFirestore(), `${COLLECTION}/types`);
await setDoc(ref, types);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);
const data = snapshot.data();

// String
Expand Down
32 changes: 16 additions & 16 deletions packages/firestore/e2e/DocumentSnapshot/get.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ describe('firestore().doc() -> snapshot.get()', function () {

describe('modular', function () {
it('throws if invalid fieldPath argument', async function () {
const { getFirestore, doc, getDocs } = firestoreModular;
const { getFirestore, doc, getDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

try {
snapshot.get(123);
Expand All @@ -182,9 +182,9 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('throws if fieldPath is an empty string', async function () {
const { getFirestore, doc, getDocs } = firestoreModular;
const { getFirestore, doc, getDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

try {
snapshot.get('');
Expand All @@ -196,9 +196,9 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('throws if fieldPath starts with a period (.)', async function () {
const { getFirestore, doc, getDocs } = firestoreModular;
const { getFirestore, doc, getDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

try {
snapshot.get('.foo');
Expand All @@ -210,9 +210,9 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('throws if fieldPath ends with a period (.)', async function () {
const { getFirestore, doc, getDocs } = firestoreModular;
const { getFirestore, doc, getDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

try {
snapshot.get('foo.');
Expand All @@ -224,9 +224,9 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('throws if fieldPath contains ..', async function () {
const { getFirestore, doc, getDocs } = firestoreModular;
const { getFirestore, doc, getDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

try {
snapshot.get('foo..bar');
Expand All @@ -238,9 +238,9 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('returns undefined if the data does not exist', async function () {
const { getFirestore, doc, getDocs, FieldPath } = firestoreModular;
const { getFirestore, doc, getDoc, FieldPath } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

const val1 = snapshot.get('foo');
const val2 = snapshot.get('foo.bar');
Expand All @@ -256,7 +256,7 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('returns the correct data with string fieldPath', async function () {
const { getFirestore, doc, getDocs, setDoc, deleteDoc } = firestoreModular;
const { getFirestore, doc, getDoc, setDoc, deleteDoc } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const types = {
string: '12345',
Expand All @@ -270,7 +270,7 @@ describe('firestore().doc() -> snapshot.get()', function () {
};

await setDoc(ref, types);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

const string1 = snapshot.get('string');
const string2 = snapshot.get('map.string');
Expand All @@ -287,7 +287,7 @@ describe('firestore().doc() -> snapshot.get()', function () {
});

it('returns the correct data with FieldPath', async function () {
const { getFirestore, doc, getDocs, setDoc, deleteDoc, FieldPath } = firestoreModular;
const { getFirestore, doc, getDoc, setDoc, deleteDoc, FieldPath } = firestoreModular;
const ref = doc(getFirestore(), `${COLLECTION}/foo`);
const types = {
string: '12345',
Expand All @@ -301,7 +301,7 @@ describe('firestore().doc() -> snapshot.get()', function () {
};

await setDoc(ref, types);
const snapshot = await getDocs(ref);
const snapshot = await getDoc(ref);

const string1 = snapshot.get(new FieldPath('string'));
const string2 = snapshot.get(new FieldPath('map', 'string'));
Expand Down
Loading

0 comments on commit 5d62408

Please sign in to comment.