├── .gitignore ├── LICENSE ├── README.CRA.md ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json ├── src ├── App.js ├── App.test.js ├── assets │ └── locales │ │ ├── en.js │ │ └── index.js ├── components │ ├── LoadingIndicator.jsx │ ├── StoreProvider.jsx │ ├── ThemeProvider.jsx │ ├── UserRepositories │ │ ├── UserRepositoriesList.jsx │ │ └── UserRepositoriesListItem.jsx │ ├── UserSelectionForm.container.jsx │ └── UserSelectionForm.jsx ├── helpers │ └── localization.js ├── index.js ├── logo.svg ├── pages │ ├── Pages.container.jsx │ ├── Pages.jsx │ ├── PublicRepositoriesList.container.jsx │ ├── PublicRepositoriesList.jsx │ └── UserSelection.jsx ├── serviceWorker.js ├── services │ └── github.js ├── store │ ├── index.js │ ├── repositories │ │ ├── actionTypes.js │ │ ├── actions.js │ │ ├── reducer.js │ │ └── selectors.js │ └── user │ │ ├── actionTypes.js │ │ ├── actions.js │ │ ├── reducer.js │ │ └── selectors.js └── test │ ├── fixtures │ └── github.js │ ├── helpers │ └── initTestLocalization.js │ └── viewGitHubRepositoriesByUsername.spec.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 63 | 64 | # dependencies 65 | /node_modules 66 | /.pnp 67 | .pnp.js 68 | 69 | # testing 70 | /coverage 71 | 72 | # production 73 | /build 74 | 75 | # misc 76 | .DS_Store 77 | .env.local 78 | .env.development.local 79 | .env.test.local 80 | .env.production.local 81 | 82 | npm-debug.log* 83 | yarn-debug.log* 84 | yarn-error.log* 85 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Anton Rublev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.CRA.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | 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. 35 | 36 | 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. 37 | 38 | 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. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-app-integration-tests-sample 2 | Sample integration tests implementation for a common react app structure 3 | 4 | [Read more about the the project and reasoning here.](https://www.toptal.com/blog/ADD_ARTICLE_LINK_HERE_WHEN_PUBLISHED) 5 | 6 | ## App implementation details 7 | 8 | Goal of the app implementation is follow a common react app structure and patterns while keeping it simple and small for ease of reading and understanding. 9 | 10 | The app implements a simple use case: 11 | 1. User enters a GitHub username 12 | 2. App displays a list of public repositories associated with the entered username 13 | 14 | The app includes: 15 | 16 | - API requests ([axios](https://github.com/axios/axios)) 17 | - Internationalization support ([react-intl-universal](https://github.com/alibaba/react-intl-universal)) 18 | - Global state management ([redux](https://github.com/reduxjs/redux) + [redux-thunk](https://github.com/reduxjs/redux-thunk)) 19 | - CSS in JS solution ([@material-ui/styles](https://material-ui.com/styles/basics/)) 20 | - SPA routing ([react-router-dom](https://github.com/ReactTraining/react-router/tree/master/packages/react-router-dom)) 21 | 22 | The project was bootstrapped with [Create React App](./README.CRA.md). 23 | 24 | ## Integration tests 25 | 26 | Integration tests are written from User perspective and ignore as much of implementation details as possible. 27 | Instead, tests assert that user can interact with the DOM to fulfill a certain scenario. 28 | 29 | Integration tests are written using: 30 | 31 | - [jest](https://github.com/facebook/jest): JavaScript testing framework 32 | - [react-testing-library](https://github.com/testing-library/react-testing-library): Simple and complete React DOM testing utilities that encourage good testing practices. 33 | - [jest-dom](https://github.com/testing-library/jest-dom): Custom jest matchers to test the state of the DOM. 34 | - [nock](https://github.com/nock/nock): HTTP server mocking and expectations library. 35 | 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-app-integration-tests-sample", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@material-ui/core": "^4.1.3", 7 | "@material-ui/styles": "^4.1.2", 8 | "axios": "^0.19.0", 9 | "mdi-material-ui": "^6.1.0", 10 | "react": "^16.8.6", 11 | "react-dom": "^16.8.6", 12 | "react-intl-universal": "^2.0.4", 13 | "react-redux": "^7.1.0", 14 | "react-router-dom": "^5.0.1", 15 | "react-scripts": "3.0.1", 16 | "redux": "^4.0.1", 17 | "redux-thunk": "^2.3.0" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": "react-app" 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | }, 40 | "devDependencies": { 41 | "@testing-library/react": "^8.0.4", 42 | "@testing-library/user-event": "^4.1.0", 43 | "jest-dom": "^3.5.0", 44 | "nock": "^10.0.6" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntonRublev360/react-app-integration-tests-sample/8a6f12f04b86e79b583f13155613501d092e16c4/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | 23 | React App 24 | 25 | 26 | 27 |
28 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /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 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ThemeProvider from './components/ThemeProvider'; 3 | import Pages from './pages/Pages.container'; 4 | import CssBaseline from '@material-ui/core/CssBaseline'; 5 | import StoreProvider from './components/StoreProvider'; 6 | 7 | export default function App() { 8 | return ( 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import './test/helpers/initTestLocalization'; 5 | 6 | it('renders without crashing', () => { 7 | const div = document.createElement('div'); 8 | ReactDOM.render(, div); 9 | ReactDOM.unmountComponentAtNode(div); 10 | }); 11 | -------------------------------------------------------------------------------- /src/assets/locales/en.js: -------------------------------------------------------------------------------- 1 | export default { 2 | userSelection: { 3 | usernameLabel: 'GitHub Username', 4 | usernamePlaceholder: 'Enter GitHub Username', 5 | submitButtonText: 'View Repositories' 6 | }, 7 | repositories: { 8 | header: '{username}\'s public repositories:', 9 | view: 'View on GitHub', 10 | loadingText: 'Loading repositories...', 11 | error: 'Failed to load the list of user\'s repositories', 12 | empty: 'No repositories found' 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /src/assets/locales/index.js: -------------------------------------------------------------------------------- 1 | import en from './en'; 2 | 3 | export default { 4 | 'en-US': en 5 | }; 6 | -------------------------------------------------------------------------------- /src/components/LoadingIndicator.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { CircularProgress, Typography } from '@material-ui/core'; 4 | import { makeStyles } from '@material-ui/core'; 5 | 6 | const useStyles = makeStyles(theme => ({ 7 | container: { 8 | display: 'flex', 9 | flexDirection: 'column', 10 | alignItems: 'center', 11 | justifyContent: 'center' 12 | } 13 | })); 14 | 15 | export default function LoadingIndicator({ 16 | isLoading, 17 | text, 18 | ...rest 19 | }) { 20 | const classes = useStyles(); 21 | if (!isLoading) { 22 | return null; 23 | } 24 | return ( 25 |
26 | 27 | 28 | {text} 29 | 30 |
31 | ); 32 | } 33 | 34 | LoadingIndicator.propTypes = { 35 | isLoading: PropTypes.bool, 36 | text: PropTypes.node 37 | }; 38 | 39 | LoadingIndicator.defaultProps = { 40 | isLoading: false 41 | }; 42 | -------------------------------------------------------------------------------- /src/components/StoreProvider.jsx: -------------------------------------------------------------------------------- 1 | import React, { useMemo } from 'react'; 2 | import { Provider } from 'react-redux' 3 | import createStore from '../store/index'; 4 | 5 | export default function StoreProvider({ children }) { 6 | /* 7 | We need to create store once for the rendered app. 8 | However, in tests the app is rendered multiple times. 9 | useMemo hook below ensures that store is re-created every time the app is first rendered in the dom 10 | */ 11 | const store = useMemo(() => createStore(), []); 12 | return ( 13 | 14 | {children} 15 | 16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /src/components/ThemeProvider.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { ThemeProvider as MuiThermeProvider } from '@material-ui/styles'; 3 | import { createMuiTheme, responsiveFontSizes } from '@material-ui/core/styles'; 4 | 5 | const theme = responsiveFontSizes(createMuiTheme({ 6 | typography: { 7 | useNextVariants: true 8 | }, 9 | palette: { 10 | type: 'light' 11 | } 12 | })); 13 | 14 | export default function ThemeProvider({ children }) { 15 | return ( 16 | {children} 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /src/components/UserRepositories/UserRepositoriesList.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { 4 | List, 5 | ListItem, 6 | ListItemText, 7 | Typography 8 | } from '@material-ui/core'; 9 | import intl from 'react-intl-universal'; 10 | import UserRepositoriesListItem from './UserRepositoriesListItem'; 11 | 12 | export default function UserRepositoriesList({ 13 | repositories, 14 | isFetching, 15 | hasError 16 | }) { 17 | if (hasError) { 18 | return ( 19 | 20 | {intl.get('repositories.error')} 21 | 22 | ); 23 | } 24 | return ( 25 | 26 | {!isFetching && repositories.length === 0 && ( 27 | 28 | 31 | 32 | )} 33 | {repositories.map(repository => ( 34 | 35 | ))} 36 | 37 | ); 38 | } 39 | 40 | UserRepositoriesList.propTypes = { 41 | repositories: PropTypes.array, 42 | isFetching: PropTypes.bool, 43 | hasError: PropTypes.bool 44 | }; 45 | 46 | UserRepositoriesList.defaultProps = { 47 | repositories: [] 48 | }; 49 | -------------------------------------------------------------------------------- /src/components/UserRepositories/UserRepositoriesListItem.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { 4 | Divider, 5 | IconButton, 6 | ListItem, 7 | ListItemText, 8 | ListItemSecondaryAction 9 | } from '@material-ui/core'; 10 | import { GithubCircle } from 'mdi-material-ui'; 11 | import intl from 'react-intl-universal'; 12 | 13 | export default function PublicRepositoryListItem({ 14 | name, 15 | description, 16 | html_url 17 | }) { 18 | return ( 19 | 20 | 21 | 25 | 26 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | ); 39 | } 40 | 41 | PublicRepositoryListItem.propTypes = { 42 | name: PropTypes.string.isRequired, 43 | description: PropTypes.string, 44 | html_url: PropTypes.string.isRequired 45 | }; 46 | -------------------------------------------------------------------------------- /src/components/UserSelectionForm.container.jsx: -------------------------------------------------------------------------------- 1 | import { connect } from 'react-redux'; 2 | import UserSelectionForm from './UserSelectionForm'; 3 | import * as userSelectors from '../store/user/selectors'; 4 | import * as userActions from '../store/user/actions'; 5 | 6 | export default connect( 7 | (state) => ({ 8 | username: userSelectors.getUsername(state) 9 | }), 10 | userActions 11 | )(UserSelectionForm); 12 | -------------------------------------------------------------------------------- /src/components/UserSelectionForm.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { Button, Grid, TextField, InputAdornment } from '@material-ui/core'; 4 | import { makeStyles } from '@material-ui/styles'; 5 | import { GithubCircle } from 'mdi-material-ui'; 6 | import intl from 'react-intl-universal'; 7 | 8 | const GitHubUserInputProps = { 9 | startAdornment: ( 10 | 11 | 12 | 13 | ), 14 | }; 15 | 16 | const useStyles = makeStyles(theme => ({ 17 | button: { 18 | marginTop: theme.spacing(1) 19 | } 20 | })); 21 | 22 | export default function UserSelectionForm({ 23 | username, 24 | setUsername 25 | }) { 26 | const classes = useStyles(); 27 | const [newUsername, setNewUsername] = useState(username); 28 | const handlenewUsernameChange = (e) => { 29 | const value = e.target.value; 30 | setNewUsername(value); 31 | } 32 | const handleSubmit = (e) => { 33 | e.preventDefault(); 34 | setUsername(newUsername); 35 | } 36 | return ( 37 |
38 | 39 | 40 | 49 | 50 | 51 | 60 | 61 | 62 |
63 | ); 64 | } 65 | 66 | UserSelectionForm.propTypes = { 67 | username: PropTypes.string, 68 | setUsername: PropTypes.func.isRequired 69 | }; 70 | -------------------------------------------------------------------------------- /src/helpers/localization.js: -------------------------------------------------------------------------------- 1 | import intl from 'react-intl-universal'; 2 | import locales from '../assets/locales/index'; 3 | 4 | const DEFAULT_LOCALE = 'en-US'; 5 | 6 | intl.init({ 7 | currentLocale: DEFAULT_LOCALE, 8 | locales 9 | }); 10 | 11 | export default intl; 12 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import * as serviceWorker from './serviceWorker'; 5 | import './helpers/localization'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/pages/Pages.container.jsx: -------------------------------------------------------------------------------- 1 | import { connect } from 'react-redux'; 2 | import Pages from './Pages'; 3 | import * as userSelectors from '../store/user/selectors'; 4 | 5 | export default connect( 6 | (state) => ({ 7 | isEditingUsername: userSelectors.isEditingUsername(state) 8 | }) 9 | )(Pages); 10 | -------------------------------------------------------------------------------- /src/pages/Pages.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { BrowserRouter, Switch, Route } from 'react-router-dom'; 4 | import PublicRepositoriesList from './PublicRepositoriesList.container'; 5 | import UserSelection from './UserSelection'; 6 | 7 | export default function Pages({ 8 | isEditingUsername 9 | }) { 10 | return ( 11 | 12 | 13 | {isEditingUsername && ( 14 | 18 | )} 19 | 23 | 24 | 25 | ); 26 | } 27 | 28 | Pages.propTypes = { 29 | isEditingUsername: PropTypes.bool 30 | }; 31 | -------------------------------------------------------------------------------- /src/pages/PublicRepositoriesList.container.jsx: -------------------------------------------------------------------------------- 1 | import { connect } from 'react-redux'; 2 | import PublicRepositoriesList from './PublicRepositoriesList'; 3 | import * as repositoriesSelectors from '../store/repositories/selectors'; 4 | import * as userSelectors from '../store/user/selectors'; 5 | import * as repositoriesActions from '../store/repositories/actions'; 6 | 7 | export default connect( 8 | (state) => ({ 9 | repositories: repositoriesSelectors.getList(state), 10 | isFetching: repositoriesSelectors.isFetching(state), 11 | username: userSelectors.getUsername(state), 12 | hasError: repositoriesSelectors.hasError(state) 13 | }), 14 | repositoriesActions 15 | )(PublicRepositoriesList); 16 | -------------------------------------------------------------------------------- /src/pages/PublicRepositoriesList.jsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { 4 | Container, 5 | Typography 6 | } from '@material-ui/core'; 7 | import { makeStyles } from '@material-ui/styles'; 8 | import intl from 'react-intl-universal'; 9 | import LoadingIndicator from '../components/LoadingIndicator'; 10 | import UserRepositoriesList from '../components/UserRepositories/UserRepositoriesList'; 11 | 12 | const useStyles = makeStyles(theme => ({ 13 | header: { 14 | margin: theme.spacing(3, 0, 2, 0) 15 | }, 16 | loadingIndicator: { 17 | margin: 'auto', 18 | display: 'block' 19 | } 20 | })); 21 | 22 | export default function PublicRepositoriesList({ 23 | repositories, 24 | fetchRepositories, 25 | username, 26 | isFetching, 27 | hasError 28 | }) { 29 | const classes = useStyles(); 30 | useEffect(() => { 31 | fetchRepositories(username); 32 | }, [username, fetchRepositories]); 33 | return ( 34 | 35 | 36 | {intl.get('repositories.header', { username })} 37 | 38 | 43 | 49 | 50 | ); 51 | } 52 | 53 | PublicRepositoriesList.propTypes = { 54 | repositories: PropTypes.array, 55 | username: PropTypes.string.isRequired, 56 | fetchRepositories: PropTypes.func.isRequired, 57 | isFetching: PropTypes.bool, 58 | hasError: PropTypes.bool 59 | }; 60 | 61 | PublicRepositoriesList.defaultProps = { 62 | repositories: [] 63 | }; 64 | -------------------------------------------------------------------------------- /src/pages/UserSelection.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Container, Paper } from '@material-ui/core'; 3 | import { makeStyles } from '@material-ui/styles'; 4 | import UserSelectionForm from '../components/UserSelectionForm.container'; 5 | 6 | const useStyles = makeStyles(theme => ({ 7 | paper: { 8 | padding: theme.spacing(2) 9 | } 10 | })); 11 | 12 | export default function UserSelection() { 13 | const classes = useStyles(); 14 | return ( 15 | 16 | 17 | 18 | 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/services/github.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | 3 | const GITHUB_BASE_URL = 'https://api.github.com'; 4 | const axiosGithub = axios.create({ 5 | baseURL: GITHUB_BASE_URL 6 | }); 7 | 8 | export async function getUserRepos(username) { 9 | const response = await axiosGithub.get(`/users/${username}/repos?sort=updated_at&order=desc`); 10 | return response.data; 11 | } 12 | -------------------------------------------------------------------------------- /src/store/index.js: -------------------------------------------------------------------------------- 1 | import { createStore, combineReducers, applyMiddleware } from 'redux'; 2 | import thunk from 'redux-thunk'; 3 | import user from './user/reducer'; 4 | import repositories from './repositories/reducer'; 5 | 6 | const rootReducer = combineReducers({ 7 | user, 8 | repositories 9 | }); 10 | 11 | export default () => createStore(rootReducer, applyMiddleware(thunk)); 12 | -------------------------------------------------------------------------------- /src/store/repositories/actionTypes.js: -------------------------------------------------------------------------------- 1 | export default { 2 | FETCH_REPOSITORIES_START: 'FETCH_REPOSITORIES_START', 3 | FETCH_REPOSITORIES_SUCCESS: 'FETCH_REPOSITORIES_SUCCESS', 4 | FETCH_REPOSITORIES_FAILURE: 'FETCH_REPOSITORIES_FAILURE', 5 | CLEAR_REPOSITORIES: 'CLEAR_REPOSITORIES' 6 | }; 7 | -------------------------------------------------------------------------------- /src/store/repositories/actions.js: -------------------------------------------------------------------------------- 1 | import ACTION_TYPES from './actionTypes'; 2 | import { getUserRepos } from '../../services/github'; 3 | 4 | export function fetchRepositories(username) { 5 | return async (dispatch) => { 6 | dispatch({ 7 | type: ACTION_TYPES.FETCH_REPOSITORIES_START 8 | }); 9 | try { 10 | const repositories = await getUserRepos(username); 11 | dispatch({ 12 | type: ACTION_TYPES.FETCH_REPOSITORIES_SUCCESS, 13 | payload: repositories 14 | }); 15 | } catch (err) { 16 | dispatch({ 17 | type: ACTION_TYPES.FETCH_REPOSITORIES_FAILURE, 18 | payload: err 19 | }); 20 | } 21 | }; 22 | } 23 | 24 | export function clearRepositories() { 25 | return { 26 | type: ACTION_TYPES.CLEAR_REPOSITORIES, 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /src/store/repositories/reducer.js: -------------------------------------------------------------------------------- 1 | import ACTION_TYPES from './actionTypes'; 2 | 3 | const initialState = { 4 | repositories: [], 5 | isFetching: true, 6 | fetchError: null 7 | }; 8 | 9 | export default function reducer(state = initialState, { type, payload }) { 10 | switch (type) { 11 | case ACTION_TYPES.FETCH_REPOSITORIES_START: 12 | return { 13 | ...state, 14 | isFetching: true, 15 | fetchError: null 16 | }; 17 | case ACTION_TYPES.FETCH_REPOSITORIES_SUCCESS: 18 | return { 19 | ...state, 20 | isFetching: false, 21 | repositories: payload 22 | }; 23 | case ACTION_TYPES.FETCH_REPOSITORIES_FAILURE: 24 | return { 25 | ...state, 26 | isFetching: false, 27 | fetchError: payload 28 | }; 29 | case ACTION_TYPES.CLEAR_REPOSITORIES: 30 | return { 31 | ...state, 32 | repositories: [], 33 | fetchError: null 34 | }; 35 | default: 36 | return state; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/store/repositories/selectors.js: -------------------------------------------------------------------------------- 1 | export function getList(state) { 2 | return state.repositories.repositories; 3 | } 4 | 5 | export function isFetching(state) { 6 | return state.repositories.isFetching; 7 | } 8 | 9 | export function hasError(state) { 10 | return Boolean(state.repositories.fetchError); 11 | } 12 | -------------------------------------------------------------------------------- /src/store/user/actionTypes.js: -------------------------------------------------------------------------------- 1 | export default { 2 | SET_NAME: 'SET_NAME', 3 | SET_EDITING_NAME: 'SET_EDITING_NAME' 4 | }; 5 | -------------------------------------------------------------------------------- /src/store/user/actions.js: -------------------------------------------------------------------------------- 1 | import ACTION_TYPES from './actionTypes'; 2 | 3 | export function setUsername(payload) { 4 | return { 5 | type: ACTION_TYPES.SET_NAME, 6 | payload 7 | }; 8 | } 9 | 10 | export function setIsEditingUsername(payload) { 11 | return { 12 | type: ACTION_TYPES.SET_EDITING_NAME, 13 | payload 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /src/store/user/reducer.js: -------------------------------------------------------------------------------- 1 | import ACTION_TYPES from './actionTypes'; 2 | 3 | const initialState = { 4 | name: 'AntonRublev360', 5 | isEditing: true 6 | }; 7 | 8 | export default function reducer(state = initialState, { type, payload }) { 9 | switch (type) { 10 | case ACTION_TYPES.SET_NAME: 11 | return { 12 | ...state, 13 | name: payload, 14 | isEditing: false 15 | }; 16 | case ACTION_TYPES.SET_EDITING_NAME: 17 | return { 18 | ...state, 19 | isEditing: payload 20 | }; 21 | default: 22 | return state; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/store/user/selectors.js: -------------------------------------------------------------------------------- 1 | export function getUsername(state) { 2 | return state.user.name; 3 | } 4 | 5 | export function isEditingUsername(state) { 6 | return state.user.isEditing; 7 | } 8 | -------------------------------------------------------------------------------- /src/test/fixtures/github.js: -------------------------------------------------------------------------------- 1 | export const FAKE_USERNAME_WITH_REPOS = 'FAKE_USERNAME_WITH_REPOS'; 2 | export const FAKE_USERNAME_WITHOUT_REPOS = 'FAKE_USERNAME_WITHOUT_REPOS'; 3 | export const FAKE_BAD_USERNAME = 'FAKE_BAD_USERNAME'; 4 | export const REPOS_LIST = [ 5 | { 6 | name: 'My test repository', 7 | description: 'My test repository description', 8 | id: 11111111, 9 | html_url: 'https://github.com/fake1' 10 | }, 11 | { 12 | name: 'Another test repository', 13 | description: 'Another test repository description', 14 | id: 22222222, 15 | html_url: 'https://github.com/fake2' 16 | }, 17 | { 18 | name: 'Third test repository', 19 | description: 'Third test repository description', 20 | id: 33333333, 21 | html_url: 'https://github.com/fake3' 22 | } 23 | ]; 24 | -------------------------------------------------------------------------------- /src/test/helpers/initTestLocalization.js: -------------------------------------------------------------------------------- 1 | import intl from 'react-intl-universal'; 2 | import locales from '../../assets/locales/index'; 3 | 4 | const DEFAULT_LOCALE = 'en-US'; 5 | 6 | intl.init({ 7 | currentLocale: DEFAULT_LOCALE, 8 | locales: { 9 | [DEFAULT_LOCALE]: createTestLocale(locales[DEFAULT_LOCALE]) 10 | } 11 | }); 12 | 13 | function createTestLocale(localesObj, path = '') { 14 | return Object.entries(localesObj).reduce((testLocalesObj, [key, value]) => { 15 | if (typeof value === 'string') { 16 | testLocalesObj[key] = `${path}${key}`; 17 | } else { 18 | testLocalesObj[key] = createTestLocale(value, `${key}.`); 19 | } 20 | return testLocalesObj; 21 | }, {}); 22 | } 23 | 24 | export default intl; 25 | -------------------------------------------------------------------------------- /src/test/viewGitHubRepositoriesByUsername.spec.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { 3 | render, 4 | cleanup, 5 | waitForElement 6 | } from '@testing-library/react'; 7 | import userEvent from '@testing-library/user-event' 8 | import 'jest-dom/extend-expect'; 9 | import nock from 'nock'; 10 | import './helpers/initTestLocalization'; 11 | import App from '../App'; 12 | import { 13 | FAKE_USERNAME_WITH_REPOS, 14 | FAKE_USERNAME_WITHOUT_REPOS, 15 | FAKE_BAD_USERNAME, 16 | REPOS_LIST 17 | } from './fixtures/github'; 18 | 19 | describe('view GitHub repositories by username', () => { 20 | beforeAll(() => { 21 | nock('https://api.github.com') 22 | .persist() 23 | .get(`/users/${FAKE_USERNAME_WITH_REPOS}/repos`) 24 | .query(true) 25 | .reply(200, REPOS_LIST) 26 | .get(`/users/${FAKE_USERNAME_WITHOUT_REPOS}/repos`) 27 | .query(true) 28 | .reply(200, []) 29 | .get(`/users/${FAKE_BAD_USERNAME}/repos`) 30 | .query(true) 31 | .reply(404);; 32 | }); 33 | 34 | afterEach(cleanup); 35 | 36 | describe('when GitHub user has public repositories', () => { 37 | it('user can view the list of public repositories for entered GitHub username', async () => { 38 | const { getByText, getByPlaceholderText, queryByText } = render(); 39 | userEvent.type(getByPlaceholderText('userSelection.usernamePlaceholder'), FAKE_USERNAME_WITH_REPOS); 40 | expect(getByPlaceholderText('userSelection.usernamePlaceholder')).toHaveAttribute('value', FAKE_USERNAME_WITH_REPOS); 41 | userEvent.click(getByText('userSelection.submitButtonText').closest('button')); 42 | getByText('repositories.header'); 43 | await waitForElement(() => getByText('repositories.loadingText')); 44 | expect(queryByText('repositories.empty')).toBeNull(); 45 | await waitForElement(() => REPOS_LIST.reduce((elementsToWaitFor, repository) => { 46 | elementsToWaitFor.push(getByText(repository.name)); 47 | elementsToWaitFor.push(getByText(repository.description)); 48 | return elementsToWaitFor; 49 | }, [])); 50 | expect(queryByText('repositories.loadingText')).toBeNull(); 51 | expect(queryByText('repositories.error')).toBeNull(); 52 | }); 53 | }); 54 | 55 | describe('when GitHub user has no public repositories', () => { 56 | it('user is presented with a message that there are no public repositories for entered GitHub username', async () => { 57 | const { getByText, getByPlaceholderText, queryByText } = render(); 58 | userEvent.type(getByPlaceholderText('userSelection.usernamePlaceholder'), FAKE_USERNAME_WITHOUT_REPOS); 59 | expect(getByPlaceholderText('userSelection.usernamePlaceholder')).toHaveAttribute('value', FAKE_USERNAME_WITHOUT_REPOS); 60 | userEvent.click(getByText('userSelection.submitButtonText').closest('button')); 61 | getByText('repositories.header'); 62 | await waitForElement(() => getByText('repositories.loadingText')); 63 | expect(queryByText('repositories.empty')).toBeNull(); 64 | await waitForElement(() => getByText('repositories.empty')); 65 | expect(queryByText('repositories.error')).toBeNull(); 66 | }); 67 | }); 68 | 69 | describe('when GitHub user does not exist', () => { 70 | it('user is presented with an error message', async () => { 71 | const { getByText, getByPlaceholderText, queryByText } = render(); 72 | userEvent.type(getByPlaceholderText('userSelection.usernamePlaceholder'), FAKE_BAD_USERNAME); 73 | expect(getByPlaceholderText('userSelection.usernamePlaceholder')).toHaveAttribute('value', FAKE_BAD_USERNAME); 74 | userEvent.click(getByText('userSelection.submitButtonText').closest('button')); 75 | getByText('repositories.header'); 76 | await waitForElement(() => getByText('repositories.loadingText')); 77 | expect(queryByText('repositories.empty')).toBeNull(); 78 | await waitForElement(() => getByText('repositories.error')); 79 | expect(queryByText('repositories.empty')).toBeNull(); 80 | }); 81 | }); 82 | }); 83 | --------------------------------------------------------------------------------