-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rayane barbosa/testes unitarios #86
Changes from 11 commits
575ec5c
8356f79
72f7f8f
9b1bf23
2bd0bdf
27fc4fe
4a0f8c8
fe84d9b
8d21645
3e69ce3
34ccaa3
b4d3065
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { socials } from './socials' | ||
|
||
describe('socials', () => { | ||
it('should have at least one social link', () => { | ||
expect(socials.length).toBeGreaterThan(0) | ||
}) | ||
|
||
it('should have valid href values', () => { | ||
socials.forEach((social) => { | ||
expect(social.href).toMatch(/^https?:\/\/[^\s/$.?#].[^\s]*$/) | ||
}) | ||
}) | ||
|
||
it('should have valid icon components', () => { | ||
socials.forEach((social) => { | ||
expect(social.icon).toBeDefined() | ||
expect(typeof social.icon).toBe('object') | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { AppHeader } from '@/components/header' | ||
import '@testing-library/jest-dom' | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import { BrowserRouter as Router } from 'react-router-dom' | ||
|
||
jest.mock('@/hooks/useIsMobile', () => ({ | ||
useIsMobile: () => true, | ||
})) | ||
|
||
jest.mock('@uidotdev/usehooks', () => ({ | ||
useWindowScroll: () => [{ y: 0 }], | ||
})) | ||
|
||
describe('AppHeader', () => { | ||
it('renders without crashing', () => { | ||
render( | ||
<Router> | ||
<AppHeader /> | ||
</Router>, | ||
) | ||
}) | ||
it('displays the hamburger menu icon on mobile', () => { | ||
render( | ||
<Router> | ||
<AppHeader /> | ||
</Router>, | ||
) | ||
const hamburgerMenuIcon = screen.getByRole('button') | ||
expect(hamburgerMenuIcon).toBeInTheDocument() | ||
}) | ||
|
||
it('displays the navigation menu when the hamburger menu icon is clicked', () => { | ||
render( | ||
<Router> | ||
<AppHeader /> | ||
</Router>, | ||
) | ||
const hamburgerMenuIcon = screen.getByRole('button') | ||
fireEvent.click(hamburgerMenuIcon) | ||
const navMenu = screen.getByRole('navigation') | ||
expect(navMenu).toBeInTheDocument() | ||
}) | ||
|
||
it('displays the "Voltar à home" link when not on the home page', () => { | ||
window.history.pushState({}, '', '/not-home') | ||
render( | ||
<Router> | ||
<AppHeader /> | ||
</Router>, | ||
) | ||
const link = screen.getByText('Voltar à home') | ||
expect(link).toBeInTheDocument() | ||
}) | ||
|
||
it('displays the "Cadastrar um pet" link when not on the subscription page', () => { | ||
window.history.pushState({}, '', '/not-subscription') | ||
render( | ||
<Router> | ||
<AppHeader /> | ||
</Router>, | ||
) | ||
const link = screen.getByText('Cadastrar um pet') | ||
expect(link).toBeInTheDocument() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { JoinUs } from './index' | ||
|
||
describe('JoinUs component', () => { | ||
beforeEach(() => { | ||
render(<JoinUs />) | ||
}) | ||
|
||
it('renders without crashing', () => { | ||
const joinUsElement = screen.getByText(/Faça parte da nossa equipe/i) | ||
expect(joinUsElement).toBeInTheDocument() | ||
}) | ||
|
||
it('contains a Paw component', () => { | ||
const pawElement = screen.getByTestId('join-us-paw') | ||
expect(pawElement).toBeInTheDocument() | ||
}) | ||
|
||
it('contains a Button component', () => { | ||
const buttonElement = screen.getByTestId('join-us-button') | ||
expect(buttonElement).toBeInTheDocument() | ||
}) | ||
|
||
it('contains a link to the Discord server', () => { | ||
const linkElement = screen.getByRole('link') | ||
expect(linkElement).toHaveAttribute( | ||
'href', | ||
'https://discord.com/invite/Pr2BZmUG', | ||
) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { ChevronRight } from 'lucide-react' | ||
import { Paw } from '../paw' | ||
import { Button } from '../ui/button' | ||
import { Button } from '../ui/components/button/button' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acredito que seja melhor ser amenas /ui/button (o arquivo do botão estar no index.tsx) e remover a pasta de components, já estamos dentro dela, não faz sentido ter components dentro de components. Já é entendível que itens dentro da ui são componentes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eu atualizei dessa forma por se tratar de uma boa prática na escrita de testes unitários de acordo com a arquitetura component-based-architecture |
||
|
||
export function JoinUs() { | ||
return ( | ||
<section className="bg-primary-500 py-10 text-white lg:py-20"> | ||
<div className="container"> | ||
<Paw className="mx-auto mb-2.5 size-10 lg:mb-11 lg:h-[68px] lg:w-[72px]" /> | ||
<Paw | ||
data-testid="join-us-paw" | ||
className="mx-auto mb-2.5 size-10 lg:mb-11 lg:h-[68px] lg:w-[72px]" | ||
/> | ||
|
||
<h2 className="mb-6 text-balance text-center text-2xl font-bold lg:mb-11 lg:text-5xl"> | ||
Faça parte da nossa equipe | ||
|
@@ -20,6 +23,7 @@ export function JoinUs() { | |
</p> | ||
|
||
<Button | ||
data-testid="join-us-button" | ||
asChild | ||
className="mx-auto mt-7 flex h-auto w-full gap-1 bg-white px-6 py-3 text-base font-semibold text-primary hover:bg-white/80 lg:mt-14 lg:w-fit" | ||
> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { OthersPlatforms } from '.' | ||
|
||
describe('OthersPlatforms', () => { | ||
test('renders without crashing', () => { | ||
render(<OthersPlatforms />) | ||
expect(screen).not.toBeNull() | ||
}) | ||
|
||
test('contains the heading', () => { | ||
render(<OthersPlatforms />) | ||
const heading = screen.getByText(/Explore Mais Opções/i) | ||
expect(heading).toBeInTheDocument() | ||
}) | ||
|
||
test('contains the paragraph text', () => { | ||
render(<OthersPlatforms />) | ||
const paragraph = screen.getByText(/Se você ainda não encontrou seu pet/i) | ||
expect(paragraph).toBeInTheDocument() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { PetCard } from '.' | ||
|
||
describe('PetCard', () => { | ||
const mockProps = { | ||
id: 1, | ||
imageSrc: 'testImageSrc', | ||
title: 'testTitle', | ||
description: 'testDescription', | ||
specie: 'testSpecie', | ||
size: 'testSize', | ||
color: 'testColor', | ||
} | ||
it('displays the correct title', () => { | ||
render(<PetCard {...mockProps} />) | ||
expect(screen.getByText(mockProps.title)).toBeInTheDocument() | ||
}) | ||
|
||
it('displays the correct description', () => { | ||
render(<PetCard {...mockProps} />) | ||
expect(screen.getByText(mockProps.description)).toBeInTheDocument() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ import { | |
DialogPortal, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '@/components/ui/dialog' | ||
} from '@/components/ui/components/dialog/dialog' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acredito que seja melhor ser amenas /ui/dialog (o arquivo do dialog estar no index.tsx) e remover a pasta de components, já estamos dentro dela, não faz sentido ter components dentro de components. Já é entendível que itens dentro da ui são componentes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eu atualizei dessa forma por se tratar de uma boa prática na escrita de testes unitários de acordo com a arquitetura component-based-architecture |
||
import { ChevronRight, ZoomIn } from 'lucide-react' | ||
import { Link } from 'react-router-dom' | ||
import { Button } from '../ui/button' | ||
import { Button } from '../ui/components/button/button' | ||
|
||
interface PetCardProps { | ||
id: number | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { Button } from './button' | ||
|
||
describe('Button', () => { | ||
it('renders correctly with default props', () => { | ||
render(<Button />) | ||
const button = screen.getByRole('button') | ||
expect(button).toBeInTheDocument() | ||
expect(button).toHaveClass( | ||
'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50', | ||
) | ||
}) | ||
|
||
it('renders correctly with variant prop', () => { | ||
render(<Button variant="destructive" />) | ||
const button = screen.getByRole('button') | ||
expect(button).toHaveClass( | ||
'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90', | ||
) | ||
}) | ||
|
||
it('renders correctly with size prop', () => { | ||
render(<Button size="lg" />) | ||
const button = screen.getByRole('button') | ||
expect(button).toHaveClass('h-10 rounded-md px-8') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from './dialog' | ||
|
||
describe('Dialog Component', () => { | ||
it('renders without crashing', () => { | ||
render( | ||
<Dialog> | ||
<DialogTrigger>Open Dialog</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Dialog Title</DialogTitle> | ||
<DialogClose>Close</DialogClose> | ||
</DialogHeader> | ||
<DialogDescription>Dialog Description</DialogDescription> | ||
<DialogFooter> | ||
<button>Cancel</button> | ||
<button>Save</button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog>, | ||
) | ||
|
||
expect(screen.findByRole('dialog')).toBeDefined() | ||
}) | ||
}) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. colocaria como index.tsx pra quando for usar não ficar um import duplicado. "/ui/components/form/form" e removeria o components já que já tem uma pasta mãe que se chama components. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eu atualizei dessa forma por se tratar de uma boa prática na escrita de testes unitários de acordo com a arquitetura component-based-architecture Nos projetos é até preferivel você manter separação de componentes. No caso dos componentes UI os separei de forma que seja mais organizavel observar e criar os testes unitários e testes de componentes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { render } from '@testing-library/react' | ||
import { ImageWithLoader } from './image-with-loader' | ||
import '@testing-library/jest-dom' | ||
|
||
describe('ImageWithLoader', () => { | ||
it('renders without errors', () => { | ||
render(<ImageWithLoader src="test.jpg" alt="Test Image" />) | ||
}) | ||
|
||
it('renders with the correct alt text', () => { | ||
const altText = 'Test Image' | ||
const { getByAltText } = render( | ||
<ImageWithLoader src="test.jpg" alt={altText} />, | ||
) | ||
expect(getByAltText(altText)).toBeInTheDocument() | ||
}) | ||
|
||
it('applies the provided className', () => { | ||
const className = 'custom-class' | ||
const { container } = render( | ||
<ImageWithLoader src="test.jpg" alt="Test Image" className={className} />, | ||
) | ||
expect(container.firstChild).toHaveClass(className) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import '@testing-library/jest-dom' | ||
import { render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
import { MaskedInput } from './input-masked' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acredito que seja melhor e mais fácil deixar separado em pastas (input, input-masked e textarea) cada um com a sua pasta ao invés de todos ficarem dentro de input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adicionei-os em input por se tratarem de entradas de texto (mesmo segmento) assim sendo mais fácil sua leitura e compreensão |
||
|
||
describe('MaskedInput', () => { | ||
it('renders without errors', () => { | ||
render(<MaskedInput />) | ||
expect(screen.getByRole('textbox')).toBeInTheDocument() | ||
}) | ||
it('renders with custom class name', () => { | ||
render(<MaskedInput className="custom-input" />) | ||
expect(screen.getByRole('textbox')).toHaveClass('custom-input') | ||
}) | ||
it('forwards ref to the input element', () => { | ||
const ref = React.createRef<HTMLInputElement>() | ||
render(<MaskedInput ref={ref} />) | ||
expect(ref.current).toBeInstanceOf(HTMLInputElement) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import '@testing-library/jest-dom' | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import React from 'react' | ||
import { Input } from './input' | ||
|
||
describe('Input', () => { | ||
test('renders without errors', () => { | ||
render(<Input />) | ||
expect(screen.getByRole('textbox')).toBeInTheDocument() | ||
}) | ||
|
||
test('renders with custom class name', () => { | ||
render(<Input className="custom-input" />) | ||
expect(screen.getByRole('textbox')).toHaveClass('custom-input') | ||
}) | ||
|
||
test('forwards ref to the input element', () => { | ||
const ref = React.createRef<HTMLInputElement>() | ||
render(<Input ref={ref} />) | ||
expect(ref.current).toBeInstanceOf(HTMLInputElement) | ||
}) | ||
|
||
test('triggers onChange event', () => { | ||
const handleChange = jest.fn() | ||
render(<Input onChange={handleChange} />) | ||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test' } }) | ||
expect(handleChange).toHaveBeenCalledTimes(1) | ||
expect(handleChange).toHaveBeenCalledWith(expect.any(Object)) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acredito que seja melhor ser amenas /ui/button (o arquivo do botão estar no index.tsx) e remover a pasta de components, já estamos dentro dela, não faz sentido ter components dentro de components. Já é entendível que itens dentro da ui são componentes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eu atualizei dessa forma por se tratar de uma boa prática na escrita de testes unitários de acordo com a arquitetura component-based-architecture
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
O projeto já está em arquitetura baseada em componentes, só não acredito que ter que importar form/form seja um modelo mental bom.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fora que também já estamos na pasta de components, não faz sentido ter outra. Se não daqui a pouco vamos ter ui/components/form/components...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blz. Vou realizar os ajustes