├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.js ├── components │ └── Counter │ │ ├── Counter.test.js │ │ ├── index.css │ │ └── index.js ├── index.css ├── index.js └── setupTests.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Testando componentes react 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-test-component", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.4", 7 | "@testing-library/react": "^11.1.0", 8 | "@testing-library/user-event": "^12.1.10", 9 | "react": "^17.0.1", 10 | "react-dom": "^17.0.1", 11 | "react-scripts": "4.0.3", 12 | "web-vitals": "^1.0.1" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": [ 22 | "react-app", 23 | "react-app/jest" 24 | ] 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/washingtondeveloper/youtube-react-test-component/e26dcde72952fbc615454d3d4bd6627721b79956/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/washingtondeveloper/youtube-react-test-component/e26dcde72952fbc615454d3d4bd6627721b79956/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/washingtondeveloper/youtube-react-test-component/e26dcde72952fbc615454d3d4bd6627721b79956/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import Counter from "./components/Counter"; 2 | 3 | function App() { 4 | return ; 5 | } 6 | 7 | export default App; 8 | -------------------------------------------------------------------------------- /src/components/Counter/Counter.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from "@testing-library/react"; 2 | import userEvent from "@testing-library/user-event"; 3 | import Counter from "."; 4 | 5 | describe("Counter Component", () => { 6 | test("deve iniciar o titulo com o valor 0", () => { 7 | render(); 8 | 9 | const counterTitle = screen.getByText("0"); 10 | 11 | expect(counterTitle).toBeInTheDocument(); 12 | }); 13 | 14 | test("deve conter a classe counter__title no titulo", () => { 15 | render(); 16 | 17 | const counterTitle = screen.getByText("0"); 18 | 19 | expect(counterTitle).toHaveClass("counter__title"); 20 | }); 21 | 22 | test("não deve iniciar o titulo com as classes counter__title--increment e counter__title--decrement", () => { 23 | render(); 24 | 25 | const counterTitle = screen.getByText("0"); 26 | 27 | expect(counterTitle).not.toHaveClass("counter__title--increment"); 28 | expect(counterTitle).not.toHaveClass("counter__title--decrement"); 29 | }); 30 | 31 | test("deve conter um botão incrementar", () => { 32 | render(); 33 | 34 | const buttonIncrement = screen.getByRole("button", { 35 | name: /incrementar/i, 36 | }); 37 | 38 | expect(buttonIncrement).toBeInTheDocument(); 39 | }); 40 | 41 | test("deve conter um botão incrementar com as classes button e button--increment", () => { 42 | render(); 43 | 44 | const buttonIncrement = screen.getByRole("button", { 45 | name: /incrementar/i, 46 | }); 47 | 48 | expect(buttonIncrement).toHaveClass("button"); 49 | expect(buttonIncrement).toHaveClass("button--increment"); 50 | }); 51 | 52 | test("deve conter um botão decrementar", () => { 53 | render(); 54 | 55 | const buttonDecrement = screen.getByRole("button", { 56 | name: /decrementar/i, 57 | }); 58 | 59 | expect(buttonDecrement).toBeInTheDocument(); 60 | }); 61 | 62 | test("deve conter um botão decrementar com as classes button e button--decrement", () => { 63 | render(); 64 | 65 | const buttonDecrement = screen.getByRole("button", { 66 | name: /decrementar/i, 67 | }); 68 | 69 | expect(buttonDecrement).toHaveClass("button"); 70 | expect(buttonDecrement).toHaveClass("button--decrement"); 71 | }); 72 | 73 | test("deve incrementar + 1 ao clicar no botão incrementar", () => { 74 | render(); 75 | 76 | const buttonIncrement = screen.getByRole("button", { 77 | name: /incrementar/i, 78 | }); 79 | 80 | expect(screen.queryByText("1")).toBeNull(); 81 | userEvent.click(buttonIncrement); 82 | expect(screen.getByText("1")).toBeInTheDocument(); 83 | }); 84 | 85 | test("deve decrementar - 1 ao clicar no botão decrementar", () => { 86 | render(); 87 | 88 | const buttonDecrement = screen.getByRole("button", { 89 | name: /decrementar/i, 90 | }); 91 | 92 | expect(screen.queryByText("-1")).toBeNull(); 93 | userEvent.click(buttonDecrement); 94 | expect(screen.getByText("-1")).toBeInTheDocument(); 95 | }); 96 | 97 | test("deve adicionar a classe counter__title--increment no titulo, quando o valor for maior do que 0", () => { 98 | render(); 99 | 100 | const buttonIncrement = screen.getByRole("button", { 101 | name: /incrementar/i, 102 | }); 103 | 104 | expect(screen.queryByText("0")).not.toHaveClass( 105 | "counter__title--increment" 106 | ); 107 | userEvent.click(buttonIncrement); 108 | expect(screen.getByText("1")).toHaveClass("counter__title--increment"); 109 | }); 110 | 111 | test("deve adicionar a classe counter__title--decrement no titulo, quando o valor for menor do que 0", () => { 112 | render(); 113 | 114 | const buttonDecrement = screen.getByRole("button", { 115 | name: /decrementar/i, 116 | }); 117 | 118 | expect(screen.queryByText("0")).not.toHaveClass( 119 | "counter__title--decrement" 120 | ); 121 | userEvent.click(buttonDecrement); 122 | expect(screen.getByText("-1")).toHaveClass("counter__title--decrement"); 123 | }); 124 | }); 125 | -------------------------------------------------------------------------------- /src/components/Counter/index.css: -------------------------------------------------------------------------------- 1 | .counter { 2 | height: 100%; 3 | 4 | display: flex; 5 | align-items: center; 6 | justify-content: center; 7 | flex-direction: column; 8 | } 9 | 10 | .counter__title { 11 | font-size: 95px; 12 | } 13 | 14 | .counter__title--increment { 15 | color: var(--color-button-increment); 16 | } 17 | .counter__title--decrement { 18 | color: var(--color-button-decrement); 19 | } 20 | 21 | .buttons { 22 | display: flex; 23 | } 24 | 25 | .button { 26 | width: 180px; 27 | height: 45px; 28 | 29 | border: none; 30 | border-radius: 6px; 31 | background-color: var(--color-button-default); 32 | font-weight: bold; 33 | text-transform: uppercase; 34 | 35 | margin: 5px; 36 | } 37 | 38 | .button:hover { 39 | background-color: var(--color-button-default-hover); 40 | } 41 | 42 | .button--increment { 43 | background-color: var(--color-button-increment); 44 | color: var(--color-white); 45 | outline-color: var(--color-button-increment-outline); 46 | cursor: pointer; 47 | } 48 | .button--increment:hover { 49 | background-color: var(--color-button-increment-hover); 50 | } 51 | 52 | .button--decrement { 53 | background-color: var(--color-button-decrement); 54 | color: var(--color-white); 55 | outline-color: var(--color-button-decrement-outline); 56 | cursor: pointer; 57 | } 58 | .button--decrement:hover { 59 | background-color: var(--color-button-decrement-hover); 60 | } 61 | -------------------------------------------------------------------------------- /src/components/Counter/index.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import "./index.css"; 3 | 4 | function Counter() { 5 | const [counter, setCounter] = useState(0); 6 | 7 | function getModifier() { 8 | if (counter > 0) return "counter__title--increment"; 9 | if (counter < 0) return "counter__title--decrement"; 10 | return ""; 11 | } 12 | 13 | return ( 14 |
15 |

{counter}

16 |
17 | 23 | 29 |
30 |
31 | ); 32 | } 33 | 34 | export default Counter; 35 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | * { 2 | margin: 0; 3 | padding: 0; 4 | box-sizing: border-box; 5 | } 6 | 7 | :root { 8 | --color-button-default: #f1f1f1; 9 | --color-button-default-hover: #d3d2d2; 10 | --color-button-increment: #41cc1f; 11 | --color-button-decrement: #f02727; 12 | --color-button-increment-hover: #38ad1b; 13 | --color-button-decrement-hover: #d12020; 14 | --color-button-increment-outline: #2e9615; 15 | --color-button-decrement-outline: #8b1414; 16 | 17 | --color-white: #fff; 18 | } 19 | 20 | html, 21 | body, 22 | #root { 23 | height: 100%; 24 | } 25 | 26 | body { 27 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 28 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 29 | sans-serif; 30 | -webkit-font-smoothing: antialiased; 31 | -moz-osx-font-smoothing: grayscale; 32 | } 33 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import App from "./App"; 4 | 5 | import "./index.css"; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById("root") 12 | ); 13 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | --------------------------------------------------------------------------------