Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const config: Config = {
"!**/jest.config.ts",
"!**/index.{js,jsx,ts,tsx}",
"!src/shared/components/ui/**",
"!src/shared/lib/**",
"!**/types/**",
],
testPathIgnorePatterns: ["/node_modules/", "/.next/"],
Expand Down
6 changes: 5 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const fontMono = Geist_Mono({
variable: "--font-mono",
})

export const metadata = {
title: "PetControl",
}

export default function RootLayout({
children,
}: Readonly<{
Expand All @@ -34,7 +38,7 @@ export default function RootLayout({
<Providers>
<ThemeProvider>
<Header />
<main className="flex w-full justify-center">{children}</main>
<main className="mb-10 flex w-full justify-center">{children}</main>
<Footer />
<CustomToaster />
</ThemeProvider>
Expand Down
57 changes: 52 additions & 5 deletions src/app/pets/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,70 @@
import { renderWithProviders } from "@/shared/test-utils/custom-render"
import Page from "./page"
import { screen } from "@testing-library/react"
import { petPageMock } from "@/shared/test-utils/mocks/pets-mocks"
import { usePetsListPageViewModel } from "@/features/pets"

jest.mock("@/features/pets", () => ({
...jest.requireActual("@/features/pets"),
usePetsListPageViewModel: jest.fn(),
PetListHeader: () => <div data-testid="pet-list-header" />,
}))

jest.mock("@/shared/components/health-check-test/HealthCheckTest", () => ({
HealthCheckTest: () => <div data-testid="health-check-test" />,
}))
const baseHookMock = {
pagePetCurrent: petPageMock,
isLoading: false,
page: 0,
handleChangePage: jest.fn(),
}

describe("Pets Page", () => {
beforeEach(() => {
;(usePetsListPageViewModel as jest.Mock).mockReturnValue(baseHookMock)
})

it("should render PetListHeader", () => {
renderWithProviders(<Page />)
expect(screen.getByTestId("pet-list-header")).toBeInTheDocument()
})

it("should render HealthCheckTest", () => {
it("should render a card for each pet in the list", () => {
renderWithProviders(<Page />)
const titleElements = screen.getAllByRole("heading", { level: 3 })
expect(titleElements).toHaveLength(petPageMock.content.length)
})

it('should display "nenhum pet foi encontrado" when the list is empty', () => {
;(usePetsListPageViewModel as jest.Mock).mockReturnValue({
...baseHookMock,
pagePetCurrent: { ...petPageMock, content: [] },
})

renderWithProviders(<Page />)
expect(screen.getByText(/nenhum pet foi encontrado/i)).toBeInTheDocument()
})

it("should display only the loader when loading and not render any pet card", () => {
;(usePetsListPageViewModel as jest.Mock).mockReturnValue({
...baseHookMock,
isLoading: true,
})

renderWithProviders(<Page />)
expect(screen.getByTestId("section-loader")).toBeInTheDocument()
expect(screen.queryAllByRole("heading", { level: 3 })).toHaveLength(0)
})

it("should display the current page and total pages correctly in the pagination", () => {
renderWithProviders(<Page />)
expect(screen.getByTestId("health-check-test")).toBeInTheDocument()

const strongElements = screen.getAllByRole("strong")

expect(strongElements.length).toBeGreaterThanOrEqual(2)

const currentPage = Number(strongElements[0].textContent)
const totalPages = Number(strongElements[1].textContent)

expect(currentPage).toBe(baseHookMock.page + 1)
expect(totalPages).toBe(baseHookMock.pagePetCurrent.totalPages)
})
})
34 changes: 28 additions & 6 deletions src/app/pets/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { HealthCheckTest } from "@/shared/components/health-check-test/HealthCheckTest"
import { PetListHeader } from "@/features/pets"
"use client"

import {
PetList,
PetListHeader,
usePetsListPageViewModel,
} from "@/features/pets"
import { CustomPagination, SectionLoader } from "@/shared/components"

export default function Page() {
const { pagePetCurrent, isLoading, page, handleChangePage } =
usePetsListPageViewModel()

return (
<section className="w-full px-10 py-8 lg:max-w-7xl">
<PetListHeader />
<div className="flex items-center justify-center p-12">
<HealthCheckTest />
<section className="flex w-full flex-col px-10 py-8 lg:max-w-7xl">
<PetListHeader totalPets={pagePetCurrent.totalElements} />

<div className="mt-8 flex w-full flex-col items-center justify-center gap-10">
<SectionLoader
className="min-h-90 items-start pt-10"
isLoading={isLoading}
/>

{!isLoading && <PetList pets={pagePetCurrent.content} />}

<CustomPagination
page={page}
totalPages={pagePetCurrent.totalPages}
onPageChange={handleChangePage}
isLoading={isLoading}
/>
</div>
</section>
)
Expand Down
9 changes: 9 additions & 0 deletions src/features/pets/api/pets-api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { api } from "@/shared/lib/api.config"
import { PetSchema } from "../schemas/pet.schema"
import { Pet } from "../types/pets.types"
import { PageResponse } from "@/shared/types/common.types"

const BASE_URL = "/pets"

export const petsApi = {
create: async (pet: PetSchema): Promise<void> => {
await api.post(BASE_URL, pet)
},

get: async (page: number, size: number): Promise<PageResponse<Pet>> => {
const { data } = await api.get(BASE_URL, {
params: { page, size },
})
return data
},
}
57 changes: 57 additions & 0 deletions src/features/pets/components/pet-card/PetCard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { render, screen } from "@testing-library/react"
import { PetCard } from "./PetCard"
import { petResponseMock } from "@/shared/test-utils/mocks/pets-mocks"
import { renderWithProviders } from "@/shared/test-utils/custom-render"

describe(PetCard.name, () => {
it("should display pet name, status and specie", () => {
renderWithProviders(<PetCard pet={petResponseMock} />)

expect(screen.getByText("Rex")).toBeInTheDocument()
expect(screen.getByText("Disponível")).toBeInTheDocument()
expect(screen.getByText("Cachorro")).toBeInTheDocument()
})

it("should display edit and delete buttons", () => {
renderWithProviders(<PetCard pet={petResponseMock} />)

expect(screen.getByRole("button", { name: /editar/i })).toBeInTheDocument()
expect(screen.getByRole("button", { name: /excluir/i })).toBeInTheDocument()
})

it("should not display any tag when tags array is empty", () => {
const pet = { ...petResponseMock, tags: [] }
renderWithProviders(<PetCard pet={pet} />)

expect(screen.queryAllByTestId("pet-tag")).toHaveLength(0)
})

it("should display exactly 5 tags", () => {
const tags = ["vacinado", "treinado", "amigável", "tímido", "sociável"]
const pet = { ...petResponseMock, tags }
renderWithProviders(<PetCard pet={pet} />)

tags.forEach((tag) => {
expect(screen.getByText(tag)).toBeInTheDocument()
})
expect(screen.queryByText("...")).not.toBeInTheDocument()
})

it("should display ellipsis when more than 5 tags", () => {
const tags = [
"vacinado",
"treinado",
"amigável",
"tímido",
"sociável",
"castrado",
]
const pet = { ...petResponseMock, tags }
render(<PetCard pet={pet} />)

tags.slice(0, 5).forEach((tag) => {
expect(screen.getByText(tag)).toBeInTheDocument()
})
expect(screen.getByText("...")).toBeInTheDocument()
})
})
53 changes: 53 additions & 0 deletions src/features/pets/components/pet-card/PetCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Pet } from "../../types/pets.types"
import { ContainerLayout, DeleteButton, InfoButton } from "@/shared/components"
import { Edit, Trash2, PawPrint } from "lucide-react"
import { PetStatusBadge } from "../pet-status-badge/PetStatusBadge"

interface PetCardProps {
pet: Pet
}

export function PetCard({ pet }: PetCardProps) {
return (
<ContainerLayout
key={pet.id}
className="flex flex-col gap-4 px-3 py-4 duration-100 hover:border-2 hover:border-primary"
>
<div className="flex h-40 items-center justify-center rounded-lg border-2 border-dashed border-gray-300 bg-gray-200">
<PawPrint size={65} className="text-gray-300" />
</div>

<div className="flex flex-col gap-0.5">
<div className="flex items-start justify-between">
<h3 className="text-xl font-bold text-gray-800">{pet.name}</h3>
<PetStatusBadge status={pet.status} />
</div>
<p className="text-sm text-gray-500 italic">{pet.specieName}</p>
</div>

<div className="flex h-10 flex-wrap items-start gap-x-2 gap-y-2">
{pet.tags.slice(0, 5).map((tag) => (
<span
data-testid="pet-tag"
key={tag}
className="rounded-2xl border border-gray-200 bg-gray-100 px-2 py-0.5 text-center text-[11px] font-medium text-gray-700"
>
{tag}
</span>
))}
{pet.tags.length > 5 && (
<span className="text-xs font-bold text-gray-400">...</span>
)}
</div>

<div className="mt-2 flex gap-3 border-t-2 pt-4">
<InfoButton className="flex-1 text-sm">
<Edit size={16} /> Editar
</InfoButton>
<DeleteButton className="text-sm">
<Trash2 size={16} /> Excluir
</DeleteButton>
</div>
</ContainerLayout>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ describe(PetListHeader.name, () => {
})

it("should render the 'Adicionar Pet' button", () => {
renderWithProviders(<PetListHeader />)
renderWithProviders(<PetListHeader totalPets={0} />)
expect(
screen.getByRole("button", { name: /adicionar pet/i })
).toBeInTheDocument()
})

it("should navigate to '/pets/novo' when the button is clicked", async () => {
renderWithProviders(<PetListHeader />)
renderWithProviders(<PetListHeader totalPets={0} />)
await userEvent.click(
screen.getByRole("button", { name: /adicionar pet/i })
)
Expand Down
17 changes: 15 additions & 2 deletions src/features/pets/components/pet-list-header/PetListHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

import { PrimaryButton } from "@/shared/components"
import { useNavigate } from "@/shared/hooks/useNavigate"
import { cn } from "@/shared/lib/utils"
import { Plus } from "lucide-react"
interface PetListHeaderProps {
totalPets: number | undefined
}

export function PetListHeader() {
export function PetListHeader({ totalPets }: PetListHeaderProps) {
const { toNewPet } = useNavigate()

return (
<header className="flex items-center justify-between">
<p>4 pets encontrados</p>
<p
className={cn(
"pl-2 tracking-wide text-gray-500",
totalPets ? "visible" : "invisible"
)}
>
{totalPets ?? 0}{" "}
{totalPets === 1 ? "pet encontrado" : "pets encontrados"}
</p>

<PrimaryButton onClick={toNewPet}>
<Plus strokeWidth={2.5} className="scale-120" /> Adicionar Pet
</PrimaryButton>
Expand Down
26 changes: 26 additions & 0 deletions src/features/pets/components/pet-list/PetList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Frown } from "lucide-react"
import { Pet } from "../../types/pets.types"
import { PetCard } from "../pet-card/PetCard"

interface PetListProps {
pets: Pet[]
}

export function PetList({ pets }: PetListProps) {
if (pets.length === 0) {
return (
<div className="flex flex-col items-center justify-center gap-5 py-10">
<Frown size={72} className="text-gray-300" />
<p className="text-gray-500">Nenhum pet foi encontrado.</p>
</div>
)
}

return (
<div className="grid w-full grid-cols-1 justify-center gap-6 md:grid-cols-2 lg:grid-cols-3">
{pets.map((pet) => (
<PetCard key={pet.id} pet={pet} />
))}
</div>
)
}
23 changes: 23 additions & 0 deletions src/features/pets/components/pet-status-badge/PetStatusBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PetStatus, petStatusLabels } from "../../enums/pet-status.enum"

interface PetStatusBadgeProps {
status: PetStatus
}

const petStatusColors: Record<PetStatus, string> = {
AVAILABLE: "bg-green-100 text-green-700 border-green-200",
IN_ANALYSIS: "bg-yellow-100 text-yellow-700 border-yellow-300",
ADOPTED: "bg-sky-100 text-sky-700 border-sky-200",
}

export function PetStatusBadge({ status }: PetStatusBadgeProps) {
return (
<span
className={`rounded-full border px-2.5 py-1 text-xs font-semibold ${
petStatusColors[status] || "border-gray-500 bg-gray-200 text-gray-700"
}`}
>
{petStatusLabels[status]}
</span>
)
}
Loading
Loading