Skip to content

[Hildélio Júnior] Exercise React and Mock #20

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 21 additions & 5 deletions src/tests/App.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
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', () => {

const results = responseAPI.results;
beforeEach(()=>{
//Fazer o mock do fetch aqui

global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue({
results,
})
})
render(<App/>)
})

test('Verifica se aparece o card com titulo de "Rick Sanchez"', () => {

const h3El = screen.getByRole("heading", {name: /Rick Sanchez/i });
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(4);
})

})