Skip to content

Commit

Permalink
ready?
Browse files Browse the repository at this point in the history
  • Loading branch information
murillo wolf cavalheiro authored and murillo wolf cavalheiro committed Nov 3, 2021
1 parent 38be0d6 commit f88b371
Show file tree
Hide file tree
Showing 22 changed files with 1,040 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,26 @@ dist

# TernJS port file
.tern-port
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# react-and-mocky
# React and Mocky

Nessa aplicação nós utilizamos a [API do Rick](https://rickandmortyapi.com/) and Morty para buscar os personagens. O endpoint utilizado é o `[https://rickandmortyapi.com/api/character](https://rickandmortyapi.com/api/character)`

O objetivo é escrever os testes para a aplicação utilizando o RTL - React Testing Library. Todos os testes devem utilizar o `ByRole`


### Setup para os testes

***A primeira coisa que deve ser feita antes de escrever os testes, é realizar o mock da função fetch.***

Esse mock deve ser feito dentro do `beforeEach()` antes de chamar a função `render(<App/>)`. A resposta da API que vai ser mockada está dentro da pasta `/tests/mocks.js`. Basta importar o `responseAPI` e utilizar da maneira que achar necessário.



### Teste 1

Verificar se ao renderizar o App aparece um card com um `h3` escrito "Rick Sanchez" dentro.

### Teste 2

Verificar se ao renderizar o App está presente na tela o `input` de texto e o `button` com o texto "Buscar"

### Teste 3

Verificar se ao buscar pelo nome "smith" aparecem 4 cards na tela.
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "react-and-mocky",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
47 changes: 47 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}

html, body, #root {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.App{
width: 100vw;
height: 100vh;
}

input[type=text]{
padding: 1rem;
width:70%;
font-size: 14px !important;
}
button {
background-color: #2d6a4f;
width: 30%;
padding: 1rem;
color: white;
cursor: pointer;
}

button:hover{
background-color: #40916c;
}

.card-list{
width: 100%;
display: flex;
align-items: center;
justify-content: space-evenly;
flex-wrap: wrap;
}

form {
margin: 3rem auto 1rem auto;
width: 50%;
}
56 changes: 56 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import './App.css';
import CharacterCard from './components/CharacterCard';
import Loading from './components/Loading';

class App extends React.Component {
constructor(props){
super(props);
this.state={
characters:[],
filteredCharacters:[],
isLoading:true,
search:''
}
}
getCharacters = async () => {
const endpoint = 'https://rickandmortyapi.com/api/character';
const {results} = await fetch(endpoint).then(response => response.json())
this.setState({isLoading:false, characters:results, filteredCharacters:results})
}


componentDidMount(){
this.getCharacters()
}

handleOnChange = ({target:{value}}) => {
this.setState({search:value})
}

handleFilterCharacters = () => {
const { characters, search} = this.state;
const filteredArray = characters.filter(({name})=> name.toUpperCase().includes(search.toUpperCase()))
this.setState({filteredCharacters:filteredArray})
}

render(){
const {filteredCharacters, isLoading} = this.state
return (
<div className="App">
<form>
<input type='text' placeholder='Rick Sanches...' onChange={this.handleOnChange}/>
<button type='button' onClick={this.handleFilterCharacters}>Buscar</button>
</form>
{
isLoading ? <Loading/> :
<section className='card-list'>
{filteredCharacters.map(item=> <CharacterCard character={item} key={Math.random()} />)}
</section>
}
</div>
);
}
}

export default App;
15 changes: 15 additions & 0 deletions src/components/CharacterCard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import './styles.css'
class CharacterCard extends React.Component {
render(){
const { character : {name, image, status}} = this.props
return(
<article className='card'>
<img alt={name} src={image} />
<h3 className='card-name'>{name}</h3>
<p className='card-status'>{status}</p>
</article>
)
}
}
export default CharacterCard;
11 changes: 11 additions & 0 deletions src/components/CharacterCard/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.card{
max-width: 350px;
width: 100%;
margin: 2rem 0;

}

.card-name{
color:#2d6a4f;

}
10 changes: 10 additions & 0 deletions src/components/Loading/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import './styles.css'
class Loading extends React.Component{
render(){
return(
<h3 className='loading'>Carregando...</h3>
)
}
}
export default Loading
3 changes: 3 additions & 0 deletions src/components/Loading/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.loading {

}
13 changes: 13 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
1 change: 1 addition & 0 deletions src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/reportWebVitals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};

export default reportWebVitals;
5 changes: 5 additions & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
18 changes: 18 additions & 0 deletions src/tests/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { render, screen } from '@testing-library/react';
import App from '../App';

describe('Test Rick & Morty API', () => {

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

})

test('Verifica se existem o input de texto e o botão "Buscar"', () => {

})

test('Verifica se ao buscar por "Smith" aparecem 4 cards', () => {

})

})
Loading

0 comments on commit f88b371

Please sign in to comment.