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
60 changes: 60 additions & 0 deletions src/app/pets/[id]/editar/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useUpdatePetPageViewModel } from "@/features/pets"
import { renderWithProviders } from "@/shared/test-utils/custom-render"
import Page from "./page"
import { screen } from "@testing-library/react"
import { speciesOptionsMock } from "@/shared/test-utils/mocks/species-mocks"
import { tagsOptionsMock } from "@/shared/test-utils/mocks/tags-mocks"
import { petResponseMock } from "@/shared/test-utils/mocks/pets-mocks"
import React from "react"

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

jest.mock("@/shared/components", () => ({
...jest.requireActual("@/shared/components"),
InputField: () => <div data-testid="input-field-mock" />,
SelectField: () => <div data-testid="select-field-mock" />,
CheckboxField: () => <div data-testid="checkbox-field-mock" />,
}))

const baseHookMock = {
speciesOptions: speciesOptionsMock,
tagsOptions: tagsOptionsMock,
isLoading: false,
handleUpdatePet: jest.fn(),
formMethods: {
handleSubmit: jest.fn(),
formState: {
isValid: true,
},
},
}

const mockParams = { id: petResponseMock.id }

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

it("should render the page title", () => {
renderWithProviders(<Page params={Promise.resolve(mockParams)} />)
expect(
screen.getByRole("heading", { name: /editar pet/i })
).toBeInTheDocument()
})

