Skip to content

Commit 285ff93

Browse files
authored
Add Analytics tests (#3)
Add Analytics tests. Other changes which should accelerate creating new tests. - Renaming files to be less verbose, and assume context based on their pathing: `app/tests/app/app_web_ssr/page.tsx` -> `app/tests/app/web_ssr/page.tsx`, for instance - Renaming components in a similar fashion: `AuthResultsDisplay` -> `ResultsDisplay`
1 parent 0859ec2 commit 285ff93

File tree

24 files changed

+399
-36
lines changed

24 files changed

+399
-36
lines changed

app/page.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ export default async function Page() {
2525
<p />
2626
<h2>Manually test:</h2>
2727
<ul>
28+
<li>Analytics</li>
29+
<ul>
30+
<li><Link href="/tests/analytics/web_client">Analytics Web SDK client-side tests</Link></li>
31+
<li><Link href="/tests/analytics/web_ssr">Analytics Web SDK server-side tests</Link></li>
32+
</ul>
2833
<li>App</li>
2934
<ul>
30-
<li><Link href="/tests/app/app_web_client">App Web SDK client-side tests</Link></li>
31-
<li><Link href="/tests/app/app_web_ssr">App Web SDK server-side tests</Link></li>
35+
<li><Link href="/tests/app/web_client">App Web SDK client-side tests</Link></li>
36+
<li><Link href="/tests/app/web_ssr">App Web SDK server-side tests</Link></li>
3237
</ul>
3338
<li>Auth</li>
3439
<ul>
35-
<li><Link href="/tests/auth/auth_web_client">Auth Web SDK client-side tests</Link></li>
36-
<li><Link href="/tests/auth/auth_web_ssr">Auth Web SDK server-side tests</Link></li>
40+
<li><Link href="/tests/auth/web_client">Auth Web SDK client-side tests</Link></li>
41+
<li><Link href="/tests/auth/web_ssr">Auth Web SDK server-side tests</Link></li>
3742
</ul>
3843
</ul>
3944
</>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import type { Metadata } from 'next'
18+
import CsrTestRunner from '../components/csr_test_runner';
19+
20+
export const metadata: Metadata = {
21+
title: 'Analytics Web SDK CSR test'
22+
}
23+
24+
export default function Page() {
25+
return (
26+
<>
27+
<h1>Analytics CSR Test results:</h1>
28+
<CsrTestRunner />
29+
</>
30+
);
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import type { Metadata } from 'next'
18+
import { testApp, TestAnalyticsResult } from '../lib/test';
19+
import ResultsDisplay from '../components/results_display';
20+
21+
export const metadata: Metadata = {
22+
title: 'Analytics Web SDK SSR test'
23+
}
24+
25+
export default async function Page() {
26+
const testAnalyticsResult: TestAnalyticsResult = await testApp(/*isServer=*/true);
27+
return (
28+
<>
29+
<h1>Analytics SSR Test results:</h1>
30+
<ResultsDisplay statusString='Tests Complete!' testAppResult={testAnalyticsResult} />
31+
</>
32+
);
33+
}

app/tests/app/app_web_client/page.tsx renamed to app/tests/analytics/app_web_client/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import type { Metadata } from 'next'
18-
import ClientResults from '../components/client_results';
18+
import ClientResults from '../components/csr_test_runner';
1919

2020
export const metadata: Metadata = {
2121
title: 'App Web SDK CSR test'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
'use client'
18+
19+
import { useState, useEffect } from 'react'
20+
import { testApp, createAnalyticsTestResult } from '../lib/test';
21+
import ResultsDisplay from './results_display';
22+
23+
export default function ClientResults() {
24+
const [testStatus, setTestStatus] = useState<string>("running...");
25+
const [testAnalyticsResult, setTestAnalyticsResult] = useState(createAnalyticsTestResult());
26+
useEffect(() => {
27+
const asyncTest = async () => {
28+
setTestAnalyticsResult(await testApp());
29+
setTestStatus("Complete!");
30+
}
31+
asyncTest().catch((e) => {
32+
console.error("Error encountered during testing: ", e);
33+
setTestStatus("Errored!");
34+
});
35+
}, []);
36+
37+
return (
38+
<ResultsDisplay statusString={testStatus} testAppResult={testAnalyticsResult} />
39+
);
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import Link from 'next/link';
18+
export default function AnalyticsResultsDisplay({ statusString, testAppResult }) {
19+
return (
20+
<>
21+
<h2 title="testStatus">{statusString}</h2>
22+
<h4 title="initializeAppResult">initializeAppResult: {testAppResult.initializeAppResult}</h4>
23+
<h4 title="isSupportedResult">isSupportedResult: {testAppResult.isSupportedResult}</h4>
24+
<h4 title="getAnalyticsResult">deleteAppResult: {testAppResult.getAnalyticsResult}</h4>
25+
<h4 title="logEventResult">logEventResult: {testAppResult.logEventResult}</h4>
26+
<h4 title="deleteAppResult">deleteAppResult: {testAppResult.deleteAppResult}</h4>
27+
<p />
28+
<Link href="/">Back to test index</Link>
29+
</>
30+
);
31+
}

app/tests/analytics/lib/test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import { deleteApp, initializeApp } from 'firebase/app';
18+
import { getAnalytics, logEvent, isSupported } from 'firebase/analytics';
19+
import { firebaseConfig } from 'lib/firebase';
20+
import { OK, OK_SKIPPED, FAILED, sleep } from 'lib/util';
21+
22+
export type TestAnalyticsResult = {
23+
initializeAppResult: string,
24+
isSupportedResult: string,
25+
getAnalyticsResult: string,
26+
logEventResult: string,
27+
deleteAppResult: string
28+
};
29+
30+
export function createAnalyticsTestResult(): TestAnalyticsResult {
31+
const testAnalyticsResult: TestAnalyticsResult = {
32+
initializeAppResult: FAILED,
33+
isSupportedResult: FAILED,
34+
getAnalyticsResult: FAILED,
35+
logEventResult: FAILED,
36+
deleteAppResult: FAILED
37+
};
38+
return testAnalyticsResult;
39+
}
40+
41+
export async function testApp(isServerApp: boolean = false): Promise<TestAnalyticsResult> {
42+
const result: TestAnalyticsResult = createAnalyticsTestResult();
43+
if (isServerApp) {
44+
try {
45+
// Note: Analytics isn't supported in node environments.
46+
const firebaseApp = initializeApp(firebaseConfig);
47+
if (initializeApp !== null) {
48+
result.initializeAppResult = OK;
49+
const supported: boolean = await isSupported();
50+
if (!supported) {
51+
result.isSupportedResult = OK;
52+
}
53+
deleteApp(firebaseApp);
54+
result.deleteAppResult = OK;
55+
}
56+
result.getAnalyticsResult = OK_SKIPPED;
57+
result.logEventResult = OK_SKIPPED;
58+
} catch (e) {
59+
console.log("Caught error: ", e);
60+
}
61+
} else /* !isServer() */ {
62+
try {
63+
const firebaseApp = initializeApp(firebaseConfig);
64+
if (firebaseApp !== null) {
65+
result.initializeAppResult = OK;
66+
const supported: boolean = await isSupported();
67+
if (supported) {
68+
result.isSupportedResult = OK;
69+
}
70+
const analytics = getAnalytics(firebaseApp);
71+
if (analytics !== null) {
72+
result.getAnalyticsResult = OK;
73+
logEvent(analytics, 'begin_checkout');
74+
result.logEventResult = OK;
75+
}
76+
// logEvent throws an error if we delete the app here, due to
77+
// it's asynchronous behavior.
78+
await sleep(1000);
79+
deleteApp(firebaseApp);
80+
result.deleteAppResult = OK;
81+
}
82+
} catch (e) {
83+
console.log("Caught error: ", e);
84+
}
85+
}
86+
return result;
87+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import type { Metadata } from 'next'
18+
import ClientResults from '../components/csr_test_runner';
19+
20+
export const metadata: Metadata = {
21+
title: 'Analytics Web SDK CSR test'
22+
}
23+
24+
export default function Page() {
25+
return (
26+
<>
27+
<h1>Analytics CSR Test results:</h1>
28+
<ClientResults />
29+
</>
30+
);
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import type { Metadata } from 'next'
18+
import { testApp, TestAnalyticsResult } from '../lib/test';
19+
import AnalyticsResultsDisplay from '../components/results_display';
20+
21+
export const metadata: Metadata = {
22+
title: 'Analytics Web SDK SSR test'
23+
}
24+
25+
export default async function Page() {
26+
const testAnalyticsResult: TestAnalyticsResult = await testApp(/*isServer=*/true);
27+
return (
28+
<>
29+
<h1>Analytics SSR Test results:</h1>
30+
<AnalyticsResultsDisplay statusString='Tests Complete!' testAppResult={testAnalyticsResult} />
31+
</>
32+
);
33+
}

app/tests/app/components/client_results.tsx renamed to app/tests/app/components/csr_test_runner.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
'use client'
1818

1919
import { useState, useEffect } from 'react'
20-
import { testApp, createTestAppResult } from '../lib/app_test';
21-
import AppResultsDisplay from './app_results_display';
20+
import { testApp, createTestAppResult } from '../lib/test';
21+
import ResultsDisplay from './results_display';
2222

23-
export default function ClientResults() {
23+
export default function CsrTestRunner() {
2424
const [testStatus, setTestStatus] = useState<string>("running...");
2525
const [testAppResult, setTestAppResult] = useState(createTestAppResult());
2626
useEffect(() => {
@@ -35,6 +35,6 @@ export default function ClientResults() {
3535
}, []);
3636

3737
return (
38-
<AppResultsDisplay statusString={testStatus} testAppResult={testAppResult} />
38+
<ResultsDisplay statusString={testStatus} testAppResult={testAppResult} />
3939
);
4040
}

0 commit comments

Comments
 (0)