├── .gitignore ├── .gitlab-ci.yml ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.js ├── App.test.js ├── Form.jsx ├── Form.test.js ├── Header.jsx ├── Listcar │ ├── ListCar.jsx │ └── ListCar.test.js ├── coba.test.js ├── header.test.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.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 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | stages: 2 | - build 3 | - test 4 | - deploy 5 | 6 | image: node:lts 7 | 8 | build job: 9 | stage: build 10 | script: 11 | - yarn install 12 | - yarn build 13 | artifacts: 14 | paths: 15 | - build/ 16 | - node_modules 17 | 18 | test job: 19 | stage: test 20 | script: 21 | - yarn test 22 | 23 | deploy job: 24 | stage: deploy 25 | script: 26 | - yarn global add netlify-cli 27 | - netlify deploy --dir=build --prod -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `yarn start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser. 13 | 14 | The page will reload when you make changes.\ 15 | You may also see any lint errors in the console. 16 | 17 | ### `yarn test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `yarn build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `yarn eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 35 | 36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. 39 | 40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `yarn build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "learn-testing", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.14.1", 7 | "@testing-library/react": "^13.0.0", 8 | "@testing-library/user-event": "^13.2.1", 9 | "axios": "^0.27.2", 10 | "chart.js": "^3.7.1", 11 | "react": "^18.0.0", 12 | "react-chartjs-2": "^4.1.0", 13 | "react-dom": "^18.0.0", 14 | "react-dropzone-uploader": "^2.11.0", 15 | "react-scripts": "5.0.1", 16 | "web-vitals": "^2.1.0" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": [ 26 | "react-app", 27 | "react-app/jest" 28 | ] 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raihan-muhammad/testing-react-example/25953c04a6a847b18a67b11b4b97bd319da53b29/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/raihan-muhammad/testing-react-example/25953c04a6a847b18a67b11b4b97bd319da53b29/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raihan-muhammad/testing-react-example/25953c04a6a847b18a67b11b4b97bd319da53b29/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.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import logo from './logo.svg'; 2 | import './App.css'; 3 | import ListCar from "./Listcar/ListCar"; 4 | import Dropzone from "react-dropzone-uploader"; 5 | import "react-dropzone-uploader/dist/styles.css"; 6 | import { useState, useEffect } from "react"; 7 | import { Bar } from "react-chartjs-2"; 8 | // eslint-disable-next-line 9 | import { Chart } from "chart.js/auto"; 10 | 11 | function App() { 12 | const [files, setFiles] = useState(); 13 | const [dataCar, setDataCar] = useState([]); 14 | 15 | const getDataCar = async () => { 16 | const data = await fetch(`https://rent-cars-api.herokuapp.com/admin/car`); 17 | const result = await data.json(); 18 | setDataCar(result); 19 | console.log(result); 20 | }; 21 | 22 | const getUploadParams = ({ meta }) => { 23 | setFiles(meta); 24 | return { url: "https://httpbin.org/post" }; 25 | }; 26 | 27 | const hanldeChangeStatus = ({ meta, file }, status) => { 28 | console.log(`Perubahaan terjadi ${status}`); 29 | setFiles(meta); 30 | }; 31 | 32 | const handleSubmit = (files, allFiles) => { 33 | console.log(`Files => ${files} && all Files => ${allFiles}`); 34 | allFiles.forEach((data) => data.remove()); 35 | }; 36 | 37 | useEffect(() => { 38 | getDataCar(); 39 | }, [files]); 40 | 41 | const dataChart = { 42 | labels: ["January", "February", "March"], 43 | datasets: [ 44 | { 45 | label: "terjual", 46 | backgroundColor: "royalblue", 47 | data: dataCar.map((item) => item.price), 48 | }, 49 | { 50 | label: "Tidak terjual", 51 | backgroundColor: "salmon", 52 | data: [3, 5, 61], 53 | }, 54 | ], 55 | }; 56 | 57 | return ( 58 | <> 59 |

60 | Selamat Datang di mana yaa! 61 |

62 | {/* Data Visualiasi with react chart.js */} 63 |
64 | 65 |
66 | 67 | {/* Media handling with react dropzone */} 68 | 74 | 75 |
76 |
77 | logo 78 |

79 | Edit src/App.js and save to reload. 80 |

