tests: fix firestore not authenticating

This commit is contained in:
Sidney Alcantara
2022-06-16 14:48:54 +10:00
parent 2fc3fa6832
commit 690386b5f9
4 changed files with 50 additions and 28 deletions

View File

@@ -100,7 +100,7 @@ function JotaiTest() {
{currentUser && <CurrentUser currentUser={currentUser} />}
<p>{JSON.stringify(userRoles)}</p>
<p>{projectId}</p>
<p>Project: {projectId}</p>
<p>{JSON.stringify(publicSettings)}</p>
<p>{JSON.stringify(projectSettings)}</p>
<p>{JSON.stringify(userSettings)}</p>

33
src/test/App.test.tsx Normal file
View File

@@ -0,0 +1,33 @@
import { customRender, signInAsAdmin } from "./testUtils";
import { screen, renderHook } from "@testing-library/react";
import { useAtom, useSetAtom } from "jotai";
import App from "@src/App";
import JotaiTestPage from "@src/pages/Test/JotaiTestPage";
import { globalScope, currentUserAtom } from "@src/atoms/globalScope";
test("renders without crashing", async () => {
customRender(<JotaiTestPage />);
expect(await screen.findByText(/Sign in with Google/i)).toBeInTheDocument();
expect(await screen.findByText(/Authenticating/i)).toBeInTheDocument();
expect(
(await screen.findAllByText(/Project: rowy-testing/i)).length
).toBeGreaterThan(0);
});
test("signs in", async () => {
const initialAtomValues = await signInAsAdmin();
customRender(<App />, initialAtomValues, true);
// const {
// result: { current: currentUser },
// } = renderHook(() => useSetAtom(currentUserAtom, globalScope));
// expect(currentUser).toBeDefined();
// expect(await screen.findByText(/Loading/i)).toBeInTheDocument();
expect((await screen.findAllByText(/tablesd/i)).length).toBeGreaterThan(0);
});
// TODO:
// test("signs in without roles in auth")

View File

@@ -1,24 +0,0 @@
import { customRender, signInAsAdmin } from "./testUtils";
import { screen } from "@testing-library/react";
import App from "@src/App";
import JotaiTest from "@src/pages/JotaiTest";
test("renders without crashing", async () => {
customRender(<JotaiTest />);
expect(await screen.findByText(/Sign in with Google/i)).toBeInTheDocument();
expect(await screen.findByText(/{"emulator":true}/i)).toBeInTheDocument();
});
// test("signs in", async () => {
// const initialAtomValues = await signInAsAdmin();
// customRender(<App />, initialAtomValues);
// expect(await screen.findByText(/Loading/i)).toBeInTheDocument();
// // expect(await screen.findByText(/Nav/i)).toBeInTheDocument();
// expect(await screen.findByText(/{"emulator":true}/i)).toBeInTheDocument();
// });
// TODO:
// test("signs in without roles in auth")

View File

@@ -5,12 +5,19 @@ import {
connectAuthEmulator,
signInWithEmailAndPassword,
} from "firebase/auth";
import {
initializeFirestore,
connectFirestoreEmulator,
} from "firebase/firestore";
import Providers, { IProvidersProps } from "@src/Providers";
import ProjectSourceFirebase from "@src/sources/ProjectSourceFirebase";
import {
envConfig,
firebaseConfigAtom,
firebaseAppAtom,
firebaseAuthAtom,
firebaseDbAtom,
} from "@src/sources/ProjectSourceFirebase";
import { currentUserAtom } from "@src/atoms/globalScope";
@@ -20,11 +27,12 @@ import { currentUserAtom } from "@src/atoms/globalScope";
*/
export const customRender = (
ui: React.ReactElement,
initialAtomValues?: IProvidersProps["initialAtomValues"]
initialAtomValues?: IProvidersProps["initialAtomValues"],
disableProjectSource: boolean = false
) =>
render(
<Providers initialAtomValues={initialAtomValues}>
<ProjectSourceFirebase />
{!disableProjectSource && <ProjectSourceFirebase />}
{ui}
</Providers>
);
@@ -37,6 +45,8 @@ export const signInAsAdmin = async () => {
const app = initializeApp(envConfig);
const auth = getAuth(app);
connectAuthEmulator(auth, "http://localhost:9099", { disableWarnings: true });
const db = initializeFirestore(app, { ignoreUndefinedProperties: true });
connectFirestoreEmulator(db, "localhost", 9299);
const userCredential = await signInWithEmailAndPassword(
auth,
@@ -49,8 +59,11 @@ export const signInAsAdmin = async () => {
expect(tokenResult.claims.roles).toContain("ADMIN");
const initialAtomValues = [
[firebaseConfigAtom, envConfig],
[firebaseAppAtom, app],
[firebaseAuthAtom, auth],
// [currentUserAtom, userCredential.user],
[firebaseDbAtom, db],
[currentUserAtom, userCredential.user],
] as const;
return initialAtomValues;