|
17 | 17 | 'use client' |
18 | 18 |
|
19 | 19 | import { useState, useEffect } from 'react' |
20 | | -import { testFirestore, initializeTestResults } from '@/lib/app_tests/firestore/test'; |
| 20 | +import { deleteApp, initializeApp } from 'firebase/app'; |
| 21 | +import { testFirestore, initializeTestResults, SerializedFirestoreData, TestResults } from '@/lib/app_tests/firestore/test'; |
21 | 22 | import ResultsDisplay from './results_display'; |
| 23 | +import { |
| 24 | + DocumentSnapshot, |
| 25 | + getFirestore, |
| 26 | + onSnapshotResume, |
| 27 | + QuerySnapshot, |
| 28 | + documentSnapshotFromJSON, |
| 29 | + querySnapshotFromJSON |
| 30 | +} from 'firebase/firestore'; |
| 31 | +import { firebaseConfig } from '@/lib/app_tests/firebase'; |
| 32 | +import { OK } from '@/lib/app_tests/util'; |
22 | 33 |
|
23 | | -export default function CsrTestRunner() { |
| 34 | +function validateDocumentData(documentData): boolean { |
| 35 | + if (documentData !== undefined) { |
| 36 | + if ( |
| 37 | + documentData.aBoolean && documentData.aBoolean === true && |
| 38 | + documentData.aName && documentData.aName === "A name" && |
| 39 | + documentData.anInteger && documentData.anInteger === 1234) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | +} |
| 45 | +async function runDeserializationTests( |
| 46 | + testResults: TestResults, |
| 47 | + serializedFirestoreData: SerializedFirestoreData |
| 48 | +): Promise<TestResults> { |
| 49 | + const firebase = initializeApp(firebaseConfig); |
| 50 | + const firestore = getFirestore(firebase); |
| 51 | + |
| 52 | + // DocumentSnapshotTests |
| 53 | + if (serializedFirestoreData.documentSnapshotJson != null) { |
| 54 | + const snapshot = documentSnapshotFromJSON(firestore, serializedFirestoreData.documentSnapshotJson); |
| 55 | + const data = snapshot.data(); |
| 56 | + if (validateDocumentData(data)) { |
| 57 | + testResults.clientSideDocumentSnapshotResult = OK; |
| 58 | + } |
| 59 | + |
| 60 | + // onResume Test |
| 61 | + const bundleDocSnapshotPromise = new Promise<void>((resolve, reject) => { |
| 62 | + let completed: boolean = false; |
| 63 | + setTimeout(() => { if (!completed) reject(); }, 2000); |
| 64 | + const unsubscribe = onSnapshotResume( |
| 65 | + firestore, |
| 66 | + serializedFirestoreData.documentSnapshotJson!, |
| 67 | + (docSnapshot: DocumentSnapshot |
| 68 | + ) => { |
| 69 | + if (docSnapshot.exists()) { |
| 70 | + const docData = docSnapshot.data(); |
| 71 | + if (validateDocumentData(docSnapshot.data())) { |
| 72 | + unsubscribe(); |
| 73 | + testResults.clientSideDocumentSnapshotOnResumeResult = OK; |
| 74 | + completed = true; |
| 75 | + resolve(); |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + await bundleDocSnapshotPromise; |
| 81 | + } |
| 82 | + |
| 83 | + // QuerySnapshotTests |
| 84 | + if (serializedFirestoreData.querySnapshotJson != null) { |
| 85 | + const snapshot = querySnapshotFromJSON(firestore, serializedFirestoreData.querySnapshotJson); |
| 86 | + if (snapshot.docs.length === 1 && validateDocumentData(snapshot.docs[0].data())) { |
| 87 | + testResults.clientSideQuerySnapshotResult = OK; |
| 88 | + } |
| 89 | + |
| 90 | + // onResume test |
| 91 | + const bundleQuerySnapshotPromise = new Promise<void>((resolve, reject) => { |
| 92 | + let completed: boolean = false; |
| 93 | + setTimeout(() => { if (!completed) reject(); }, 2000); |
| 94 | + const unsubscribe = onSnapshotResume( |
| 95 | + firestore, |
| 96 | + serializedFirestoreData.querySnapshotJson!, |
| 97 | + (querySnapshot: QuerySnapshot |
| 98 | + ) => { |
| 99 | + if (querySnapshot.docs.length === 1 && validateDocumentData(querySnapshot.docs[0].data())) { |
| 100 | + testResults.clientSideQuerySnapshotOnResumeResult = OK; |
| 101 | + unsubscribe(); |
| 102 | + completed = true; |
| 103 | + resolve(); |
| 104 | + } |
| 105 | + }); |
| 106 | + }); |
| 107 | + await bundleQuerySnapshotPromise; |
| 108 | + } |
| 109 | + return testResults; |
| 110 | +} |
| 111 | + |
| 112 | +export default function CsrTestRunner(props) { |
24 | 113 | const [testStatus, setTestStatus] = useState<string>("running..."); |
25 | | - const [testResults, setTestResults] = useState(initializeTestResults(/* isServer= */ false)); |
| 114 | + const [testResults, setTestResults] = useState(initializeTestResults()); |
26 | 115 | useEffect(() => { |
27 | 116 | const asyncTest = async () => { |
28 | | - setTestResults(await testFirestore(/* isServer= */ false)); |
| 117 | + let testResults = await testFirestore(/* isServer= */ false); |
| 118 | + testResults = await runDeserializationTests(testResults, props.serializedFirestoreData); |
| 119 | + setTestResults(testResults); |
29 | 120 | setTestStatus("Complete!"); |
30 | 121 | } |
31 | 122 | asyncTest().catch((e) => { |
32 | 123 | console.error("Error encountered during testing: ", e); |
33 | 124 | setTestStatus("Errored!"); |
34 | 125 | }); |
35 | | - }, []); |
| 126 | + }, [props.serializedFirestoreData]); |
36 | 127 |
|
37 | 128 | return ( |
38 | 129 | <ResultsDisplay statusString={testStatus} testResults={testResults} /> |
|
0 commit comments