From 1483cb6ee8f7fc249db3bd763b506fbf29917c82 Mon Sep 17 00:00:00 2001 From: Lucca Medeiros Date: Fri, 30 Aug 2024 17:21:59 -0300 Subject: [PATCH 01/21] =?UTF-8?q?Setup(#163):=20Configura=C3=A7=C3=A3o=20d?= =?UTF-8?q?o=20JEST=20no=20projeto.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.ts | 11 +++++++++++ package.json | 14 +++++++++++++- tsconfig.json | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 jest.config.ts diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 0000000..56adcff --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,11 @@ +export default { + preset: 'ts-jest', + testEnvironment: '@happy-dom/jest-environment', + transform: { + '^.+\\.tsx?$': 'ts-jest', + // process `*.tsx` files with `ts-jest` + }, + moduleNameMapper: { + '\\.(gif|ttf|eot|svg|png)$': '/test/__ mocks __/fileMock.js', + }, +}; diff --git a/package.json b/package.json index e7d16c0..2b12deb 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,16 @@ "dev": "vite", "build": "tsc && vite build", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", - "preview": "vite preview" + "preview": "vite preview", + "test": "jest" }, "dependencies": { + "@happy-dom/jest-environment": "^15.7.0", + "@testing-library/dom": "^10.4.0", "firebase": "^10.12.4", "formik": "^2.4.6", + "happy-dom": "^15.7.0", + "jest-environment-jsdom": "^29.7.0", "primeflex": "^3.3.1", "primeicons": "^7.0.0", "primereact": "^10.6.3", @@ -23,6 +28,9 @@ "zustand": "^4.5.4" }, "devDependencies": { + "@testing-library/jest-dom": "^6.5.0", + "@testing-library/react": "^16.0.0", + "@types/jest": "^29.5.12", "@types/react": "^18.2.66", "@types/react-dom": "^18.2.22", "@typescript-eslint/eslint-plugin": "^7.2.0", @@ -31,6 +39,10 @@ "eslint": "^8.57.0", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.6", + "identity-obj-proxy": "^3.0.0", + "jest": "^29.7.0", + "ts-jest": "^29.2.5", + "ts-node": "^10.9.2", "typescript": "^5.2.2", "vite": "^5.2.0" } diff --git a/tsconfig.json b/tsconfig.json index a7fc6fb..c74e8de 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,8 @@ "module": "ESNext", "skipLibCheck": true, + "types": ["node", "jest", "@testing-library/jest-dom"], + /* Bundler mode */ "moduleResolution": "bundler", "allowImportingTsExtensions": true, @@ -14,6 +16,8 @@ "noEmit": true, "jsx": "react-jsx", + "esModuleInterop": true, + /* Linting */ "strict": true, "noUnusedLocals": true, From 9600dd6d11c2e8ffe294295c86f3649829564b35 Mon Sep 17 00:00:00 2001 From: Lucca Medeiros Date: Fri, 30 Aug 2024 21:10:02 -0300 Subject: [PATCH 02/21] =?UTF-8?q?Feat:=20Cria=C3=A7=C3=A3o=20do=20input.sp?= =?UTF-8?q?ec.tsx=20para=20testes=20referente=20ao=20InputComponent.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/Inputs/input.spec.tsx | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/__tests__/components/Inputs/input.spec.tsx diff --git a/src/__tests__/components/Inputs/input.spec.tsx b/src/__tests__/components/Inputs/input.spec.tsx new file mode 100644 index 0000000..98feb36 --- /dev/null +++ b/src/__tests__/components/Inputs/input.spec.tsx @@ -0,0 +1,51 @@ +import '@testing-library/jest-dom'; +import { fireEvent, render, screen } from '@testing-library/react'; +import { useState } from 'react'; + +import InputComponent from '../../../components/Input'; + +describe('Testes referente ao InputComponent', () => { + it('Validação de existência do Input', () => { + const Component = () => { + const [initialValue, setInitialValue] = useState(''); + return ( + { + setInitialValue(text.target.value); + }} + /> + ); + }; + render(); + + const input = screen.getByRole('textbox'); + expect(input).toBeInTheDocument(); + }); + + it('Alteração de valores do Input', () => { + const Component = () => { + const [initialValue, setInitialValue] = useState(''); + return ( + { + setInitialValue(text.target.value); + }} + /> + ); + }; + render(); + + const input = screen.getByRole('textbox'); + fireEvent.change(input, { target: { value: '123' } }); + + expect(input).toHaveValue('123'); + }); +}); From c5d7cbdeab68097c6913c17edaf3e4f579cee970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Fri, 30 Aug 2024 21:20:27 -0300 Subject: [PATCH 03/21] =?UTF-8?q?Test(#163):=20Cria=C3=A7=C3=A3o=20de=20te?= =?UTF-8?q?stes=20para=20a=20navbar=20da=20LandingPage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/Landing/navbar.spec.tsx | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/__tests__/components/Landing/navbar.spec.tsx diff --git a/src/__tests__/components/Landing/navbar.spec.tsx b/src/__tests__/components/Landing/navbar.spec.tsx new file mode 100644 index 0000000..da849de --- /dev/null +++ b/src/__tests__/components/Landing/navbar.spec.tsx @@ -0,0 +1,37 @@ +import '@testing-library/jest-dom'; + +import { render, screen } from '@testing-library/react'; +import { BrowserRouter as Router } from 'react-router-dom'; + +import userEvent from '@testing-library/user-event'; +import NavbarComponent2 from '../../../components/Landing/Navbar2'; + +describe('Teste na Navbar da LandingPage', () => { + test('Renderiza o botão "Entrar"', () => { + render( + + + + ); + + // Verifica se o botão "Entrar" está no documento + const enterButton = screen.getByRole('button', { name: /Entrar/i }); + expect(enterButton).toBeInTheDocument(); + }); + + test('Navega para a página de sign-in quando o botão "Entrar" é clicado', async () => { + render( + + + + ); + + // Simula o clique no botão "Entrar" + const enterButton = screen.getByRole('button', { name: /Entrar/i }); + userEvent.click(enterButton); + + // Verifica se o link para a página de sign-in está presente + const linkElement = screen.getByRole('link', { name: /Entrar/i }); + expect(linkElement).toHaveAttribute('href', '/SignIn'); + }); +}); From 51476cc3e250e7f78fc40f1460fdbbb80b4214a7 Mon Sep 17 00:00:00 2001 From: Lucca Medeiros Date: Fri, 30 Aug 2024 21:32:07 -0300 Subject: [PATCH 04/21] =?UTF-8?q?Setup(#163):=20Instala=C3=A7=C3=A3o=20da?= =?UTF-8?q?=20depend=C3=AAncia=20@testing-library/user-event.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ package.json | 1 + 2 files changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 860f7c0..05ebc83 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ dist-ssr # Environment .env + +# YARN LOCK TCHAU +yarn.lock \ No newline at end of file diff --git a/package.json b/package.json index 2b12deb..7ffb1a2 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "devDependencies": { "@testing-library/jest-dom": "^6.5.0", "@testing-library/react": "^16.0.0", + "@testing-library/user-event": "^14.5.2", "@types/jest": "^29.5.12", "@types/react": "^18.2.66", "@types/react-dom": "^18.2.22", From d5def4df08399a981a71d36160ff0ca8e90854b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Fri, 30 Aug 2024 22:30:47 -0300 Subject: [PATCH 05/21] =?UTF-8?q?Test(#163):=20Cria=C3=A7=C3=A3o=20de=20te?= =?UTF-8?q?ste=20para=20bot=C3=A3o=20de=20"Cadastre-se"=20na=20LandingPage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 --- .../components/Landing/navbar.spec.tsx | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/__tests__/components/Landing/navbar.spec.tsx b/src/__tests__/components/Landing/navbar.spec.tsx index da849de..99ee443 100644 --- a/src/__tests__/components/Landing/navbar.spec.tsx +++ b/src/__tests__/components/Landing/navbar.spec.tsx @@ -34,4 +34,32 @@ describe('Teste na Navbar da LandingPage', () => { const linkElement = screen.getByRole('link', { name: /Entrar/i }); expect(linkElement).toHaveAttribute('href', '/SignIn'); }); + + test('Renderiza o botão "Cadastre-se"', () => { + render( + + + + ); + + // Verifica se o botão "Cadastre-se" está no documento + const enterButton = screen.getByRole('button', { name: /Cadastre-se/i }); + expect(enterButton).toBeInTheDocument(); + }); + + test('Navega para a página de SignUp quando o botão "Cadastre-se" é clicado', async () => { + render( + + + + ); + + // Simula o clique no botão "Entrar" + const enterButton = screen.getByRole('button', { name: /Cadastre-se/i }); + userEvent.click(enterButton); + + // Verifica se o link para a página de sign-in está presente + const linkElement = screen.getByRole('link', { name: /Cadastre-se/i }); + expect(linkElement).toHaveAttribute('href', '/signUp'); + }); }); From 0de48cdb47dd3d84e9204a23e6f615df868e6429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Fri, 30 Aug 2024 22:48:20 -0300 Subject: [PATCH 06/21] =?UTF-8?q?Test(#163):=20Realiza=C3=A7=C3=A3o=20de?= =?UTF-8?q?=20teste=20no=20footer=20da=20LandingPage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/Landing/Calltoaction.test.tsx | 25 +++++++++++++++++++ .../components/Landing/navbar.spec.tsx | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/components/Landing/Calltoaction.test.tsx diff --git a/src/__tests__/components/Landing/Calltoaction.test.tsx b/src/__tests__/components/Landing/Calltoaction.test.tsx new file mode 100644 index 0000000..58806d1 --- /dev/null +++ b/src/__tests__/components/Landing/Calltoaction.test.tsx @@ -0,0 +1,25 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import '@testing-library/jest-dom'; +import Calltoaction from '../../../components/Landing/Calltoaction'; + +describe('Testes no componente Calltoaction', () => { + test('Verificar se somos redirecionados para a URl do GitHub quando "CRIADO POR SQUAD 9" é clicado', () => { + render(); + + // Obtenha o link usando o texto "CRIADO POR SQUAD 9" + const linkElement = screen.getByText(/CRIADO POR SQUAD 9/i); + + // Verifique se o link tem o href correto + expect(linkElement.closest('a')).toHaveAttribute( + 'href', + 'https://github.com/unb-mds/Squad09-UnBHUB' + ); + + // Simule o clique no link + userEvent.click(linkElement); + + // Verifique se o link foi clicado (isso não abre a URL na verdade, só confirma que o link é funcional) + expect(linkElement.closest('a')).toBeInTheDocument(); + }); +}); diff --git a/src/__tests__/components/Landing/navbar.spec.tsx b/src/__tests__/components/Landing/navbar.spec.tsx index 99ee443..bcf8fe6 100644 --- a/src/__tests__/components/Landing/navbar.spec.tsx +++ b/src/__tests__/components/Landing/navbar.spec.tsx @@ -54,11 +54,11 @@ describe('Teste na Navbar da LandingPage', () => { ); - // Simula o clique no botão "Entrar" + // Simula o clique no botão "Cadastre-se" const enterButton = screen.getByRole('button', { name: /Cadastre-se/i }); userEvent.click(enterButton); - // Verifica se o link para a página de sign-in está presente + // Verifica se o link para a página de sign-up está presente const linkElement = screen.getByRole('link', { name: /Cadastre-se/i }); expect(linkElement).toHaveAttribute('href', '/signUp'); }); From 2c691f3f99f0f56639823ee7fc56cf2eca64e31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Fri, 30 Aug 2024 23:33:15 -0300 Subject: [PATCH 07/21] =?UTF-8?q?Test(#163):=20Realiza=C3=A7=C3=A3o=20de?= =?UTF-8?q?=20teste=20unit=C3=A1rio=20no=20bot=C3=A3o=20de=20"Saiba=20Mais?= =?UTF-8?q?"=20da=20LandingPage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 --- .../components/Landing/Hero.spec.tsx | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/__tests__/components/Landing/Hero.spec.tsx diff --git a/src/__tests__/components/Landing/Hero.spec.tsx b/src/__tests__/components/Landing/Hero.spec.tsx new file mode 100644 index 0000000..ebd8cbe --- /dev/null +++ b/src/__tests__/components/Landing/Hero.spec.tsx @@ -0,0 +1,22 @@ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import Hero from '../../../components/Landing/Hero'; // Ajuste o caminho conforme necessário + +describe('Teste no componente Hero', () => { + test('Renderiza o botão "Saiba Mais"', () => { + render(); + + // Verifica se o botão "Saiba Mais" está no documento + const saibaMaisButton = screen.getByRole('button', { name: /Saiba Mais/i }); + expect(saibaMaisButton).toBeInTheDocument(); + }); + + test('O botão "Saiba Mais" redireciona para a URL correta quando clicado', () => { + render(); + + // Simula o clique no botão "Saiba Mais" + const saibaMaisButton = screen.getByRole('button', { name: /Saiba Mais/i }); + userEvent.click(saibaMaisButton); + }); +}); From 5100358af89dad52319c98e0525c56c46d9af8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Sat, 31 Aug 2024 15:40:25 -0300 Subject: [PATCH 08/21] =?UTF-8?q?Test(#163):=20Testes=20Unit=C3=A1rios=20n?= =?UTF-8?q?o=20componente=20checkbox=20da=20p=C3=A1gina=20de=20SignIn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 --- .../components/Landing/Calltoaction.test.tsx | 4 +- .../components/Landing/Hero.spec.tsx | 4 +- .../components/SignIn/Checkbox.spec.tsx | 61 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/components/SignIn/Checkbox.spec.tsx diff --git a/src/__tests__/components/Landing/Calltoaction.test.tsx b/src/__tests__/components/Landing/Calltoaction.test.tsx index 58806d1..e56a571 100644 --- a/src/__tests__/components/Landing/Calltoaction.test.tsx +++ b/src/__tests__/components/Landing/Calltoaction.test.tsx @@ -1,6 +1,8 @@ +import '@testing-library/jest-dom'; + import { render, screen } from '@testing-library/react'; + import userEvent from '@testing-library/user-event'; -import '@testing-library/jest-dom'; import Calltoaction from '../../../components/Landing/Calltoaction'; describe('Testes no componente Calltoaction', () => { diff --git a/src/__tests__/components/Landing/Hero.spec.tsx b/src/__tests__/components/Landing/Hero.spec.tsx index ebd8cbe..ab3b932 100644 --- a/src/__tests__/components/Landing/Hero.spec.tsx +++ b/src/__tests__/components/Landing/Hero.spec.tsx @@ -1,7 +1,9 @@ import '@testing-library/jest-dom'; + import { render, screen } from '@testing-library/react'; + import userEvent from '@testing-library/user-event'; -import Hero from '../../../components/Landing/Hero'; // Ajuste o caminho conforme necessário +import Hero from '../../../components/Landing/Hero'; describe('Teste no componente Hero', () => { test('Renderiza o botão "Saiba Mais"', () => { diff --git a/src/__tests__/components/SignIn/Checkbox.spec.tsx b/src/__tests__/components/SignIn/Checkbox.spec.tsx new file mode 100644 index 0000000..52c37c0 --- /dev/null +++ b/src/__tests__/components/SignIn/Checkbox.spec.tsx @@ -0,0 +1,61 @@ +import '@testing-library/jest-dom'; + +import { fireEvent, render, screen } from '@testing-library/react'; +import { useState } from 'react'; + +import CheckboxComponent from '../../../components/SignIn/Checkbox'; + +describe('Testes no CheckboxComponent', () => { + it('Validação de existência do Checkbox', () => { + const Component = () => { + const [isChecked, setIsChecked] = useState(false); + return ( + setIsChecked(!!e.checked)} + label="TestLabel" + /> + ); + }; + render(); + + const checkbox = screen.getByRole('checkbox'); + expect(checkbox).toBeInTheDocument(); + }); + + it('Alteração de valores do Checkbox', () => { + const Component = () => { + const [isChecked, setIsChecked] = useState(false); + return ( + setIsChecked(!!e.checked)} + label="TestLabel" + /> + ); + }; + render(); + + const checkbox = screen.getByRole('checkbox'); + fireEvent.click(checkbox); + + expect(checkbox).toBeChecked(); + }); + + it('Renderiza a label corretamente', () => { + const Component = () => { + const [isChecked, setIsChecked] = useState(false); + return ( + setIsChecked(!!e.checked)} + label="TestLabel" + /> + ); + }; + render(); + + const label = screen.getByLabelText('TestLabel'); + expect(label).toBeInTheDocument(); + }); +}); From 853e3478cacd4ceb5cd4d6e16eb364f35475f52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Sat, 31 Aug 2024 16:16:05 -0300 Subject: [PATCH 09/21] =?UTF-8?q?Test(#163):=20Testes=20Unit=C3=A1rios=20n?= =?UTF-8?q?o=20SignInCard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/SignIn/SignInCard.spec.tsx | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/__tests__/components/SignIn/SignInCard.spec.tsx diff --git a/src/__tests__/components/SignIn/SignInCard.spec.tsx b/src/__tests__/components/SignIn/SignInCard.spec.tsx new file mode 100644 index 0000000..2d89ef0 --- /dev/null +++ b/src/__tests__/components/SignIn/SignInCard.spec.tsx @@ -0,0 +1,111 @@ +import '@testing-library/jest-dom'; + +import { + act, + fireEvent, + render, + screen, + waitFor, +} from '@testing-library/react'; +import { BrowserRouter as Router } from 'react-router-dom'; + +import SignInCardComponent from '../../../components/SignIn/SignInCard'; +import SignInFunction from '../../../functions/SignIn/SignIn'; + +// Mock da função SignInFunction +jest.mock('../../../functions/SignIn/SignIn', () => ({ + __esModule: true, + default: jest.fn().mockResolvedValue({}), +})); + +describe('SignInCardComponent', () => { + test('renderiza o formulário e todos os elementos', async () => { + await act(async () => { + render( + + + + ); + }); + + expect(screen.getByLabelText(/Endereço de E-mail/i)).toBeInTheDocument(); + expect(screen.getByLabelText(/Senha/i)).toBeInTheDocument(); + expect(screen.getByRole('button', { name: /Entrar/i })).toBeInTheDocument(); + expect(screen.getByText(/Ainda não tem uma conta?/i)).toBeInTheDocument(); + expect( + screen.getByRole('link', { name: /Cadastrar-se/i }) + ).toBeInTheDocument(); + }); + + test('atualiza os valores dos inputs ao alterar', async () => { + await act(async () => { + render( + + + + ); + }); + + const emailInput = screen.getByLabelText(/Endereço de E-mail/i); + const passwordInput = screen.getByLabelText(/Senha/i); + + await act(async () => { + fireEvent.change(emailInput, { target: { value: 'test@example.com' } }); + fireEvent.change(passwordInput, { target: { value: 'password123' } }); + }); + + expect(emailInput).toHaveValue('test@example.com'); + expect(passwordInput).toHaveValue('password123'); + }); + + test('exibe erros de validação quando o formulário é enviado com campos vazios', async () => { + await act(async () => { + render( + + + + ); + }); + + await act(async () => { + fireEvent.click(screen.getByRole('button', { name: /Entrar/i })); + }); + + await waitFor(() => { + expect( + screen.getByText(/Por favor, forneça um email/i) + ).toBeInTheDocument(); + expect( + screen.getByText(/Por favor, forneça uma senha/i) + ).toBeInTheDocument(); + }); + }); + + test('chama SignInFunction e navega ao enviar o formulário', async () => { + await act(async () => { + render( + + + + ); + }); + + await act(async () => { + fireEvent.change(screen.getByLabelText(/Endereço de E-mail/i), { + target: { value: 'test@example.com' }, + }); + fireEvent.change(screen.getByLabelText(/Senha/i), { + target: { value: 'password123' }, + }); + fireEvent.click(screen.getByRole('button', { name: /Entrar/i })); + }); + + await waitFor(() => { + expect(SignInFunction).toHaveBeenCalledWith( + 'test@example.com', + 'password123' + ); + // Aqui você pode verificar se a navegação foi feita, se necessário + }); + }); +}); From b8552a2c035844bb3680f5993d06bcf9654cee06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Sat, 31 Aug 2024 19:14:24 -0300 Subject: [PATCH 10/21] =?UTF-8?q?Test(#163):=20Teste=20unit=C3=A1rio=20com?= =?UTF-8?q?=20as=20fun=C3=A7=C3=B5es=20FormatTime=20e=20FormatDate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 --- .../functions/FormatTime/FormatDate.spec.tsx | 35 +++++++++++++++ .../functions/FormatTime/FormatTime.spec.tsx | 45 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/__tests__/functions/FormatTime/FormatDate.spec.tsx create mode 100644 src/__tests__/functions/FormatTime/FormatTime.spec.tsx diff --git a/src/__tests__/functions/FormatTime/FormatDate.spec.tsx b/src/__tests__/functions/FormatTime/FormatDate.spec.tsx new file mode 100644 index 0000000..775548f --- /dev/null +++ b/src/__tests__/functions/FormatTime/FormatDate.spec.tsx @@ -0,0 +1,35 @@ +import formatDate from '../../../functions/FormatDate'; +import { Timestamp } from 'firebase/firestore'; + +// Mock do Timestamp +class MockTimestamp { + seconds: number; + nanoseconds: number; + + constructor(seconds: number, nanoseconds: number) { + this.seconds = seconds; + this.nanoseconds = nanoseconds; + } +} + +describe('formatDate', () => { + test('deve formatar corretamente um Timestamp para uma string de data', () => { + // Exemplo de data: 31 de agosto de 2023 (em segundos desde a Epoch) + const mockTimestamp = new MockTimestamp(1693531200, 0); // 01/09/2023 + + const formattedDate = formatDate(mockTimestamp as unknown as Timestamp); + + // Espera que a data seja formatada corretamente no formato 'dd/mm/yyyy' + expect(formattedDate).toBe('31/08/2023'); + }); + + test('deve formatar corretamente um Timestamp diferente', () => { + // Exemplo de data: 24 de dezembro de 2022 (em segundos desde a Epoch) + const mockTimestamp = new MockTimestamp(1671926400, 0); // 24/12/2022 + + const formattedDate = formatDate(mockTimestamp as unknown as Timestamp); + + // Espera que a data seja formatada corretamente no formato 'dd/mm/yyyy' + expect(formattedDate).toBe('24/12/2022'); + }); +}); diff --git a/src/__tests__/functions/FormatTime/FormatTime.spec.tsx b/src/__tests__/functions/FormatTime/FormatTime.spec.tsx new file mode 100644 index 0000000..4c30128 --- /dev/null +++ b/src/__tests__/functions/FormatTime/FormatTime.spec.tsx @@ -0,0 +1,45 @@ +import formatTime from '../../../functions/FormatTime'; +import { Timestamp } from 'firebase/firestore'; + +// Mock do Timestamp +class MockTimestamp { + seconds: number; + nanoseconds: number; + + constructor(seconds: number, nanoseconds: number) { + this.seconds = seconds; + this.nanoseconds = nanoseconds; + } +} + +describe('formatTime', () => { + test('deve formatar corretamente um Timestamp para uma string de hora', () => { + // Exemplo de tempo: 06:05 (em segundos desde a Epoch) + const mockTimestamp = new MockTimestamp(1693559100, 0); + + const formattedTime = formatTime(mockTimestamp as unknown as Timestamp); + + // Espera que o tempo seja formatado corretamente no formato 'hh:mm' + expect(formattedTime).toBe('06:05'); + }); + + test('deve formatar corretamente um Timestamp diferente', () => { + // Exemplo de tempo: 21:30 (em segundos desde a Epoch) + const mockTimestamp = new MockTimestamp(1693528200, 0); + + const formattedTime = formatTime(mockTimestamp as unknown as Timestamp); + + // Espera que o tempo seja formatado corretamente no formato 'hh:mm' + expect(formattedTime).toBe('21:30'); + }); + + test('deve formatar corretamente um Timestamp com horas e minutos diferentes', () => { + // Exemplo de tempo: 16:35 (em segundos desde a Epoch) + const mockTimestamp = new MockTimestamp(1693596900, 0); + + const formattedTime = formatTime(mockTimestamp as unknown as Timestamp); + + // Espera que o tempo seja formatado corretamente no formato 'hh:mm' + expect(formattedTime).toBe('16:35'); + }); +}); From 7040d5cf04ecf22a2c78401000ed73acc0995183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Sat, 31 Aug 2024 19:27:23 -0300 Subject: [PATCH 11/21] =?UTF-8?q?Test(#163):=20Testes=20unit=C3=A1rios=20d?= =?UTF-8?q?as=20fun=C3=A7=C3=B5es=20'isHighlighted'=20e=20'getHighlightedM?= =?UTF-8?q?essage'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 --- .../Calendar/HighlightUtils.spec.tsx | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/__tests__/functions/Calendar/HighlightUtils.spec.tsx diff --git a/src/__tests__/functions/Calendar/HighlightUtils.spec.tsx b/src/__tests__/functions/Calendar/HighlightUtils.spec.tsx new file mode 100644 index 0000000..c316685 --- /dev/null +++ b/src/__tests__/functions/Calendar/HighlightUtils.spec.tsx @@ -0,0 +1,57 @@ +import { + isHighlighted, + getHighlightedMessage, +} from '../../../functions/Calendar/HighlightUtils'; + +describe('isHighlighted', () => { + test('deve retornar true se a data estiver na lista de datas destacadas', () => { + const date = new Date(2023, 7, 31); // 31 de agosto de 2023 + const highlightedDates = [new Date(2023, 7, 31), new Date(2023, 8, 1)]; + + expect(isHighlighted(date, highlightedDates)).toBe(true); + }); + + test('deve retornar false se a data não estiver na lista de datas destacadas', () => { + const date = new Date(2023, 7, 31); // 31 de agosto de 2023 + const highlightedDates = [new Date(2023, 8, 1), new Date(2023, 8, 2)]; + + expect(isHighlighted(date, highlightedDates)).toBe(false); + }); + + test('deve retornar false se a lista de datas destacadas estiver vazia', () => { + const date = new Date(2023, 7, 31); // 31 de agosto de 2023 + const highlightedDates: Date[] = []; + + expect(isHighlighted(date, highlightedDates)).toBe(false); + }); +}); + +describe('getHighlightedMessage', () => { + test('deve retornar a mensagem correta para uma data destacada', () => { + const date = new Date(2023, 7, 31); // 31 de agosto de 2023 + const highlightedMessages = { + '2023-08-31': 'Aniversário', + '2023-09-01': 'Feriado', + }; + + expect(getHighlightedMessage(date, highlightedMessages)).toBe( + 'Aniversário' + ); + }); + + test('deve retornar uma string vazia se a data não tiver uma mensagem associada', () => { + const date = new Date(2023, 7, 31); // 31 de agosto de 2023 + const highlightedMessages = { + '2023-09-01': 'Feriado', + }; + + expect(getHighlightedMessage(date, highlightedMessages)).toBe(''); + }); + + test('deve retornar uma string vazia se o objeto de mensagens destacadas estiver vazio', () => { + const date = new Date(2023, 7, 31); // 31 de agosto de 2023 + const highlightedMessages: Record = {}; + + expect(getHighlightedMessage(date, highlightedMessages)).toBe(''); + }); +}); From 1bb7ffb1c42091e0b9558c2f0c9f1d21bdadabe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Sat, 31 Aug 2024 19:39:03 -0300 Subject: [PATCH 12/21] =?UTF-8?q?Test(#163):=20Testes=20unit=C3=A1rios=20n?= =?UTF-8?q?o=20componente=20'Calltoaction2'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Cardapio/Calltoaction2.spec.tsx | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/__tests__/components/Cardapio/Calltoaction2.spec.tsx diff --git a/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx b/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx new file mode 100644 index 0000000..7ac0ea7 --- /dev/null +++ b/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx @@ -0,0 +1,34 @@ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; +import Calltoaction2 from '../../../components/Cardapio/Calltoaction2'; + +describe('Calltoaction2', () => { + test('renders the component with correct elements', () => { + render(); + + // Verifica se o link para o GitHub está presente e tem o texto correto + const githubLink = screen.getByRole('link', { + name: /CRIADO POR SQUAD 9/i, + }); + expect(githubLink).toBeInTheDocument(); + expect(githubLink).toHaveAttribute( + 'href', + 'https://github.com/unb-mds/Squad09-UnBHUB' + ); + + // Verifica se o link para o RU está presente e tem o texto correto + const ruLink = screen.getByRole('link', { + name: /https:\/\/ru\.unb\.br\/index\.php\/cardapio-refeitorio/i, + }); + expect(ruLink).toBeInTheDocument(); + expect(ruLink).toHaveAttribute( + 'href', + 'https://ru.unb.br/index.php/cardapio-refeitorio' + ); + + // Verifica se os textos adicionais estão presentes + expect( + screen.getByText('Informações retiradas de:', { exact: false }) + ).toBeInTheDocument(); + }); +}); From a624686772ed90a4502ac97779e42e0a5405162e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Celeri?= Date: Sun, 1 Sep 2024 13:34:40 -0300 Subject: [PATCH 13/21] =?UTF-8?q?Test(#163):=20Testes=20unit=C3=A1rios=20n?= =?UTF-8?q?o=20componente=20'SignUpCard'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 --- .../Cardapio/Calltoaction2.spec.tsx | 2 + .../components/SignUp/SignUpCard.spec.tsx | 158 ++++++++++++++++++ .../functions/FormatTime/FormatDate.spec.tsx | 1 + .../functions/FormatTime/FormatTime.spec.tsx | 1 + 4 files changed, 162 insertions(+) create mode 100644 src/__tests__/components/SignUp/SignUpCard.spec.tsx diff --git a/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx b/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx index 7ac0ea7..c6fc350 100644 --- a/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx +++ b/src/__tests__/components/Cardapio/Calltoaction2.spec.tsx @@ -1,5 +1,7 @@ import '@testing-library/jest-dom'; + import { render, screen } from '@testing-library/react'; + import Calltoaction2 from '../../../components/Cardapio/Calltoaction2'; describe('Calltoaction2', () => { diff --git a/src/__tests__/components/SignUp/SignUpCard.spec.tsx b/src/__tests__/components/SignUp/SignUpCard.spec.tsx new file mode 100644 index 0000000..cd39009 --- /dev/null +++ b/src/__tests__/components/SignUp/SignUpCard.spec.tsx @@ -0,0 +1,158 @@ +import '@testing-library/jest-dom'; +import { + act, + fireEvent, + render, + screen, + waitFor, +} from '@testing-library/react'; +import { BrowserRouter as Router } from 'react-router-dom'; + +import SignUpCardComponent from '../../../components/SignUp/SignUpCard'; +import SignUpFunction from '../../../functions/SignUp/SignUp'; + +// Mock da função SignUpFunction +jest.mock('../../../functions/SignUp/SignUp', () => ({ + __esModule: true, + default: jest.fn().mockResolvedValue({}), +})); + +describe('SignUpCardComponent', () => { + test('renderiza o formulário e todos os elementos', async () => { + await act(async () => { + render( + + + + ); + }); + + expect(screen.getByLabelText(/Endereço de E-mail/i)).toBeInTheDocument(); + expect( + screen.getByLabelText('Senha', { selector: 'input' }) + ).toBeInTheDocument(); + expect( + screen.getByLabelText('Senha novamente', { selector: 'input' }) + ).toBeInTheDocument(); + expect( + screen.getByRole('button', { name: /Cadastre-se/i }) + ).toBeInTheDocument(); + }); + + test('atualiza os valores dos inputs ao alterar', async () => { + await act(async () => { + render( + + + + ); + }); + + const emailInput = screen.getByLabelText(/Endereço de E-mail/i); + const passwordInput = screen.getByLabelText('Senha', { selector: 'input' }); + const confirmPasswordInput = screen.getByLabelText('Senha novamente', { + selector: 'input', + }); + + await act(async () => { + fireEvent.change(emailInput, { target: { value: 'test@example.com' } }); + fireEvent.change(passwordInput, { target: { value: 'password123' } }); + fireEvent.change(confirmPasswordInput, { + target: { value: 'password123' }, + }); + }); + + expect(emailInput).toHaveValue('test@example.com'); + expect(passwordInput).toHaveValue('password123'); + expect(confirmPasswordInput).toHaveValue('password123'); + }); + + test('exibe erros de validação quando o formulário é enviado com campos vazios', async () => { + await act(async () => { + render( + + + + ); + }); + + await act(async () => { + fireEvent.click(screen.getByRole('button', { name: /Cadastre-se/i })); + }); + + await waitFor(() => { + expect( + screen.getByText(/O campo de e-mail é obrigatório/i) + ).toBeInTheDocument(); + expect( + screen.getByText(/O campo de senha é obrigatório/i) + ).toBeInTheDocument(); + expect( + screen.getByText(/O campo de confirmação de senha é obrigatório/i) + ).toBeInTheDocument(); + }); + }); + + test('exibe erro de validação quando as senhas não coincidem', async () => { + await act(async () => { + render( + + + + ); + }); + + await act(async () => { + fireEvent.change(screen.getByLabelText('Senha', { selector: 'input' }), { + target: { value: 'password123' }, + }); + fireEvent.change( + screen.getByLabelText('Senha novamente', { selector: 'input' }), + { + target: { value: 'password321' }, + } + ); + fireEvent.click(screen.getByRole('button', { name: /Cadastre-se/i })); + }); + + await waitFor(() => { + expect( + screen.getByText(/As senhas devem ser iguais/i) + ).toBeInTheDocument(); + }); + }); + + test('chama SignUpFunction e navega ao enviar o formulário com dados válidos', async () => { + await act(async () => { + render( + + + + ); + }); + + await act(async () => { + fireEvent.change(screen.getByLabelText(/Endereço de E-mail/i), { + target: { value: 'test@example.com' }, + }); + fireEvent.change(screen.getByLabelText('Senha', { selector: 'input' }), { + target: { value: 'password123' }, + }); + fireEvent.change( + screen.getByLabelText('Senha novamente', { selector: 'input' }), + { + target: { value: 'password123' }, + } + ); + fireEvent.click(screen.getByRole('button', { name: /Cadastre-se/i })); + }); + + await waitFor(() => { + expect(SignUpFunction).toHaveBeenCalledWith({ + email: 'test@example.com', + password: 'password123', + }); + // Aqui você pode verificar se a navegação foi feita, se necessário + }); + }); +}); diff --git a/src/__tests__/functions/FormatTime/FormatDate.spec.tsx b/src/__tests__/functions/FormatTime/FormatDate.spec.tsx index 775548f..e629695 100644 --- a/src/__tests__/functions/FormatTime/FormatDate.spec.tsx +++ b/src/__tests__/functions/FormatTime/FormatDate.spec.tsx @@ -1,4 +1,5 @@ import formatDate from '../../../functions/FormatDate'; + import { Timestamp } from 'firebase/firestore'; // Mock do Timestamp diff --git a/src/__tests__/functions/FormatTime/FormatTime.spec.tsx b/src/__tests__/functions/FormatTime/FormatTime.spec.tsx index 4c30128..b28b151 100644 --- a/src/__tests__/functions/FormatTime/FormatTime.spec.tsx +++ b/src/__tests__/functions/FormatTime/FormatTime.spec.tsx @@ -1,4 +1,5 @@ import formatTime from '../../../functions/FormatTime'; + import { Timestamp } from 'firebase/firestore'; // Mock do Timestamp From 1ebd5dd974a1ae1d827692f712c369d8f699f701 Mon Sep 17 00:00:00 2001 From: Cristiano Borges de Morais Date: Sun, 1 Sep 2024 16:27:31 -0300 Subject: [PATCH 14/21] =?UTF-8?q?Fix(#169):=20Ajustando=20bot=C3=A3o=20de?= =?UTF-8?q?=20editar=20na=20library=20e=20consertando=20bug=20na=20edi?= =?UTF-8?q?=C3=A7=C3=A3o=20da=20library=20e=20tasks.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Activities/editActivity.tsx | 2 +- src/components/Library/editBook.tsx | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/components/Activities/editActivity.tsx b/src/components/Activities/editActivity.tsx index e565e46..e9930f5 100644 --- a/src/components/Activities/editActivity.tsx +++ b/src/components/Activities/editActivity.tsx @@ -131,7 +131,7 @@ export default function EditActivityComponent( header={isEditing ? 'Editar atividade' : 'Visualizar atividade'} visible={visibleEdit} style={{ width: '40vw', maxWidth: '600px' }} - onHide={() => EditsetVisible(false)} + onHide={() => {EditsetVisible(false);setIsEditing(false)}} >
EditsetVisible1(false)} + onHide={() => {EditsetVisible1(false); setIsEditing(false)}} > )}