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
22 changes: 22 additions & 0 deletions src/app/layout.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { screen } from "@testing-library/react"
import RootLayout from "./layout"
import { renderWithProviders } from "@/shared/test-utils/custom-render"

describe(RootLayout.name, () => {
it("should render the Header, Footer and the internal content", () => {
const title = "Test"

renderWithProviders(
<RootLayout>
<h1>{title}</h1>
</RootLayout>
)
expect(
screen.getByText(/Sistema de Gerenciamento de Pets/i)
).toBeInTheDocument()
expect(
screen.getByText(/Todos os direitos reservados/i)
).toBeInTheDocument()
expect(screen.getByText(title)).toBeInTheDocument()
})
})
3 changes: 2 additions & 1 deletion src/app/pets/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface PageProps {

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

return (
<>
Expand All @@ -25,6 +25,7 @@ export default function Page({ params }: PageProps) {
subtitle="Visualização completa"
headerActions={
<PetActionButtons
onDelete={handleDeletePet}
petId={id}
containerClassName="flex-col md:flex-row"
infoButtonClassName="self-center text-sm p-4 md:p-5 md:text-md"
Expand Down
54 changes: 35 additions & 19 deletions src/app/pets/page.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
"use client"

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

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

return (
<section className="flex w-full flex-col px-10 py-8 lg:max-w-7xl">
<PetListHeader totalPets={pagePetCurrent.totalElements} />
<>
<PageLoader isLoading={isDeleting} />

<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}
/>
<section className="flex w-full flex-col px-10 py-8 lg:max-w-7xl">
<PetListHeader totalPets={pagePetCurrent.totalElements} />

{!isLoading && <PetList pets={pagePetCurrent.content} />}
<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}
/>

<CustomPagination
page={page}
totalPages={pagePetCurrent.totalPages}
onPageChange={handleChangePage}
isLoading={isLoading}
/>
</div>
</section>
{!isLoading && (
<PetList pets={pagePetCurrent.content} onDelete={handleDeletePet} />
)}

<CustomPagination
page={page}
totalPages={pagePetCurrent.totalPages}
onPageChange={handleChangePage}
isLoading={isLoading}
/>
</div>
</section>
</>
)
}
40 changes: 37 additions & 3 deletions src/features/pets/api/pets-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { petsApi } from "./pets-api"
import { api } from "@/shared/lib/api.config"
import { petMock } from "@/shared/test-utils/mocks/pets-mocks"
import { petMock, petResponseMock } from "@/shared/test-utils/mocks/pets-mocks"

const PET_ID = petResponseMock.id
const PET_URL = "/pets"
const PET_URL_ID = `${PET_URL}/${PET_ID}`

describe("petsApi", () => {
afterEach(() => {
Expand All @@ -9,9 +13,39 @@ describe("petsApi", () => {

it("should call api.post with the correct data when creating a pet", async () => {
;(api.post as jest.Mock).mockResolvedValue(undefined)

await petsApi.create(petMock)

expect(api.post).toHaveBeenCalledWith("/pets", petMock)
expect(api.post).toHaveBeenCalledWith(PET_URL, petMock)
})

it("should call api.put with the correct data when updating a pet", async () => {
;(api.put as jest.Mock).mockResolvedValue({ data: petMock })
await petsApi.update(PET_ID, petMock)

expect(api.put).toHaveBeenCalledWith(PET_URL_ID, petMock)
})

it("should call api.delete with the correct id when deleting a pet", async () => {
;(api.delete as jest.Mock).mockResolvedValue({ data: petMock })
await petsApi.delete(PET_ID)

expect(api.delete).toHaveBeenCalledWith(PET_URL_ID)
})

it("should call api.get with the correct id when getting a pet", async () => {
;(api.get as jest.Mock).mockResolvedValue({ data: petMock })
await petsApi.get(PET_ID)

expect(api.get).toHaveBeenCalledWith(PET_URL_ID)
})

it("should call api.get with correct params when getting all pets", async () => {
const page = 0
const size = 6

;(api.get as jest.Mock).mockResolvedValue({ data: [petMock] })
await petsApi.getAll(page, size)

expect(api.get).toHaveBeenCalledWith(PET_URL, { params: { page, size } })
})
})
3 changes: 3 additions & 0 deletions src/features/pets/api/pets-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const petsApi = {
const { data } = await api.put(`${BASE_URL}/${id}`, pet)
return data
},
delete: async (id: string): Promise<void> => {
await api.delete(`${BASE_URL}/${id}`)
},
get: async (id: string): Promise<Pet> => {
const { data } = await api.get(`${BASE_URL}/${id}`)
return data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import { DeleteButton, InfoButton } from "@/shared/components"
import { useNavigate } from "@/shared/hooks/useNavigate"
import { cn } from "@/shared/lib/utils"
import { Edit, Trash2 } from "lucide-react"
import { PetDeleteDialog } from "../pet-delete-dialog/PetDeleteDialog"

interface PetActionButtonsProps {
petId: string
onDelete: (id: string) => void
containerClassName?: string
infoButtonClassName?: string
deleteButtonClassName?: string
}

export function PetActionButtons({
petId,
onDelete,
containerClassName,
infoButtonClassName,
deleteButtonClassName,
Expand All @@ -26,9 +29,16 @@ export function PetActionButtons({
>
<Edit size={16} /> Editar
</InfoButton>
<DeleteButton className={deleteButtonClassName}>
<Trash2 size={16} /> Excluir
</DeleteButton>
<PetDeleteDialog
onDelete={() => {
onDelete(petId)
}}
triggerAction={
<DeleteButton className={deleteButtonClassName}>
<Trash2 size={16} /> Excluir
</DeleteButton>
}
></PetDeleteDialog>
</div>
)
}
10 changes: 5 additions & 5 deletions src/features/pets/components/pet-card/PetCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import { renderWithProviders } from "@/shared/test-utils/custom-render"

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

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} />)
renderWithProviders(<PetCard pet={petResponseMock} onDelete={jest.fn()} />)

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} />)
renderWithProviders(<PetCard pet={pet} onDelete={jest.fn()} />)

expect(screen.queryAllByTestId("pet-tag")).toHaveLength(0)
})
Expand All @@ -34,7 +34,7 @@ describe(PetCard.name, () => {
{ id: "4d5e6f7a-8b9c-0d1e-2f3a-4b5c6d7e8f9a", name: "Tímido" },
]
const pet = { ...petResponseMock, tags }
renderWithProviders(<PetCard pet={pet} />)
renderWithProviders(<PetCard pet={pet} onDelete={jest.fn()} />)

tags.forEach((tag) => {
expect(screen.getByText(tag.name)).toBeInTheDocument()
Expand All @@ -51,7 +51,7 @@ describe(PetCard.name, () => {
{ id: "9c0d1e2f-3a4b-5c6d-7e8f-9a0b1c2d3e4f", name: "Sociável" },
]
const pet = { ...petResponseMock, tags }
render(<PetCard pet={pet} />)
render(<PetCard pet={pet} onDelete={jest.fn()} />)

tags.slice(0, 4).forEach((tag) => {
expect(screen.getByText(tag.name)).toBeInTheDocument()
Expand Down
4 changes: 3 additions & 1 deletion src/features/pets/components/pet-card/PetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { useNavigate } from "@/shared/hooks/useNavigate"

interface PetCardProps {
pet: Pet
onDelete: (id: string) => void
}

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

return (
Expand Down Expand Up @@ -39,6 +40,7 @@ export function PetCard({ pet }: PetCardProps) {
</a>
<PetActionButtons
petId={pet.id}
onDelete={onDelete}
containerClassName="mt-2 border-t-2 pt-4"
infoButtonClassName="flex-1 text-sm"
deleteButtonClassName="text-sm"
Expand Down
23 changes: 23 additions & 0 deletions src/features/pets/components/pet-delete-dialog/PetDeleteDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CustomDialog } from "@/shared/components"
import { ReactNode } from "react"

interface PetDeleteDialogProps {
triggerAction: ReactNode
onDelete: () => void
}

export function PetDeleteDialog({
triggerAction,
onDelete,
}: PetDeleteDialogProps) {
return (
<CustomDialog
title="Excluir Pet"
description="Tem certeza que deseja excluir este pet? Esta ação não poderá ser desfeita."
confirmLabel="Excluir"
cancelLabel="Cancelar"
triggerAction={triggerAction}
onConfirm={onDelete}
/>
)
}
5 changes: 3 additions & 2 deletions src/features/pets/components/pet-list/PetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { PetCard } from "../pet-card/PetCard"

interface PetListProps {
pets: Pet[]
onDelete: (id: string) => void
}

export function PetList({ pets }: PetListProps) {
export function PetList({ pets, onDelete }: PetListProps) {
if (pets.length === 0) {
return (
<div className="flex flex-col items-center justify-center gap-5 py-10">
Expand All @@ -19,7 +20,7 @@ export function PetList({ pets }: PetListProps) {
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} />
<PetCard key={pet.id} pet={pet} onDelete={onDelete} />
))}
</div>
)
Expand Down
Loading
Loading