|
1 | | -# TWD Example: React Router v7 Data Mode |
| 1 | +# TWD + React Router v7 (Data Mode) |
2 | 2 |
|
3 | | -This example demonstrates the data mode of React Router v7, which is my favorite mode for developing single-page applications (SPAs). It is the simplest approach because it leverages web standards, allowing you to define loaders and actions directly on routes for seamless data fetching and mutation. By using the React Router approach, you can avoid relying on additional state management or request management tools, keeping your application lightweight and straightforward. |
| 3 | +This repo demonstrates React Router v7 in data mode, tested end‑to‑end with TWD (`twd-js`). The app uses route loaders/actions for data, and TWD provides a browser‑native test runner you can use interactively during development or headlessly in CI. |
4 | 4 |
|
5 | | -This example will include twd-js and test the application with TWD. |
| 5 | +For an overview of React Router data mode, see the [API and mode availability](https://reactrouter.com/start/modes#api--mode-availability-table). |
6 | 6 |
|
7 | | -For a detailed overview of the features available in data mode, refer to the [React Router API and Mode Availability Table](https://reactrouter.com/start/modes#api--mode-availability-table). |
| 7 | +## Quick Start |
8 | 8 |
|
9 | | -## Installation |
| 9 | +- Install deps |
10 | 10 |
|
11 | | -To use this mode, install the React Router library: |
| 11 | +```bash |
| 12 | +npm install |
| 13 | +``` |
| 14 | + |
| 15 | +- Start dev server + mock API (json‑server) |
12 | 16 |
|
13 | 17 | ```bash |
14 | | -npm install react-router |
| 18 | +npm run serve:dev |
| 19 | +``` |
| 20 | + |
| 21 | +Open the app at the Vite URL printed in the terminal. In dev, the TWD sidebar appears automatically and discovers tests from `.twd.test.ts(x)` files. |
| 22 | + |
| 23 | +## Where TWD Lives |
| 24 | + |
| 25 | +- Tests: [src/twd-tests](src/twd-tests) |
| 26 | + - Examples: [src/twd-tests/helloWorld.twd.test.ts](src/twd-tests/helloWorld.twd.test.ts), [src/twd-tests/qrScanner.twd.test.tsx](src/twd-tests/qrScanner.twd.test.tsx), [src/twd-tests/todoList.twd.test.ts](src/twd-tests/todoList.twd.test.ts) |
| 27 | +- Dev wiring: TWD is initialized in [src/main.tsx](src/main.tsx) with `initTWD()` and `import.meta.glob("./**/*.twd.test.ts")` so tests are auto‑discovered in development. |
| 28 | + |
| 29 | +### TWD Test Basics |
| 30 | + |
| 31 | +Common helpers used in this repo: |
| 32 | + |
| 33 | +```ts |
| 34 | +import { describe, it } from "twd-js/runner"; |
| 35 | +import { twd, screenDom, userEvent, expect } from "twd-js"; |
| 36 | + |
| 37 | +describe("Example", () => { |
| 38 | + it("navigates and asserts", async () => { |
| 39 | + await twd.visit("/"); |
| 40 | + const title = await screenDom.getByText("Welcome to TWD"); |
| 41 | + twd.should(title, "be.visible"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("mocks requests", async () => { |
| 45 | + await twd.mockRequest("getTodoList", { method: "GET", url: "/api/todos", response: [], status: 200 }); |
| 46 | + await twd.visit("/todos"); |
| 47 | + await twd.waitForRequest("getTodoList"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("mocks components", async () => { |
| 51 | + twd.mockComponent("qrScanner", ({ onScan }) => ( |
| 52 | + <button onClick={() => onScan([{ rawValue: "123" }])}>Mock scan</button> |
| 53 | + )); |
| 54 | + await twd.visit("/qr-scanner"); |
| 55 | + }); |
| 56 | +}); |
15 | 57 | ``` |
16 | 58 |
|
17 | | -## Running This Example |
| 59 | +## Running Tests |
18 | 60 |
|
19 | | -Follow these steps to run the example: |
| 61 | +### Interactive (recommended during development) |
| 62 | +- Use `npm run serve:dev` and open the app. |
| 63 | +- Trigger tests from the TWD sidebar in the browser (run all or individual tests). |
20 | 64 |
|
21 | | -1. **Install Dependencies** |
22 | | - Navigate to the project directory and install the required dependencies: |
| 65 | +### Headless CI with coverage |
| 66 | +Run Vite with coverage instrumentation and the mock API, then execute the TWD CI runner: |
23 | 67 |
|
24 | | - ```bash |
25 | | - npm install |
26 | | - ``` |
| 68 | +```bash |
| 69 | +# Terminal 1: mock API |
| 70 | +npm run serve |
27 | 71 |
|
28 | | -2. **Start the Development Server** |
29 | | - Run the following command to start the development server and the fake data which is provided by [json-sever](https://www.npmjs.com/package/json-server). |
| 72 | +# Terminal 2: Vite dev server with coverage env |
| 73 | +npm run dev:ci |
30 | 74 |
|
31 | | - ```bash |
32 | | - npm run serve:dev |
33 | | - ``` |
| 75 | +# Terminal 3: headless TWD runner (uses Puppeteer) |
| 76 | +npm run test:ci |
34 | 77 |
|
35 | | -3. **Access the Application** |
36 | | - Open your browser and navigate to the URL provided in the terminal to view the application. |
| 78 | +# Optional: generate coverage reports into ./coverage |
| 79 | +npm run collect:coverage:html |
| 80 | +# or |
| 81 | +npm run collect:coverage:lcov |
| 82 | +npm run collect:coverage:text |
| 83 | +``` |
| 84 | + |
| 85 | +Notes: |
| 86 | +- `dev:ci` sets `CI=true` so `vite-plugin-istanbul` instruments code. The CI runner writes `.nyc_output/out.json`; the `collect:coverage:*` scripts turn that into reports. |
| 87 | +- The CI runner opens the dev server at `http://localhost:5173` and executes all discovered tests. |
37 | 88 |
|
38 | | ---- |
| 89 | +## Running the App Only |
39 | 90 |
|
40 | | -Feel free to explore the code in this folder to understand how declarative routing is implemented in this example. |
| 91 | +If you just want to browse the app without tests: |
41 | 92 |
|
42 | | -## Tools Used in This Example |
| 93 | +```bash |
| 94 | +npm run serve:dev |
| 95 | +``` |
43 | 96 |
|
44 | | -This example leverages the following tools: |
| 97 | +## Tools |
45 | 98 |
|
46 | | -- **React Router**: A library for declarative routing in React applications. |
47 | | -- **json-server**: A simple tool to create a fake REST API for testing and prototyping. |
48 | | -- **twd-js**: A library for testing React applications with TWD. |
| 99 | +- **React Router**: declarative routing with loaders/actions. |
| 100 | +- **json-server**: mock REST API ([data/data.json](data/data.json), [data/routes.json](data/routes.json)). |
| 101 | +- **twd-js**: interactive and headless testing for web apps. |
| 102 | +- **Vite + vite-plugin-istanbul**: dev server and coverage instrumentation. |
| 103 | +- **Puppeteer + NYC**: headless browser execution and coverage reporting. |
49 | 104 |
|
50 | | -These tools help demonstrate how data mode works in a practical scenario, with React Router handling navigation and data fetching, json-server providing mock data for the application, and React Router's built-in loaders and actions managing application state seamlessly without requiring additional state management libraries. |
| 105 | +Explore the app routes and loaders in [src/AppRoutes.tsx](src/AppRoutes.tsx). TWD enables fast, realistic tests without extra state or request management libraries. |
51 | 106 |
|
0 commit comments