Skip to content

Commit 28711d2

Browse files
committed
Refactored tests.
1 parent 3bd4cff commit 28711d2

File tree

4 files changed

+218
-177
lines changed

4 files changed

+218
-177
lines changed

src/components/app_tests/firestore/csr_test_runner.tsx

Lines changed: 5 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -17,141 +17,19 @@
1717
'use client'
1818

1919
import { useState, useEffect } from 'react'
20-
import { initializeApp } from 'firebase/app';
21-
import { testFirestore, initializeTestResults, SerializedFirestoreData, TestResults } from '@/lib/app_tests/firestore/test';
22-
import ResultsDisplay from './results_display';
2320
import {
24-
Bytes,
25-
DocumentSnapshot,
26-
documentSnapshotFromJSON,
27-
getFirestore,
28-
GeoPoint,
29-
Timestamp,
30-
onSnapshotResume,
31-
QuerySnapshot,
32-
querySnapshotFromJSON,
33-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
34-
VectorValue,
35-
vector
36-
} from 'firebase/firestore';
37-
import { firebaseConfig } from '@/lib/app_tests/firebase';
38-
import { OK } from '@/lib/app_tests/util';
39-
40-
function validateDocumentData(documentData): boolean {
41-
if (documentData !== undefined) {
42-
if (
43-
documentData.aBoolean && documentData.aBoolean === true &&
44-
documentData.aName && documentData.aName === "A name" &&
45-
documentData.anInteger && documentData.anInteger === 1234) {
46-
return true;
47-
}
48-
}
49-
return false;
50-
}
51-
async function runDeserializationTests(
52-
testResults: TestResults,
53-
serializedFirestoreData: SerializedFirestoreData
54-
): Promise<TestResults> {
55-
const firebase = initializeApp(firebaseConfig);
56-
const firestore = getFirestore(firebase);
57-
58-
// DocumentSnapshotTests
59-
if (serializedFirestoreData.documentSnapshotJson != null) {
60-
const snapshot = documentSnapshotFromJSON(firestore, serializedFirestoreData.documentSnapshotJson);
61-
const data = snapshot.data();
62-
if (validateDocumentData(data)) {
63-
testResults.clientSideDocumentSnapshotResult = OK;
64-
}
65-
66-
// onResume Test
67-
const bundleDocSnapshotPromise = new Promise<void>((resolve, reject) => {
68-
let completed: boolean = false;
69-
setTimeout(() => { if (!completed) reject(); }, 2000);
70-
const unsubscribe = onSnapshotResume(
71-
firestore,
72-
serializedFirestoreData.documentSnapshotJson!,
73-
(docSnapshot: DocumentSnapshot
74-
) => {
75-
if (docSnapshot.exists()) {
76-
if (validateDocumentData(docSnapshot.data())) {
77-
unsubscribe();
78-
testResults.clientSideDocumentSnapshotOnResumeResult = OK;
79-
completed = true;
80-
resolve();
81-
}
82-
}
83-
});
84-
});
85-
await bundleDocSnapshotPromise;
86-
}
87-
88-
// QuerySnapshotTests
89-
if (serializedFirestoreData.querySnapshotJson != null) {
90-
const snapshot = querySnapshotFromJSON(firestore, serializedFirestoreData.querySnapshotJson);
91-
if (snapshot.docs.length === 1 && validateDocumentData(snapshot.docs[0].data())) {
92-
testResults.clientSideQuerySnapshotResult = OK;
93-
}
94-
95-
// onResume test
96-
const bundleQuerySnapshotPromise = new Promise<void>((resolve, reject) => {
97-
let completed: boolean = false;
98-
setTimeout(() => { if (!completed) reject(); }, 2000);
99-
const unsubscribe = onSnapshotResume(
100-
firestore,
101-
serializedFirestoreData.querySnapshotJson!,
102-
(querySnapshot: QuerySnapshot
103-
) => {
104-
if (querySnapshot.docs.length === 1 && validateDocumentData(querySnapshot.docs[0].data())) {
105-
testResults.clientSideQuerySnapshotOnResumeResult = OK;
106-
unsubscribe();
107-
completed = true;
108-
resolve();
109-
}
110-
});
111-
});
112-
await bundleQuerySnapshotPromise;
113-
}
114-
115-
if(serializedFirestoreData.bytesJson !== null) {
116-
const bytes = Bytes.fromJSON(serializedFirestoreData.bytesJson);
117-
if(bytes.isEqual(Bytes.fromUint8Array(new Uint8Array([0, 1, 2, 3, 4, 5])))) {
118-
testResults.clientSideDeserializedBytesResult = OK;
119-
}
120-
}
121-
122-
if(serializedFirestoreData.geoPointJson !== null) {
123-
const geoPoint = GeoPoint.fromJSON(serializedFirestoreData.geoPointJson);
124-
if(geoPoint.latitude === 1 && geoPoint.longitude === 2) {
125-
testResults.clientSideDeserializedGeoPointResult = OK;
126-
}
127-
}
128-
129-
if(serializedFirestoreData.timestampJson !== null) {
130-
const timestamp = Timestamp.fromJSON(serializedFirestoreData.timestampJson);
131-
if(timestamp.seconds === 123 && timestamp.nanoseconds === 456) {
132-
testResults.clientSideDeserializedTimestampResult = OK;
133-
}
134-
}
135-
136-
if(serializedFirestoreData.vectorValueJson !== null) {
137-
const num: number[] = [1, 2, 3];
138-
const deserializedVectorValue = VectorValue.fromJSON(serializedFirestoreData.vectorValueJson);
139-
const controlVectorValue = vector(num);
140-
if(deserializedVectorValue.isEqual(controlVectorValue)) {
141-
testResults.clientSideDeserializedVectorValueResult = OK;
142-
}
143-
}
144-
145-
return testResults;
146-
}
21+
initializeTestResults,
22+
testSerializedFirestoreData,
23+
testFirestore } from '@/lib/app_tests/firestore/test';
24+
import ResultsDisplay from './results_display';
14725