it("should render the PageLoader when isLoading is true", () => {
;(useUpdatePetPageViewModel 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]/editar/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use client"

import { PetForm, useUpdatePetPageViewModel } from "@/features/pets"
import { ContainerLayout, 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 {
formMethods,
speciesOptions,
tagsOptions,
isLoading,
handleUpdatePet,
} = useUpdatePetPageViewModel(id)

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

<PageLayout title="Editar Pet" subtitle="Atualize as informações do pet">
<ContainerLayout className="w-full md:max-w-150">
<PetForm
formMethods={formMethods}
speciesOptions={speciesOptions}
tagsOptions={tagsOptions}
onSubmit={handleUpdatePet}
btnName="Atualizar Pet"
/>
</ContainerLayout>
</PageLayout>
</>
)
}
1 change: 1 addition & 0 deletions src/app/pets/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function Page({ params }: PageProps) {
subtitle="Visualização completa"
headerActions={
<PetActionButtons
petId={id}
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"
Expand Down
17 changes: 6 additions & 11 deletions src/app/pets/novo/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const baseHookMock = {
tagsOptionsMock,
isLoading: false,
handleCreatePet: jest.fn(),
isCreating: false,
formMethods: {
handleSubmit: jest.fn(),
formState: {
isValid: true,
},
},
}

describe("Create Page", () => {
Expand All @@ -46,14 +51,4 @@ describe("Create Page", () => {
renderWithProviders(<Page />)
expect(screen.getByTestId("page-loader")).toBeInTheDocument()
})

it("should render the PageLoader when isCreating is true", () => {
;(useCreatePetPageViewModel as jest.Mock).mockReturnValue({
...baseHookMock,
isCreating: true,
})

renderWithProviders(<Page />)
expect(screen.getByTestId("page-loader")).toBeInTheDocument()
})
})
6 changes: 4 additions & 2 deletions src/app/pets/novo/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import { ContainerLayout, PageLayout, PageLoader } from "@/shared/components"

export default function Page() {
const {
formMethods,
speciesOptions,
tagsOptions,
isLoading,
handleCreatePet,
isCreating,
} = useCreatePetPageViewModel()

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

<PageLayout
title="Adicionar Novo Pet"
subtitle="Preencha os dados do novo pet"
>
<ContainerLayout className="w-full md:max-w-150">
<PetForm
formMethods={formMethods}
speciesOptions={speciesOptions}
tagsOptions={tagsOptions}
onSubmit={handleCreatePet}
btnName="Cadastrar Pet"
/>
</ContainerLayout>
</PageLayout>
Expand Down
4 changes: 4 additions & 0 deletions src/features/pets/api/pets-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const petsApi = {
create: async (pet: PetSchema): Promise<void> => {
await api.post(BASE_URL, pet)
},
update: async (id: string, pet: PetSchema): Promise<Pet> => {
const { data } = await api.put(`${BASE_URL}/${id}`, pet)
return data
},
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
@@ -1,21 +1,29 @@
import { DeleteButton, InfoButton } from "@/shared/components"
import { useNavigate } from "@/shared/hooks/useNavigate"
import { cn } from "@/shared/lib/utils"
import { Edit, Trash2 } from "lucide-react"

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

export function PetActionButtons({
petId,
containerClassName,
infoButtonClassName,
deleteButtonClassName,
}: PetActionButtonsProps) {
const { toEditPet } = useNavigate()

return (
<div className={cn("flex gap-3", containerClassName)}>
<InfoButton className={infoButtonClassName}>
<InfoButton
className={infoButtonClassName}
onClick={() => toEditPet(petId)}
>
<Edit size={16} /> Editar
</InfoButton>
<DeleteButton className={deleteButtonClassName}>
Expand Down
19 changes: 15 additions & 4 deletions src/features/pets/components/pet-card/PetCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,34 @@ describe(PetCard.name, () => {
})

it("should display exactly 4 tags", () => {
const tags = ["vacinado", "treinado", "amigável", "tímido"]
const tags = [
{ id: "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d", name: "Vacinado" },
{ id: "2b3c4d5e-6f7a-8b9c-0d1e-2f3a4b5c6d7e", name: "Treinado" },
{ id: "3c4d5e6f-7a8b-9c0d-1e2f-3a4b5c6d7e8f", name: "Amigável" },
{ id: "4d5e6f7a-8b9c-0d1e-2f3a-4b5c6d7e8f9a", name: "Tímido" },
]
const pet = { ...petResponseMock, tags }
renderWithProviders(<PetCard pet={pet} />)

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

it("should display ellipsis when more than 4 tags", () => {
const tags = ["vacinado", "treinado", "amigável", "tímido", "sociável"]
const tags = [
{ id: "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d", name: "Vacinado" },
{ id: "2b3c4d5e-6f7a-8b9c-0d1e-2f3a4b5c6d7e", name: "Treinado" },
{ id: "3c4d5e6f-7a8b-9c0d-1e2f-3a4b5c6d7e8f", name: "Amigável" },
{ id: "4d5e6f7a-8b9c-0d1e-2f3a-4b5c6d7e8f9a", name: "Tímido" },
{ id: "9c0d1e2f-3a4b-5c6d-7e8f-9a0b1c2d3e4f", name: "Sociável" },
]
const pet = { ...petResponseMock, tags }
render(<PetCard pet={pet} />)

tags.slice(0, 4).forEach((tag) => {
expect(screen.getByText(tag)).toBeInTheDocument()
expect(screen.getByText(tag.name)).toBeInTheDocument()
})
expect(screen.getByText("...")).toBeInTheDocument()
})
Expand Down
37 changes: 19 additions & 18 deletions src/features/pets/components/pet-card/PetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@ export function PetCard({ pet }: PetCardProps) {
return (
<ContainerLayout
key={pet.id}
className="flex cursor-pointer flex-col gap-1 px-3 py-4 duration-100 hover:border-2 hover:border-primary"
onClick={() => toPet(pet.id)}
className="flex flex-col gap-1 px-3 py-4 duration-100 hover:border-2 hover:border-primary"
>
<ImagePlaceholder className="mb-4 h-40" />
<a onClick={() => toPet(pet.id)} className="cursor-pointer">
<ImagePlaceholder className="mb-4 h-40" />

<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} />
<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} />
</div>
<p className="text-2xlg text-gray-500 italic">{pet.species.name}</p>
</div>
<p className="text-2xlg text-gray-500 italic">{pet.species}</p>
</div>

<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) => (
<PetTagBadge key={tag} tag={tag} />
))}
{pet.tags.length > 4 && (
<span className="text-xs font-bold text-gray-400">...</span>
)}
</div>

<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) => (
<PetTagBadge key={tag.id} tag={tag.name} />
))}
{pet.tags.length > 4 && (
<span className="text-xs font-bold text-gray-400">...</span>
)}
</div>
</a>
<PetActionButtons
petId={pet.id}
containerClassName="mt-2 border-t-2 pt-4"
infoButtonClassName="flex-1 text-sm"
deleteButtonClassName="text-sm"
Expand Down
4 changes: 2 additions & 2 deletions src/features/pets/components/pet-details/PetDetails.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ describe(PetDetails.name, () => {

expect(screen.getByText(petResponseMock.id)).toBeInTheDocument()
expect(screen.getByText(petResponseMock.name)).toBeInTheDocument()
expect(screen.getByText(petResponseMock.species)).toBeInTheDocument()
expect(screen.getByText(petResponseMock.species.name)).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()
expect(screen.getByText(tag.name)).toBeInTheDocument()
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/features/pets/components/pet-details/PetDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function PetDetails({ pet }: PetDetailsProps) {
</div>
<div>
<h4 className="mb-0.5 text-gray-700">Espécie</h4>
<p className="font-bold">{pet.species}</p>
<p className="font-bold">{pet.species.name}</p>
</div>
<div>
<h4 className="mb-1.5 text-gray-700">Status</h4>
Expand All @@ -47,7 +47,7 @@ export function PetDetails({ pet }: PetDetailsProps) {
) : (
<div className="mt-2 flex flex-wrap gap-2">
{pet.tags.map((tag) => (
<PetTagBadge key={tag} tag={tag} />
<PetTagBadge key={tag.id} tag={tag.name} />
))}
</div>
)}
Expand Down
Loading
Loading