|
17 | 17 | 'use client' |
18 | 18 |
|
19 | 19 | 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'; |
23 | 20 | 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'; |
147 | 25 |
|
148 | 26 | export default function CsrTestRunner(props) { |
149 | 27 | const [testStatus, setTestStatus] = useState<string>("running..."); |
150 | 28 | const [testResults, setTestResults] = useState(initializeTestResults()); |
151 | 29 | useEffect(() => { |
152 | 30 | const asyncTest = async () => { |
153 | 31 | let testResults = await testFirestore(/* isServer= */ false); |
154 | | - testResults = await runDeserializationTests(testResults, props.serializedFirestoreData); |
| 32 | + testResults = await testSerializedFirestoreData(testResults, props.serializedFirestoreData); |
155 | 33 | setTestResults(testResults); |
156 | 34 | setTestStatus("Complete!"); |
157 | 35 | } |
|
0 commit comments