-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(deps): update next to a version that exists * feat(tests): refactor into smaller tests
- Loading branch information
Showing
15 changed files
with
303 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react" | ||
import { render, screen } from "@testing-library/react" | ||
|
||
import Courses, { Props } from "./courses" | ||
|
||
describe("<Courses />", () => { | ||
it("should render correctly with 1 course", async() => { | ||
const mockCourses: Props["courses"] = [ | ||
{ | ||
id: 1, | ||
title: "fake title 1", | ||
description: "fake description 1", | ||
cost: 1, | ||
type: "fake type 1", | ||
capacity: 1, | ||
registered: 1, | ||
}, | ||
]; | ||
|
||
render(<Courses courses={mockCourses} />); | ||
|
||
expect(screen.getByText("Courses")).toBeInTheDocument(); | ||
expect(screen.getAllByTestId("course-row").length).toStrictEqual(1); | ||
expect(screen.getByRole("cell", { name: "fake title 1" })).toBeInTheDocument(); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import { rest } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
import { RegistrationFormDto } from "@/types/RegistrationFormDto"; | ||
import RegistrationForm, { Props } from "./registration-form"; | ||
|
||
const server = setupServer(); | ||
|
||
describe("RegistrationForm", () => { | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it("renders the page correctly", () => { | ||
const mockCourses: Props["courses"] = [ | ||
{ | ||
id: 1, | ||
title: "fake title 1", | ||
description: "fake description 1", | ||
cost: 1, | ||
type: "fake type 1", | ||
capacity: 1, | ||
registered: 1, | ||
}, | ||
]; | ||
|
||
render( | ||
<RegistrationForm courses={mockCourses} /> | ||
); | ||
|
||
expect(screen.getByText("Register onto Course")).toBeInTheDocument(); | ||
expect(screen.getByPlaceholderText("First Name")).toBeInTheDocument(); | ||
expect(screen.getByPlaceholderText("Last Name")).toBeInTheDocument(); | ||
expect(screen.getByPlaceholderText("Email")).toBeInTheDocument(); | ||
expect( | ||
screen.getByRole("button", { name: "Register" }) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("handles form submission correctly", async () => { | ||
const mockRegistrationData: RegistrationFormDto = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@example.com", | ||
courseId: "1", | ||
}; | ||
|
||
server.use( | ||
rest.post("http://localhost:3000/api/register", async (req, res, ctx) => { | ||
const requestJson = await req.json() as Prisma.RegistrationCreateInput; | ||
const { firstName, lastName, email, course } = requestJson; | ||
|
||
// Return a mock response | ||
return res(ctx.json({ id: 1, firstName, lastName, email })); | ||
}) | ||
); | ||
|
||
const mockCourses: Props["courses"] = [ | ||
{ | ||
id: 1, | ||
title: "fake title 1", | ||
description: "fake description 1", | ||
cost: 1, | ||
type: "fake type 1", | ||
capacity: 1, | ||
registered: 1, | ||
}, | ||
]; | ||
const user = userEvent.setup(); | ||
|
||
render(<RegistrationForm courses={mockCourses} />); | ||
|
||
// Enter form input values | ||
await user.type( | ||
screen.getByPlaceholderText("First Name"), | ||
mockRegistrationData.firstName | ||
); | ||
await user.type( | ||
screen.getByPlaceholderText("Last Name"), | ||
mockRegistrationData.lastName | ||
); | ||
await user.type(screen.getByPlaceholderText("Email"), mockRegistrationData.email); | ||
|
||
// Select a course from the dropdown | ||
await user.selectOptions( | ||
screen.getByPlaceholderText("Course"), | ||
mockRegistrationData.courseId.toString() | ||
); | ||
|
||
// Submit the form | ||
await user.click(screen.getByRole("button", { name: "Register" })); | ||
|
||
// Assert that form fields are cleared | ||
expect(screen.getByPlaceholderText("First Name")).toHaveValue(""); | ||
expect(screen.getByPlaceholderText("Last Name")).toHaveValue(""); | ||
expect(screen.getByPlaceholderText("Email")).toHaveValue(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
import { Course } from "@/types"; | ||
import Registrations, { Props } from './registrations'; | ||
|
||
describe("Registrations", () => { | ||
it("should render correctly with 1 registration", () => { | ||
const mockCourse: Course = { | ||
id: 1, | ||
title: "fake title 1", | ||
description: "fake description 1", | ||
cost: 1, | ||
type: "fake type 1", | ||
capacity: 1, | ||
registered: 1, | ||
} | ||
|
||
const mockRegistrations: Props["registrations"] = [ | ||
{ | ||
id: 1, | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "john@example.com", | ||
courseId: 1, | ||
course: mockCourse, | ||
}, | ||
]; | ||
|
||
render( | ||
<Registrations registrations={mockRegistrations} /> | ||
); | ||
|
||
expect(screen.getByText("Registrations")).toBeInTheDocument(); | ||
expect(screen.getByText("John Doe")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import { RegistrationForm } from '@/types/RegistrationForm'; | ||
import axios from 'axios'; | ||
|
||
import { RegistrationFormDto } from '@/types/RegistrationFormDto'; | ||
import transformRegistrationForm from '../utils/transformRegistrationForm'; | ||
|
||
export default async function saveRegistrationForm(registrationForm: RegistrationForm) { | ||
let registrationData = transformRegistrationForm(registrationForm) | ||
export default async function saveRegistrationForm(registrationFormDto: RegistrationFormDto) { | ||
let registrationData = transformRegistrationForm(registrationFormDto) | ||
|
||
const response = await fetch('/api/register', { | ||
method: 'POST', | ||
body: JSON.stringify(registrationData) | ||
const response = await axios.post('http://localhost:3000/api/register', { | ||
...registrationData | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(response.statusText); | ||
} | ||
return await response.json(); | ||
return response.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import { RegistrationForm as RegistrationFormDto } from '@/types/RegistrationForm'; | ||
import { RegistrationFormDto } from '@/types/RegistrationFormDto'; | ||
|
||
export default function transformRegistrationForm(registrationForm: RegistrationFormDto): Prisma.RegistrationCreateInput { | ||
const registrationDb: Prisma.RegistrationCreateInput = { | ||
firstName: registrationForm.firstName, | ||
lastName: registrationForm.lastName, | ||
email: registrationForm.email, | ||
course: { | ||
connect: { id: parseInt(registrationForm.courseId, 10) } | ||
} | ||
}; | ||
const transformRegistrationForm = (registrationForm: RegistrationFormDto): Prisma.RegistrationCreateInput => ({ | ||
firstName: registrationForm.firstName, | ||
lastName: registrationForm.lastName, | ||
email: registrationForm.email, | ||
course: { | ||
connect: { id: parseInt(registrationForm.courseId, 10) } | ||
} | ||
}); | ||
|
||
return registrationDb; | ||
} | ||
export default transformRegistrationForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.