From 2bb32235baa52d5a80c0401688e5bc5a0df99f1d Mon Sep 17 00:00:00 2001 From: "Raphael J. Sandor" <45048465+R-Sandor@users.noreply.github.com> Date: Sun, 8 Sep 2024 12:17:31 -0400 Subject: [PATCH] fix test --- .github/workflows/sonar.yml | 22 ++++- .../resetPassword/passwordResetPage.test.tsx | 98 ++++++++++--------- frontend/app/account/resetPassword/page.tsx | 7 +- 3 files changed, 73 insertions(+), 54 deletions(-) diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 5dcc2a6f..c56838a4 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -9,8 +9,8 @@ on: pull_request: types: [opened, synchronize, reopened] jobs: - sonarCloudServer: - name: sonarCloudServer + sonarQubeServer: + name: SonarQube Server runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -20,7 +20,7 @@ jobs: uses: actions/setup-java@v4 with: java-version: 17 - distribution: 'zulu' # see if this makes a difference on the integration tests. + distribution: 'temurin' - name: Cache SonarQube packages uses: actions/cache@v4 with: @@ -33,7 +33,7 @@ jobs: path: ~/.gradle/caches key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} restore-keys: ${{ runner.os }}-gradle - - name: sonarQube Scan 1 + - name: Scan server env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # analysis token associated to your project SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} @@ -41,3 +41,17 @@ jobs: cd server/ ./scripts/createServerKeys.sh ./gradlew build sonar --info + sonarQubeFrontend: + name: sonarQube Frontend + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Scan Frontend + uses: sonarsource/sonarqube-scan-action@master + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN_FRONTEND }} # analysis token associated to your project + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} + with: + projectBaseDir: frontend/ diff --git a/frontend/__tests__/account/resetPassword/passwordResetPage.test.tsx b/frontend/__tests__/account/resetPassword/passwordResetPage.test.tsx index 09f24329..fbea9fca 100644 --- a/frontend/__tests__/account/resetPassword/passwordResetPage.test.tsx +++ b/frontend/__tests__/account/resetPassword/passwordResetPage.test.tsx @@ -6,63 +6,67 @@ import axios from "axios"; import MockAdapter from "axios-mock-adapter"; import { act } from "react-dom/test-utils"; import { beforeEach, beforeAll, vi, describe, it, expect } from "vitest"; +import { debug } from "vitest-preview"; const user = userEvent.setup(); beforeEach(async () => { - await act(async () => { - render(); - }); + await act(async () => { + render(); + }); }); beforeAll(() => { - vi.mock("next/navigation", async (importOriginal) => { - const actual = (await importOriginal()) as Object; - return { - ...actual, - useRouter: vi.fn(() => ({ - push: vi.fn(), - })), - // giberish token - usePathname: vi.fn().mockImplementation(() => "/account/login/"), - }; - }); + vi.mock("next/navigation", async (importOriginal) => { + const actual = (await importOriginal()) as Object; + return { + ...actual, + useRouter: vi.fn(() => ({ + push: vi.fn(), + })), + // giberish token + usePathname: vi.fn().mockImplementation(() => "/account/login/"), + }; + }); }); describe('User attempts reset password:', () => { - it('User enters invalid email', async () => { - const email = screen.getByPlaceholderText(/email/i); - await act(async () => { - await user.type(email, "ajacobs") - await clickAway(user) - }) - expect(screen.getByText("Invalid email")).toBeInTheDocument() - await act(async () => { - await user.clear(email) - }) - expect(screen.getByText("Required")).toBeInTheDocument() + it('User enters invalid email', async () => { + const email = screen.getByPlaceholderText(/email/i); + await act(async () => { + await user.type(email, "ajacobs") + await clickAway(user) + }) + expect(screen.getByText("Invalid email")).toBeInTheDocument() + await act(async () => { + await user.clear(email) }) - it('User enters an email that does not exist.', async () => { - const email = screen.getByPlaceholderText(/email/i); + expect(screen.getByText("Required")).toBeInTheDocument() + }) + it('User enters an email that does not exist.', async () => { + const email = screen.getByPlaceholderText(/email/i); - // This sets the mock adapter on the default instance - let mock = new MockAdapter(axios); - mock.onPost().reply(400, "User does not exist") - await act(async () => { - await user.type(email, "ajacobs@gmail.com") - await user.click(screen.getByText(/submit/i)) - }) - await waitFor(() => { - expect(screen.getByText("User does not exist")).toBeInTheDocument(); - }); + // This sets the mock adapter on the default instance + let mock = new MockAdapter(axios); + mock.onPost().reply(400, { + error: 'User does not exist' + }) + await act(async () => { + await user.type(email, "ajacobs@gmail.com") + await user.click(screen.getByText(/submit/i)) }) - it('User requests valid password reset for email.', async () => { - const email = screen.getByPlaceholderText(/email/i); - // This sets the mock adapter on the default instance - let mock = new MockAdapter(axios); - mock.onPost().reply(200, "Password reset sent") - await act(async () => { - await user.type(email, "ajacobs@gmail.com") - await user.click(screen.getByText(/submit/i)) - }) + debug(); + await waitFor(() => { + expect(screen.getByText("User does not exist")).toBeInTheDocument(); + }); + }) + it('User requests valid password reset for email.', async () => { + const email = screen.getByPlaceholderText(/email/i); + // This sets the mock adapter on the default instance + let mock = new MockAdapter(axios); + mock.onPost().reply(200, "Password reset sent") + await act(async () => { + await user.type(email, "ajacobs@gmail.com") + await user.click(screen.getByText(/submit/i)) }) -}) \ No newline at end of file + }) +}) diff --git a/frontend/app/account/resetPassword/page.tsx b/frontend/app/account/resetPassword/page.tsx index 95ab067b..92428c7b 100644 --- a/frontend/app/account/resetPassword/page.tsx +++ b/frontend/app/account/resetPassword/page.tsx @@ -59,8 +59,8 @@ export default function Page() { useEffect(() => { if (submitSuccess) { setSubmitMessage("Reset token has been sent to your email."); - } - }, [submitSuccess]); + } + }, [submitSuccess, submitMessage]); const handleOnSubmit = async (forgot: ForgotPasswordRequest, actions: any) => { axios.post(resetUrl + "?email=" + forgot.email) @@ -68,9 +68,10 @@ export default function Page() { if (response.status == 200) setSubmitSuccess(true); }).catch((rejected) => { + console.log("REJECTED") setSubmitMessage(rejected.response.data.error); setSubmitSuccess(false); - actions.resetForm(); + // actions.resetForm(); }) }; return (