This repository contains the sample code for the Front-End Frameworks: React (Part 1, Part 2) episodes of James Shore's Let's Code: Test-Driven JavaScript screencast. Let's Code: Test-Driven JavaScript is a screencast series focused on rigorous, professional JavaScript development.
This episode is an exploration and review of the React library for building front-end user interfaces. This repository contains an example application written in React. It demonstrates several concepts:
- Sample application. The sample application code can be found in
src/client
. The app runs insideindex.html
. It launches frommain.js
. The UI code is in the JSX files insrc/client/ui
, starting withapplication_ui.jsx
.
The application also includes a domain layer, value objects, and a (simulated) persistence layer. Those layers are in subdirectories of src/client
. See the screencast for more information about the sample app. (In particular, the way value objects are rendered was an experiment to see how well React could deal with an unusual architecture, not a pattern to follow in your own applications.)
-
Unit Tests. All the code is unit tested. You can find tests of the React code in
src/client/ui/_*_test.jsx
. The React tests demonstrate three different approaches: string comparison usingReact.renderComponentToStaticMarkup()
, and targeted DOM comparisons using the TestUtils library (the second test), and a hybrid approach for comparing React components directly. (The hybrid approach is very useful, but it pokes into React internals, so you may want to avoid it or limit your use.) -
Production-mode JSX transformation. We use Jake to translate React's JSX language to JavaScript. See the "compileJsx" target in
Jakefile.js
andbuild/util/jsx_runner.js
for example code. You can usejsx_runner.js
in your own build scripts. -
Modularity. We use CommonJS
require()
statements for modularity, and we use Browserify to make it work. See the sample application for an example of how the modularity works in practice. See the "browserify" target inJakefile.js
andbuild/util/browserify_runner.js
for the automated build. You can usebrowserify_runner.js
in your own build scripts.
For further details about how this code works, watch the screencast.
To try this code on your own computer:
- Install Node.js.
- Download and unzip the source code into a convenient directory.
- All commands must run from the root of the source tree:
cd <directory>
.
- Run
./jake.sh run
(Unix/Mac) orjake run
(Windows) - Open
http://localhost:8080
in a browser.
- Run
./jake.sh karma
(Unix/Mac) orjake karma
(Windows) to start the Karma server. - Start the browsers you want to test and point each one at
http://localhost:9876
. - Run
./jake.sh
(Unix/Mac) orjake
(Windows) every time you want to build and test.
MIT License. See LICENSE.TXT
.