feat:(SW-360): redirect authenticated users away from signup

This commit is contained in:
Chuma McPhoy
2024-10-03 14:16:08 +02:00
parent aa20a803d0
commit 6c54a54a9e
4 changed files with 25 additions and 181 deletions

View File

@@ -10,7 +10,7 @@ import NextLevelRewardsBlock from "@/components/Blocks/DynamicContent/Rewards/Ne
import PreviousStays from "@/components/Blocks/DynamicContent/Stays/Previous"
import SoonestStays from "@/components/Blocks/DynamicContent/Stays/Soonest"
import UpcomingStays from "@/components/Blocks/DynamicContent/Stays/Upcoming"
import Form from "@/components/Forms/Register"
import ServerRegisterForm from "@/components/Forms/Register/ServerRegisterForm"
import type { DynamicContentProps } from "@/types/components/blocks/dynamicContent"
import { DynamicContentEnum } from "@/types/enums/dynamicContent"
@@ -53,7 +53,7 @@ export default async function DynamicContent({
case DynamicContentEnum.Blocks.components.previous_stays:
return <PreviousStays {...dynamic_content} />
case DynamicContentEnum.Blocks.components.sign_up_form:
return <Form {...dynamic_content} />
return <ServerRegisterForm {...dynamic_content} />
case DynamicContentEnum.Blocks.components.soonest_stays:
return <SoonestStays {...dynamic_content} />
case DynamicContentEnum.Blocks.components.upcoming_stays:

View File

@@ -1,111 +0,0 @@
import { expect } from "@jest/globals"
import { render, screen, waitFor } from "@testing-library/react"
import { userEvent } from "@testing-library/user-event"
import { editProfile } from "@/actions/editProfile"
import Form from "."
import { type User } from "@/types/user"
// jest.mock("@/actions/editProfile", () => ({
// editProfile: jest.fn().mockResolvedValue({ status: "" }),
// }))
// describe("EditProfile", () => {
// it("Should submit form with correct data", async () => {
// const user: User = {
// email: "test@test.com",
// name: "Test User",
// phoneNumber: "+4612345678",
// dateOfBirth: "1990-01-08",
// address: {
// city: "Test City",
// countryCode: "SE",
// streetAddress: "Test Street",
// zipCode: "12345",
// },
// journeys: [],
// nights: 0,
// shortcuts: [],
// victories: [],
// language: "en",
// firstName: "Test",
// lastName: "User",
// memberships: [],
// profileId: "1",
// }
// render(<Form user={user} />)
// const newValues = {
// email: "new@test.com",
// dateOfBirth: "2000-01-01",
// address: {
// city: "New City",
// countryCode: "SE",
// streetAddress: "New Street",
// zipCode: "67890",
// },
// phoneNumber: "+469999999",
// language: "No",
// password: "oldPassword123!",
// newPassword: "newPassword123!",
// retypeNewPassword: "newPassword123!",
// }
// const dateOfBirthInput = screen.getByTestId("dateOfBirth")
// const daySelect = dateOfBirthInput.querySelector("select[name='date']")!
// const monthSelect = dateOfBirthInput.querySelector("select[name='month']")!
// const yearSelect = dateOfBirthInput.querySelector("select[name='year']")!
// await userEvent.selectOptions(daySelect, "1")
// await userEvent.selectOptions(monthSelect, "1")
// await userEvent.selectOptions(yearSelect, "2000")
// const streetAddressInput = screen.getByTestId("address.streetAddress")
// await userEvent.clear(streetAddressInput)
// await userEvent.type(streetAddressInput, newValues.address.streetAddress)
// const cityInput = screen.getByTestId("address.city")
// await userEvent.clear(cityInput)
// await userEvent.type(cityInput, newValues.address.city)
// const zipCodeInput = screen.getByTestId("address.zipCode")
// await userEvent.clear(zipCodeInput)
// await userEvent.type(zipCodeInput, newValues.address.zipCode)
// const countryInput = screen.getByLabelText("Select a country")
// await userEvent.click(countryInput)
// await userEvent.type(countryInput, "Sweden")
// await userEvent.keyboard("{ArrowDown}{Enter}")
// const emailInput = screen.getByTestId("email")
// await userEvent.clear(emailInput)
// await userEvent.type(emailInput, newValues.email)
// const phoneNumberInput = screen.getByTestId("phoneNumber")
// await userEvent.clear(phoneNumberInput)
// await userEvent.type(phoneNumberInput, newValues.phoneNumber.slice(3)) // Remove country code
// const languageInput = screen.getByTestId("language")
// await userEvent.click(languageInput)
// await userEvent.click(screen.getByTestId("Norwegian"))
// await userEvent.type(screen.getByTestId("password"), newValues.password)
// await userEvent.type(
// screen.getByTestId("newPassword"),
// newValues.newPassword
// )
// await userEvent.type(
// screen.getByTestId("retypeNewPassword"),
// newValues.retypeNewPassword
// )
// await userEvent.click(screen.getByText("Save"))
// await waitFor(() => {
// expect(editProfile).toHaveBeenCalledWith(newValues)
// })
// })
// })

View File

@@ -0,0 +1,23 @@
import { redirect } from "next/navigation"
import { overview } from "@/constants/routes/myPages"
import { auth } from "@/auth"
import { getLang } from "@/i18n/serverContext"
import Form from "../index"
import { RegisterFormProps } from "@/types/components/form/registerForm"
export default async function ServerRegisterForm({
link,
subtitle,
title,
}: RegisterFormProps) {
const session = await auth()
if (session) {
// We don't want to allow users to signup if they are already authenticated.
redirect(overview[getLang()])
}
return <Form link={link} subtitle={subtitle} title={title} />
}

View File

@@ -1,68 +0,0 @@
import { expect } from "@jest/globals"
import { render, screen, waitFor } from "@testing-library/react"
import { userEvent } from "@testing-library/user-event"
import { registerUser } from "@/actions/registerUser"
import Form from "./index"
// Mock server action to prevent api calls
jest.mock("@/actions/registerUser", () => ({
registerUser: jest.fn().mockResolvedValue(true),
}))
describe("Register user form", () => {
test("Should submit form with correct data", async () => {
render(<Form title="Register" subtitle="Register" />)
const values = {
firstName: "John",
lastName: "Doe",
dateOfBirth: "1990-01-01",
address: {
zipCode: "11111",
countryCode: "SE",
},
email: "john.doe@example.com",
phoneNumber: "+46123456789",
password: "securePassword123!",
termsAccepted: true,
}
await userEvent.type(screen.getByTestId("firstName"), values.firstName)
await userEvent.type(screen.getByTestId("lastName"), values.lastName)
const dateOfBirthInput = screen.getByTestId("dateOfBirth")
const daySelect = dateOfBirthInput.querySelector("select[name='date']")!
const monthSelect = dateOfBirthInput.querySelector("select[name='month']")!
const yearSelect = dateOfBirthInput.querySelector("select[name='year']")!
await userEvent.selectOptions(daySelect, "1")
await userEvent.selectOptions(monthSelect, "1")
await userEvent.selectOptions(yearSelect, "1990")
await userEvent.type(
screen.getByTestId("address.zipCode"),
values.address.zipCode
)
const countryInput = screen.getByLabelText("Select a country")
await userEvent.click(countryInput)
await userEvent.type(countryInput, "Sweden")
await userEvent.keyboard("{ArrowDown}{Enter}")
await userEvent.type(screen.getByTestId("email"), values.email)
await userEvent.type(
screen.getByTestId("phoneNumber"),
values.phoneNumber.slice(3)
)
await userEvent.type(screen.getByTestId("password"), values.password)
await userEvent.click(screen.getAllByTestId("termsAccepted")[0])
await userEvent.click(screen.getByTestId("submit"))
await waitFor(() => {
expect(registerUser).toHaveBeenCalledWith(values)
})
})
})