React Redux Starter Code
A minimal app to be used as starter code for labs and homework in the SOFT 260 course at UNL. The starter code demonstrates basic usage of the React Hooks API, the Redux Toolkit (RTK), React Router, Jest, and the React Testing Library (RTL) in the context of a React Redux progressive web app (PWA).
Quick Start
Recursively clone this repository and cd into the root folder:
$ git clone --recursive git@git.unl.edu:soft-core/soft-260/react-redux-starter-code.git
$ cd react-redux-starter-code
(If you forget --recursive when cloning, you can cd into your clone and run
git submodule update --init --recursive instead.)
Install dependencies:
$ npm install
(Near the end you may see some warnings because create-react-app transitively
depends on some deprecated packages.)
Optionally run the linter and the test suite:
$ npm run lint
$ npm run test
And then serve the application locally:
$ npm start
When you are done, press control-c to stop the server.
Folders and Files
The folders and files in the starter code are briefly described below. React Redux applications use a model-view-controller (MVC) architecture (see https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), so in these descriptions the terms "model", "view", and "controller" refer to those roles from MVC.
Submodules
-
The Git submodule
stylelint-configcontains the stylelint configuration for the coding style used in theminimal-appproject. -
The Git submodule
eslint-configcontains the ESLint configuration for the coding style used in theminimal-appproject. Percreate-react-appconvention, in a development build of the main app, a separate, weaker coding style also warns at runtime about likely bugs.
General Configuration
-
The file
minimal-app/.gitignoreprevents non-source-code files from being accidentally committed to the repository. -
The file
minimal-app/package.jsondescribes the project and its dependencies. It can be edited to customize the inputs and processes used in various software lifecyle tasks like linting, testing, or deploying. -
The file
minimal-app/package-lock.jsonrecords the exact set of dependencies used to satisfy the requirements inminimal-app/package.json. It should not be hand-edited; use commands likenpm installornpm uninstallfrom theminimal-appdirectory to make changes. -
The folder
minimal-app/node_modulescontains the dependencies installed bynpm.
Application Infrastructure
-
The file
minimal-app/public/manifest.jsonprovides data needed to run the web application as a PWA. (See https://developer.mozilla.org/en-US/docs/Web/Manifest for more information.) -
The file
minimal-app/public/logo.svgis the image used for the app's icon. (A maskable icon is recommended; see https://web.dev/maskable-icon/ for more information and https://maskable.app/ to test an image.) -
The file
minimal-app/public/index.htmlcontains the skeleton document in which the app's HTML is embedded. -
The file
minimal-app/src/index.jsspecifies other wrappers around the application proper than cannot be given inindex.html, usually because they involve React components, not pure HTML. In this case, the wrappers:-
Enable extra checks at development time using React strict mode. (See https://reactjs.org/docs/strict-mode.html for more information.)
-
Make the Redux store from
minimal-app/src/app/store.jsavailable to the app's React components. -
Enable routing with React Router. (See https://reacttraining.com/react-router/web for more information.)
-
Apply the letterboxed portrait layout from
index.css, which is described next.
-
-
The file
minimal-app/src/index.csscontains the CSS associated withindex.html. In this case the CSS forces a letterboxed portrait layout and specifies a sans-serif font. -
The file
minimal-app/src/service-worker.jsacts as a proxy server, which among other things makes it possible to run a PWA while offline. (See https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API for more information.) -
The file
minimal-app/src/serviceWorkerRegistration.jsregisters the service worker so that the browser knows about it.
Model and Controller Code
-
The file
minimal-app/src/app/store.jscombines the models provided by the app's features to create a model for the whole app. -
The file
minimal-app/src/features/counter/counterSlice.jsimplements a Redux slice, the model and associated controllers for a particular feature, which in this case is a simple counter. (See https://redux-toolkit.js.org/ for more information.)
View Code
-
The file
minimal-app/src/app.jsimplements the React component representing the entire app. In this case only one route,/, is implemented, but the returned fragment is intentionally written so that it is easy to add additional routes. -
The file
minimal-app/src/features/counter/counter.jsimplements a React component that counts the number of times a user has tapped a button. This component uses the slice fromcounterSlice.js. -
The file
minimal-app/src/features/counter/counter.module.cssprovides styles for the React component incounter.js.
Test Infrastructure
-
The file
minimal-app/src/setupTests.jsprovides setup code that applies to every test case. In this case that code imports RTL's custom matchers from testing React components. -
The file
minimal-app/src/testing/mockRedux.jsprovides the ability to mock the part of Redux used by React components under the React Hooks API so that those view components can be tested independently of model and controller code.
Test Code
-
The file
minimal-app/app.test.jsdemonstrates a snapshot regression test of the app component with mocked selectors. (See https://jestjs.io/docs/en/snapshot-testing for more information.) -
The file
minimal-app/src/__snapshots__/app.test.js.snapcontains the oracles for the snapshot tests inapp.test.js. When the tests are run in watch mode (using the commandnpm test), these oracles can be updated interactively if a snapshot test fails due to changed requirements. -
The file
minimal-app/src/features/counter/counter.test.jsdemonstrates non-snapshot tests of the view code in the counter component. -
The file
minimal-app/src/features/counter/counterSlice.test.jsdemonstrates tests of model and controller code.
Adaptation Checklist
When adapting this code for a new project, make sure to do at least the following:
-
Change the project name, version number, and description in
minimal-app/package.json. -
Change the short name, name, description, colors, and other settings in
minimal-app/public/manifest.json. -
Change the title, description, and theme color in
minimal-app/public/index.html. -
Rename the folder from
minimal-appto something descriptive and change the corresponding entries in the outerpackage.json. -
Rerun
npm installto updatepackage-lock.jsonbased on the above changes.