Skip to content

Commit fd4a45f

Browse files
authored
Added messaging tests (#9)
Added tests for the Messaging SDK.
1 parent a8e8c03 commit fd4a45f

File tree

7 files changed

+242
-0
lines changed

7 files changed

+242
-0
lines changed

app/page.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export default async function Page() {
6666
<li><Link href="/tests/functions/web_client">Functions Web SDK client-side tests</Link></li>
6767
<li><Link href="/tests/functions/web_ssr">Functions Web SDK server-side tests</Link></li>
6868
</ul>
69+
<p />
70+
<li>Messaging</li>
71+
<ul>
72+
<li><Link href="/tests/messaging/web_client">Messaging Web SDK client-side tests</Link></li>
73+
<li><Link href="/tests/messaging/web_ssr">Messaging Web SDK server-side tests</Link></li>
74+
</ul>
6975
</ul>
7076
</>
7177
);
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 { testMessaging, initializeTestResults } from '../lib/test';
21+
import ResultsDisplay from './results_display';
22+
23+
export default function CsrTestRunner() {
24+
const [testStatus, setTestStatus] = useState<string>("running...");
25+
const [testResults, setTestResults] = useState(initializeTestResults());
26+
useEffect(() => {
27+
const asyncTest = async () => {
28+
setTestResults(await testMessaging());
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} testResults={testResults} />
39+
);
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 ResultsDisplay({ statusString, testResults }) {
19+
return (
20+
<>
21+
<h2 title="testStatus">{statusString}</h2>
22+
<h4 title="initializeAppResult">initializeAppResult: {testResults.initializeAppResult}</h4>
23+
<h4 title="initializeMessagingResult">initializeMessagingResult: {testResults.initializeMessagingResult}</h4>
24+
<h4 title="deleteAppResult">deleteAppResult: {testResults.deleteAppResult}</h4>
25+
<p />
26+
<Link href="/">Back to test index</Link>
27+
</>
28+
);
29+
}

app/tests/messaging/lib/test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
18+
import { deleteApp, initializeApp } from 'firebase/app';
19+
import { firebaseConfig } from 'lib/firebase';
20+
import { getMessaging } from 'firebase/messaging';
21+
import { OK, OK_SKIPPED, FAILED } from 'lib/util';
22+
23+
export type TestResults = {
24+
initializeAppResult: string,
25+
initializeMessagingResult: string,
26+
deleteAppResult: string
27+
};
28+
29+
export function initializeTestResults(): TestResults {
30+
return {
31+
initializeAppResult: FAILED,
32+
initializeMessagingResult: FAILED,
33+
deleteAppResult: FAILED
34+
};
35+
}
36+
37+
export async function testMessaging(isServer: boolean = false): Promise<TestResults> {
38+
const result: TestResults = initializeTestResults();
39+
if (isServer) {
40+
result.initializeAppResult = OK_SKIPPED;
41+
result.initializeMessagingResult = OK_SKIPPED;
42+
result.deleteAppResult = OK_SKIPPED;
43+
return result;
44+
}
45+
46+
try {
47+
const firebaseApp = initializeApp(firebaseConfig);
48+
if (firebaseApp === null) {
49+
return result;
50+
}
51+
result.initializeAppResult = OK;
52+
53+
const messaging = getMessaging(firebaseApp);
54+
if (!isServer && messaging || isServer && !messaging) {
55+
result.initializeMessagingResult = OK;
56+
}
57+
58+
deleteApp(firebaseApp);
59+
result.deleteAppResult = OK;
60+
} catch (e) {
61+
console.error("Caught error: ", e);
62+
}
63+
return result;
64+
}
+31
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: 'Messaging Web SDK CSR test'
22+
}
23+
24+
export default function Page() {
25+
return (
26+
<>
27+
<h1>Messaging CSR Test results:</h1>
28+
<CSRTestRunner />
29+
</>
30+
);
31+
}

app/tests/messaging/web_ssr/page.tsx

+33
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 { testMessaging, TestResults } from '../lib/test';
19+
import ResultsDisplay from '../components/results_display';
20+
21+
export const metadata: Metadata = {
22+
title: 'Messaging Web SDK SSR test'
23+
}
24+
25+
export default async function Page() {
26+
const testResults: TestResults = await testMessaging(/*isServer=*/true);
27+
return (
28+
<>
29+
<h1>Messaging SSR Test results:</h1>
30+
<ResultsDisplay statusString='Tests Complete!' testResults={testResults} />
31+
</>
32+
);
33+
}

tests/messsaging.spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 { test, expect } from '@playwright/test';
18+
19+
test.describe.configure({ mode: 'serial' });
20+
21+
async function commonExpectations(page) {
22+
await expect(page.getByTitle('initializeAppResult')).not.toContainText("FAILED");
23+
await expect(page.getByTitle('initializeMessagingResult')).not.toContainText("FAILED");
24+
await expect(page.getByTitle('deleteAppResult')).not.toContainText("FAILED");
25+
}
26+
27+
test('functions operations should pass - client', async ({ page, baseURL }) => {
28+
await page.goto(`${baseURL}/tests/messaging/web_client`);
29+
await expect(page.getByTitle('testStatus')).toContainText('Complete', { timeout: 10000 });
30+
await expect(page.locator('h1')).toContainText('Messaging CSR Test');
31+
await commonExpectations(page);
32+
});
33+
34+
test('functions operations should pass - server', async ({ page, baseURL }) => {
35+
await page.goto(`${baseURL}/tests/messaging/web_ssr`);
36+
await expect(page.getByTitle('testStatus')).toContainText('Complete', { timeout: 10000 });
37+
await expect(page.locator('h1')).toContainText('Messaging SSR Test');
38+
await commonExpectations(page);
39+
});

0 commit comments

Comments
 (0)