14826
export default function CsrTestRunner(props) {
14927
const [testStatus, setTestStatus] = useState<string>("running...");
15028
const [testResults, setTestResults] = useState(initializeTestResults());
15129
useEffect(() => {
15230
const asyncTest = async () => {
15331
let testResults = await testFirestore(/* isServer= */ false);
154-
testResults = await runDeserializationTests(testResults, props.serializedFirestoreData);
32+
testResults = await testSerializedFirestoreData(testResults, props.serializedFirestoreData);
15533
setTestResults(testResults);
15634
setTestStatus("Complete!");
15735
}

src/components/app_tests/firestore/results_display.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ export default function ResultsDisplay({ statusString, testResults }) {
4040
<h4 title="deleteAppResult">deleteAppResult: {testResults.deleteAppResult}</h4>
4141

4242
<h3> CSR-side deserialization tests </h3>
43-
<h4 title="clientSideDocumentSnapshotResult">clientSideDocumentSnapshotResult: {testResults.clientSideDocumentSnapshotResult}</h4>
44-
<h4 title="clientSideDocumentSnapshotOnResumeResult">clientSideDocumentSnapshotOnResumeResult: {testResults.clientSideDocumentSnapshotOnResumeResult}</h4>
45-
<h4 title="clientSideQuerySnapshotResult">clientSideQuerySnapshotResult: {testResults.clientSideQuerySnapshotResult}</h4>
46-
<h4 title="clientSideQuerySnapshotOnResumeResult">clientSideQuerySnapshotOnResumeResult: {testResults.clientSideQuerySnapshotOnResumeResult}</h4>
47-
<h4 title="clientSideDeserializedBytesResult">clientSideDeserializedBytesResult: {testResults.clientSideDeserializedBytesResult}</h4>
48-
<h4 title="clientSideDeserializedGeoPointResult">clientSideDeserializedGeoPointResult: {testResults.clientSideDeserializedGeoPointResult}</h4>
49-
<h4 title="clientSideDeserializedTimestampResult">clientSideDeserializedTimestampResult: {testResults.clientSideDeserializedTimestampResult}</h4>
50-
<h4 title="clientSideDeserializedVectorValueResult">clientSideDeserializedVectorValueResult: {testResults.clientSideDeserializedVectorValueResult}</h4>
51-
43+
<h4 title="csrDocumentSnapshotResult">csrDocumentSnapshotResult: {testResults.csrDocumentSnapshotResult}</h4>
44+
<h4 title="csrDocumentSnapshotOnResumeResult">csrDocumentSnapshotOnResumeResult: {testResults.csrDocumentSnapshotOnResumeResult}</h4>
45+
<h4 title="csrQuerySnapshotResult">csrQuerySnapshotResult: {testResults.csrQuerySnapshotResult}</h4>
46+
<h4 title="csrQuerySnapshotOnResumeResult">csrQuerySnapshotOnResumeResult: {testResults.csrQuerySnapshotOnResumeResult}</h4>
47+
<h4 title="csrDeserializedBytesResult">csrDeserializedBytesResult: {testResults.csrDeserializedBytesResult}</h4>
48+
<h4 title="csrDeserializedGeoPointResult">csrDeserializedGeoPointResult: {testResults.csrDeserializedGeoPointResult}</h4>
49+
<h4 title="csrDeserializedTimestampResult">csrDeserializedTimestampResult: {testResults.csrDeserializedTimestampResult}</h4>
50+
<h4 title="csrDeserializedVectorValueResult">csrDeserializedVectorValueResult: {testResults.csrDeserializedVectorValueResult}</h4>
51+
5252
<p />
5353
<Link href="/">Back to test index</Link>
5454
</>

0 commit comments

Comments
 (0)