|
| 1 | +# Testing React with Jest and Enzyme - [live](https://learning-zone.github.io/testing-react-with-jest-and-enzyme/) |
| 2 | + |
| 3 | +<img src="assets/test-react.png" alt="Testing React using Jest and Enzyme" width="800px" /> |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +# Package Installation |
| 9 | +npm install |
| 10 | + |
| 11 | +# Run the Project |
| 12 | +npm start |
| 13 | + |
| 14 | +# Project Test |
| 15 | +npm test |
| 16 | + |
| 17 | +# Generate Code Coverage Report |
| 18 | +npm run test -- --coverage --watchAll=false |
| 19 | +``` |
| 20 | + |
| 21 | +## Project Covers |
| 22 | + |
| 23 | +- Component Testing |
| 24 | +- Props Testing |
| 25 | +- Snapshot Testing |
| 26 | +- Event Testing |
| 27 | +- Mocking function |
| 28 | +- Rest API Testing |
| 29 | + |
| 30 | +## Folder Structure |
| 31 | + |
| 32 | +```js |
| 33 | + + src |
| 34 | + + components |
| 35 | + + Events |
| 36 | + + __snapshots__ |
| 37 | + - Events.js |
| 38 | + - Events.test.js |
| 39 | + + Header |
| 40 | + - Header.js |
| 41 | + - Header.test.js |
| 42 | + + Props |
| 43 | + - Props.js |
| 44 | + - Props.test.js |
| 45 | + + RestAPI |
| 46 | + - RestAPI.js |
| 47 | + - RestAPI.test.js |
| 48 | + + __mocks__ |
| 49 | + - axios.js |
| 50 | +``` |
| 51 | + |
| 52 | +## Code Coverage Report |
| 53 | + |
| 54 | +```js |
| 55 | +|File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | |
| 56 | +|------------------------|----------|----------|----------|----------|-------------------| |
| 57 | +|All files | 88.89 | 0 | 75 | 88.24 | | |
| 58 | +| src | 100 | 100 | 100 | 100 | | |
| 59 | +| App.js | 100 | 100 | 100 | 100 | | |
| 60 | +| src/Components/Events | 50 | 0 | 33.33 | 50 | | |
| 61 | +| Events.js | 50 | 0 | 33.33 | 50 | 14,16 | |
| 62 | +| src/Components/Header | 100 | 100 | 100 | 100 | | |
| 63 | +| Header.js | 100 | 100 | 100 | 100 | | |
| 64 | +| src/Components/Props | 100 | 100 | 100 | 100 | | |
| 65 | +| Props.js | 100 | 100 | 100 | 100 | | |
| 66 | +| src/Components/RestAPI | 100 | 100 | 80 | 100 | | |
| 67 | +| RestAPI.js | 100 | 100 | 80 | 100 | | |
| 68 | +|------------------------|----------|----------|----------|----------|-------------------| |
| 69 | +``` |
| 70 | + |
| 71 | +## Deploy React App to GitHub Pages |
| 72 | + |
| 73 | +**Step 01: Install GitHub Pages package as a dev-dependency** |
| 74 | + |
| 75 | +```bash |
| 76 | +# Install Github Pages into repository |
| 77 | +npm install gh-pages --save-dev |
| 78 | +``` |
| 79 | + |
| 80 | +**Step 02: Add properties to `package.json` file** |
| 81 | + |
| 82 | +The first property we need to add at the top level `homepage` second we will define this as a string and the value will be `http://{username}.github.io/{repo-name}` {username} is your GitHub username, and {repo-name} is the name of the GitHub repository you created it will look like this: |
| 83 | + |
| 84 | +```bash |
| 85 | +"homepage": "http://learning-zone.github.io/testing-react-with-jest-and-enzyme" |
| 86 | +``` |
| 87 | + |
| 88 | +Second in the existing scripts property we to need to add predeploy and deploy. |
| 89 | + |
| 90 | +```bash |
| 91 | +"scripts": { |
| 92 | +//... |
| 93 | +"predeploy": "npm run build", |
| 94 | +"deploy": "gh-pages -d build" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +**Step 03: Create a Github repository and initialize it** |
| 99 | + |
| 100 | +Now, create a remote GitHub repository with your app name and go back initialize this `git init` add it as remote `git remote add origin git@github.com:learning-zone/testing-react-with-jest-and-enzyme.git` |
| 101 | + |
| 102 | +**Step 04: Deploy to GitHub Pages** |
| 103 | + |
| 104 | +```bash |
| 105 | +npm run deploy |
| 106 | +``` |
| 107 | + |
| 108 | +this command will create a branch named gh-pages this branch host your app, and homepage property you created in package.json file hold your link for a live preview, or you can open the **Repository >> Settings** scroll down to **GitHub Pages** section you will find this: |
| 109 | + |
| 110 | +<img src="assets/github-pages.png" alt="Github Pages" /> |
| 111 | + |
| 112 | +## Technology |
| 113 | + |
| 114 | +- [React](https://create-react-app.dev/docs/getting-started/) |
| 115 | +- [Jest](https://jestjs.io/docs/en/getting-started.html) |
| 116 | +- [Enzyme](https://enzymejs.github.io/enzyme/docs/installation/react-16.html) |
| 117 | +- [Material-UI](https://material-ui.com/getting-started/installation/) |
| 118 | +- [Axios](https://github.com/axios/axios) |
| 119 | +- [React Testing Library](https://reactjs.org/docs/testing-recipes.html) |
0 commit comments