From 439ebc84b5c57f2e1cc01dca273bc78915f0913f Mon Sep 17 00:00:00 2001 From: hildelio Date: Mon, 21 Nov 2022 18:25:56 -0300 Subject: [PATCH 1/2] Tests 1 e 2 --- src/tests/App.test.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/tests/App.test.js b/src/tests/App.test.js index ea6cb2d..beb321a 100644 --- a/src/tests/App.test.js +++ b/src/tests/App.test.js @@ -1,24 +1,39 @@ import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import App from '../App'; +import responseAPI from './mocks' describe('Test Rick & Morty API', () => { beforeEach(()=>{ - //Fazer o mock do fetch aqui - + global.fetch = jest.fn().mockResolvedValue({ + json: jest.fn().mockResolvedValue({ + responseAPI, + }) + }) render() }) test('Verifica se aparece o card com titulo de "Rick Sanchez"', () => { - + const h3El = screen.getByRole("heading", {level: 3}); + expect(h3El).toBeInTheDocument(); }) test('Verifica se existem o input de texto e o botão "Buscar"', () => { - + const inputEl = screen.getByRole("textbox"); + const buttonEl = screen.getByRole("button"); + expect(inputEl).toBeInTheDocument(); + expect(buttonEl).toBeInTheDocument(); }) test('Verifica se ao buscar por "Smith" aparecem 4 cards', () => { - + const inputEl = screen.getByRole("textbox"); + const buttonEl = screen.getByRole("button"); + const h3El = screen.getAllByRole("heading", { name: /smith/i }); + + userEvent.type(inputEl, "Smith"); + userEvent.click(buttonEl); + expect(h3El).toHaveLength(3); }) }) From e020e73fa06e9ad978f9a9290177e5792092de9a Mon Sep 17 00:00:00 2001 From: hildelio Date: Mon, 21 Nov 2022 23:48:22 -0300 Subject: [PATCH 2/2] =?UTF-8?q?Exerc=C3=ADcio=20finalizado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tests/App.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/tests/App.test.js b/src/tests/App.test.js index beb321a..3514874 100644 --- a/src/tests/App.test.js +++ b/src/tests/App.test.js @@ -5,17 +5,18 @@ import responseAPI from './mocks' describe('Test Rick & Morty API', () => { +const results = responseAPI.results; beforeEach(()=>{ global.fetch = jest.fn().mockResolvedValue({ json: jest.fn().mockResolvedValue({ - responseAPI, + results, }) }) render() }) test('Verifica se aparece o card com titulo de "Rick Sanchez"', () => { - const h3El = screen.getByRole("heading", {level: 3}); + const h3El = screen.getByRole("heading", {name: /Rick Sanchez/i }); expect(h3El).toBeInTheDocument(); }) @@ -33,7 +34,7 @@ describe('Test Rick & Morty API', () => { userEvent.type(inputEl, "Smith"); userEvent.click(buttonEl); - expect(h3El).toHaveLength(3); + expect(h3El).toHaveLength(4); }) })