Skip to content

Commit 84b6b77

Browse files
committed
test: improve utils perf
1 parent f5746f3 commit 84b6b77

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

tests/firestore/utils.spec.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
DocumentSnapshot,
3-
GeoPoint,
4-
getDoc,
5-
Timestamp,
6-
} from 'firebase/firestore'
1+
import { GeoPoint, getDoc, Timestamp } from 'firebase/firestore'
72
import { beforeEach, describe, expect, it } from 'vitest'
83
import {
94
extractRefs,
@@ -12,41 +7,38 @@ import {
127
import { setupFirestoreRefs } from '../utils'
138

149
describe('Firestore and Database utils', () => {
15-
const { collection, doc, setDoc, updateDoc, addDoc } = setupFirestoreRefs()
10+
const { collection, doc, addDoc } = setupFirestoreRefs()
1611

17-
const docRef = doc()
1812
const collectionRef = collection()
1913

20-
const docData = {
21-
n: 42,
22-
is: true,
23-
items: [{ text: 'foo' }],
24-
ref: docRef,
25-
}
26-
27-
let snapshot!: DocumentSnapshot<any>
14+
let docRef = doc()
2815
beforeEach(async () => {
29-
await setDoc(docRef, {
30-
// TODO: ref of collection
31-
// collection,
32-
data: {},
33-
index: 0,
34-
})
35-
const forSnapshot = await addDoc(collectionRef, { ...docData })
36-
snapshot = await getDoc(
37-
forSnapshot.withConverter(firestoreDefaultConverter)
38-
)
16+
// fresh document for each test
17+
docRef = doc()
3918
})
4019

41-
it('createSnapshot adds an id', () => {
42-
expect(snapshot.data().id).not.toBeFalsy()
20+
async function addDocToCollection() {
21+
const forSnapshot = await addDoc(collectionRef, {
22+
n: 42,
23+
is: true,
24+
items: [{ text: 'foo' }],
25+
ref: docRef,
26+
})
27+
return await getDoc(forSnapshot.withConverter(firestoreDefaultConverter))
28+
}
29+
30+
it('createSnapshot adds an id', async () => {
31+
const snapshot = await addDocToCollection()
32+
expect(snapshot.data()?.id).not.toBeFalsy()
4333
})
4434

45-
it('id is not enumerable', () => {
46-
expect(Object.keys(snapshot.data()).includes('id')).toBe(false)
35+
it('id is not enumerable', async () => {
36+
const snapshot = await addDocToCollection()
37+
expect(Object.keys(snapshot.data() ?? {}).includes('id')).toBe(false)
4738
})
4839

49-
it('contains all the data', () => {
40+
it('contains all the data', async () => {
41+
const snapshot = await addDocToCollection()
5042
expect(snapshot.data()).toEqual({
5143
n: 42,
5244
is: true,
@@ -56,7 +48,16 @@ describe('Firestore and Database utils', () => {
5648
})
5749

5850
it('extracts refs from documents', async () => {
59-
const [noRefsDoc, refs] = extractRefs(docData, undefined, {})
51+
const [noRefsDoc, refs] = extractRefs(
52+
{
53+
n: 42,
54+
is: true,
55+
items: [{ text: 'foo' }],
56+
ref: docRef,
57+
},
58+
undefined,
59+
{}
60+
)
6061
expect(noRefsDoc.ref).toBe(docRef.path)
6162
expect(refs).toEqual({
6263
ref: docRef,

tests/utils.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ import {
2626
addDoc,
2727
setDoc,
2828
updateDoc,
29-
PartialWithFieldValue,
30-
SetOptions,
31-
WithFieldValue,
3229
} from 'firebase/firestore'
3330
import {
3431
connectStorageEmulator,

0 commit comments

Comments
 (0)