81 | 87 | Learn React 88 | 89 |
90 |
91 | 92 | ); 93 | } 94 | 95 | export default App; 96 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/Learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/Form.jsx: -------------------------------------------------------------------------------- 1 | const Form = ({ CALL_FUNCTION }) => { 2 | return ( 3 | <> 4 |

Halaman Form

5 | 6 |

Selamat data di Halaman Form

7 | 8 | 9 | 10 | ); 11 | }; 12 | 13 | export default Form; 14 | -------------------------------------------------------------------------------- /src/Form.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from "@testing-library/react"; 2 | import userEvent from "@testing-library/user-event"; 3 | import Form from "./Form"; 4 | 5 | describe("Test Form", () => { 6 | const MOCK_FUNCTION = jest.fn(); 7 | 8 | beforeEach(() => { 9 | // eslint-disable-next-line 10 | render(
); 11 | }); 12 | 13 | test("cek apakah text halaman form ada atau engga", () => { 14 | const textHeading = screen.getByTestId("text-form"); 15 | expect(textHeading).toBeInTheDocument(); 16 | }); 17 | 18 | test("Check apakah button submit menjalankan sebuah function", () => { 19 | const btnSubmit = screen.getByRole("button"); 20 | userEvent.click(btnSubmit); 21 | 22 | expect(MOCK_FUNCTION).toHaveBeenCalled(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/Header.jsx: -------------------------------------------------------------------------------- 1 | const Header = () => { 2 | return

Header

; 3 | }; 4 | 5 | export default Header; 6 | -------------------------------------------------------------------------------- /src/Listcar/ListCar.jsx: -------------------------------------------------------------------------------- 1 | import { useState } from "react"; 2 | import axios from "axios"; 3 | 4 | const ListCar = () => { 5 | const [listCar, setListCar] = useState([]); 6 | const getCar = async () => { 7 | const { data } = await axios( 8 | `https://rent-cars-api.herokuapp.com/admin/car` 9 | ); 10 | 11 | setListCar(data); 12 | }; 13 | return ( 14 | <> 15 |

List Car

16 | 19 |
    20 | {listCar.map((car) => ( 21 |
  • 22 | {car.name} 23 |
  • 24 | ))} 25 |
26 | 27 | ); 28 | }; 29 | 30 | export default ListCar; 31 | -------------------------------------------------------------------------------- /src/Listcar/ListCar.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen, waitFor } from "@testing-library/react"; 2 | import userEvent from "@testing-library/user-event"; 3 | import axios from "axios"; 4 | import ListCar from "./ListCar"; 5 | 6 | jest.mock("axios"); 7 | 8 | test("Apakah data dari api jumlahnya sama", async () => { 9 | const dataListCar = [ 10 | { 11 | id: 1, 12 | name: "Kijang", 13 | }, 14 | { 15 | id: 2, 16 | name: "Inova", 17 | }, 18 | { 19 | id: 3, 20 | name: "Fortuner", 21 | }, 22 | ]; 23 | 24 | axios.mockImplementation(() => Promise.resolve({ data: dataListCar })); 25 | render(); 26 | 27 | const btnShow = screen.getByTestId("btn-show"); 28 | userEvent.click(btnShow); 29 | await waitFor(() => { 30 | const listCar = screen.getAllByTestId("car"); 31 | expect(listCar).toHaveLength(dataListCar.length); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /src/coba.test.js: -------------------------------------------------------------------------------- 1 | const penjumlahan = (angka1, angka2) => { 2 | return angka1 + angka2; 3 | }; 4 | 5 | const pengurangan = (angka1, angka2) => { 6 | return angka1 - angka2; 7 | }; 8 | 9 | // Pengurangan 10 | // Pembagian 11 | // Perkalian 12 | // Rata-rata 13 | 14 | describe(`Testing for aritmatika`, () => { 15 | test(`logika function penjumlahan`, () => { 16 | expect(penjumlahan(1, 1)).toBe(2); 17 | }); 18 | 19 | it(`Shoud be a pengurangan`, () => { 20 | expect(pengurangan(5, 2)).toBe(3); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/header.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from "@testing-library/react"; 2 | import Header from "./Header"; 3 | 4 | it(`Shoud be render Header`, () => { 5 | render(
); 6 | expect(screen.getByText("Header")).toBeInTheDocument(); 7 | }); 8 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from "./reportWebVitals"; 6 | 7 | const root = ReactDOM.createRoot(document.getElementById('root')); 8 | root.render( 9 | 10 | 11 | 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------