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
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function RootLayout({
}>) {
return (
<html
lang="en"
lang="pt-BR"
suppressHydrationWarning
className={cn(
"antialiased",
Expand Down
61 changes: 61 additions & 0 deletions src/app/pets/[id]/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { renderWithProviders } from "@/shared/test-utils/custom-render"
import Page from "./page"
import { screen } from "@testing-library/react"
import React from "react"
import { petResponseMock } from "@/shared/test-utils/mocks/pets-mocks"
import { usePetPageViewModel } from "@/features/pets"

jest.mock("@/features/pets", () => ({
...jest.requireActual("@/features/pets"),
usePetPageViewModel: jest.fn(),
}))

const baseHookMock = {
pet: petResponseMock,
isLoading: false,
}

const mockParams = { id: petResponseMock.id }

describe("Pet Details Page", () => {
beforeEach(() => {
jest.clearAllMocks()
React.use = jest.fn().mockReturnValue(mockParams)
;(usePetPageViewModel as jest.Mock).mockReturnValue(baseHookMock)
})

it("should display the title and subtitle", () => {
renderWithProviders(<Page params={Promise.resolve(mockParams)} />)

expect(
screen.getByRole("heading", { name: /detalhes do pet/i })
).toBeInTheDocument()
expect(screen.getByText(/visualização completa/i)).toBeInTheDocument()
})

it("should display the edit and delete buttons", () => {
renderWithProviders(<Page params={Promise.resolve(mockParams)} />)

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

it("should render the PetDetails component", () => {
renderWithProviders(<Page params={Promise.resolve(mockParams)} />)

expect(
screen.getByRole("heading", { level: 3, name: /informações do pet/i })
).toBeInTheDocument()
expect(screen.queryByTestId("page-loader")).not.toBeInTheDocument()
})

it("should render the Loader page when loading", () => {
;(usePetPageViewModel as jest.Mock).mockReturnValue({
...baseHookMock,
isLoading: true,
})
renderWithProviders(<Page params={Promise.resolve(mockParams)} />)

expect(screen.getByTestId("page-loader")).toBeInTheDocument()
})
})
38 changes: 38 additions & 0 deletions src/app/pets/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use client"

import {
PetActionButtons,
PetDetails,
usePetPageViewModel,
} from "@/features/pets"
import { PageLayout, PageLoader } from "@/shared/components"
import React from "react"

interface PageProps {
params: Promise<{ id: string }>
}

export default function Page({ params }: PageProps) {
const { id } = React.use(params)
const { pet, isLoading } = usePetPageViewModel(id)

return (
<>
<PageLoader isLoading={isLoading} />

<PageLayout
title="Detalhes do Pet"
subtitle="Visualização completa"
headerActions={
<PetActionButtons
containerClassName="flex-col md:flex-row"
infoButtonClassName="self-center text-sm p-4 md:p-5 md:text-md"
deleteButtonClassName="self-center text-sm p-4 md:p-5 md:text-md"
/>
}
>
<PetDetails pet={pet} />
</PageLayout>
</>
)
}
10 changes: 5 additions & 5 deletions src/app/pets/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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"
import { usePetsPageViewModel } from "@/features/pets"

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

Expand All @@ -19,7 +19,7 @@ const baseHookMock = {

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

it("should render PetListHeader", () => {
Expand All @@ -34,7 +34,7 @@ describe("Pets Page", () => {
})

it('should display "nenhum pet foi encontrado" when the list is empty', () => {
;(usePetsListPageViewModel as jest.Mock).mockReturnValue({
;(usePetsPageViewModel as jest.Mock).mockReturnValue({
...baseHookMock,
pagePetCurrent: { ...petPageMock, content: [] },
})
Expand All @@ -44,7 +44,7 @@ describe("Pets Page", () => {
})

it("should display only the loader when loading and not render any pet card", () => {
;(usePetsListPageViewModel as jest.Mock).mockReturnValue({
;(usePetsPageViewModel as jest.Mock).mockReturnValue({
...baseHookMock,
isLoading: true,
})
Expand Down
8 changes: 2 additions & 6 deletions src/app/pets/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"use client"

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

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

return (
<section className="flex w-full flex-col px-10 py-8 lg:max-w-7xl">
Expand Down
7 changes: 5 additions & 2 deletions src/features/pets/api/pets-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ export const petsApi = {
create: async (pet: PetSchema): Promise<void> => {
await api.post(BASE_URL, pet)
},

get: async (page: number, size: number): Promise<PageResponse<Pet>> => {
get: async (id: string): Promise<Pet> => {
const { data } = await api.get(`${BASE_URL}/${id}`)
return data
},
getAll: async (page: number, size: number): Promise<PageResponse<Pet>> => {
const { data } = await api.get(BASE_URL, {
params: { page, size },
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DeleteButton, InfoButton } from "@/shared/components"
import { cn } from "@/shared/lib/utils"
import { Edit, Trash2 } from "lucide-react"

interface PetActionButtonsProps {
containerClassName?: string
infoButtonClassName?: string
deleteButtonClassName?: string
}

export function PetActionButtons({
containerClassName,
infoButtonClassName,
deleteButtonClassName,
}: PetActionButtonsProps) {
return (
<div className={cn("flex gap-3", containerClassName)}>
<InfoButton className={infoButtonClassName}>
<Edit size={16} /> Editar
</InfoButton>
<DeleteButton className={deleteButtonClassName}>
<Trash2 size={16} /> Excluir
</DeleteButton>
</div>
)
}
38 changes: 16 additions & 22 deletions src/features/pets/components/pet-card/PetCard.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { Pet } from "../../types/pets.types"
import { ContainerLayout, DeleteButton, InfoButton } from "@/shared/components"
import { Edit, Trash2, PawPrint } from "lucide-react"
import { ContainerLayout, ImagePlaceholder } from "@/shared/components"
import { PetStatusBadge } from "../pet-status-badge/PetStatusBadge"
import { PetTagBadge } from "../pet-tag-badge/PetTagBadge"
import { PetActionButtons } from "../pet-action-buttons/PetActionButtons"
import { useNavigate } from "@/shared/hooks/useNavigate"

interface PetCardProps {
pet: Pet
}

export function PetCard({ pet }: PetCardProps) {
const { toPet } = useNavigate()

return (
<ContainerLayout
key={pet.id}
className="flex flex-col gap-1 px-3 py-4 duration-100 hover:border-2 hover:border-primary"
className="flex cursor-pointer flex-col gap-1 px-3 py-4 duration-100 hover:border-2 hover:border-primary"
onClick={() => toPet(pet.id)}
>
<div className="mb-2 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>
<ImagePlaceholder className="mb-4 h-40" />

<div className="mb-3 flex flex-col gap-0.5">
<div className="mb-4 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} />
Expand All @@ -27,27 +30,18 @@ export function PetCard({ pet }: PetCardProps) {

<div className="flex h-15 flex-wrap items-start gap-x-2 gap-y-2 overflow-hidden text-ellipsis whitespace-nowrap">
{pet.tags.slice(0, 4).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>
<PetTagBadge key={tag} tag={tag} />
))}
{pet.tags.length > 4 && (
<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>
<PetActionButtons
containerClassName="mt-2 border-t-2 pt-4"
infoButtonClassName="flex-1 text-sm"
deleteButtonClassName="text-sm"
/>
</ContainerLayout>
)
}
54 changes: 54 additions & 0 deletions src/features/pets/components/pet-details/PetDetails.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { render, screen } from "@testing-library/react"
import { PetDetails } from "./PetDetails"
import { petResponseMock } from "@/shared/test-utils/mocks/pets-mocks"

jest.mock("@/shared/components", () => ({
...jest.requireActual("@/shared/components"),
ImagePlaceholder: () => <div data-testid="image-placeholder" />,
}))

describe(PetDetails.name, () => {
it("should display content section titles", () => {
render(<PetDetails pet={petResponseMock} />)

expect(screen.getByText("Fotos do Pet")).toBeInTheDocument()
expect(screen.getByText("Informações do Pet")).toBeInTheDocument()
})

it("should display the subtitles in the 'Informações do Pet' section", () => {
render(<PetDetails pet={petResponseMock} />)

expect(screen.getByText("ID")).toBeInTheDocument()
expect(screen.getByText("Nome")).toBeInTheDocument()
expect(screen.getByText("Espécie")).toBeInTheDocument()
expect(screen.getByText("Status")).toBeInTheDocument()
expect(screen.getByText("Tags")).toBeInTheDocument()
})

it("should display 4 image placeholders", () => {
render(<PetDetails pet={petResponseMock} />)
expect(screen.getAllByTestId("image-placeholder")).toHaveLength(4)
})

it("should display pet information (id, name, species and status)", () => {
render(<PetDetails pet={petResponseMock} />)

expect(screen.getByText(petResponseMock.id)).toBeInTheDocument()
expect(screen.getByText(petResponseMock.name)).toBeInTheDocument()
expect(screen.getByText(petResponseMock.species)).toBeInTheDocument()
expect(screen.getByText(/disponível/i)).toBeInTheDocument()
})

it("should display all pet tags", () => {
render(<PetDetails pet={petResponseMock} />)
petResponseMock.tags.forEach((tag) => {
expect(screen.getByText(tag)).toBeInTheDocument()
})
})

it('should display "Nenhuma tag foi cadastrada" when there are no tags', () => {
const pet = { ...petResponseMock, tags: [] }
render(<PetDetails pet={pet} />)
expect(screen.getByText(/nenhuma tag foi cadastrada/i)).toBeInTheDocument()
})
})
59 changes: 59 additions & 0 deletions src/features/pets/components/pet-details/PetDetails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ContainerLayout, ImagePlaceholder } from "@/shared/components"
import { Pet } from "../../types/pets.types"
import { PetStatusBadge } from "../pet-status-badge/PetStatusBadge"
import { PetTagBadge } from "../pet-tag-badge/PetTagBadge"

interface PetDetailsProps {
pet: Pet | undefined
}

export function PetDetails({ pet }: PetDetailsProps) {
if (!pet) return

return (
<div className="flex w-full flex-col gap-6 md:max-w-5xl md:flex-row">
<ContainerLayout title="Fotos do Pet" className="w-full">
<ImagePlaceholder className="min-h-50 md:min-h-80" />
<div className="mt-5 hidden w-full justify-between gap-5 md:flex md:min-h-30">
<ImagePlaceholder className="flex-1" />
<ImagePlaceholder className="flex-1" />
<ImagePlaceholder className="flex-1" />
</div>
</ContainerLayout>
<ContainerLayout title="Informações do Pet" className="w-full md:w-11/12">
<div className="flex flex-col gap-4">
<div>
<h4 className="mb-0.5 text-gray-700">ID</h4>
<p className="font-bold">{pet.id}</p>
</div>
<div>
<h4 className="mb-0.5 text-gray-700">Nome</h4>
<p className="font-bold">{pet.name}</p>
</div>
<div>
<h4 className="mb-0.5 text-gray-700">Espécie</h4>
<p className="font-bold">{pet.species}</p>
</div>
<div>
<h4 className="mb-1.5 text-gray-700">Status</h4>
<PetStatusBadge status={pet.status} />
</div>
<div>
<h4 className="text-gray-700">Tags</h4>
{pet.tags.length === 0 ? (
<span className="mt-1.5 pl-0.5 text-xs text-gray-500 italic">
Nenhuma tag foi cadastrada
</span>
) : (
<div className="mt-2 flex flex-wrap gap-2">
{pet.tags.map((tag) => (
<PetTagBadge key={tag} tag={tag} />
))}
</div>
)}
</div>
</div>
</ContainerLayout>
</div>
)
}
Loading
Loading