Skip to content

Commit 8cbaad4

Browse files
committed
docs: update readme
1 parent caf3998 commit 8cbaad4

1 file changed

Lines changed: 84 additions & 29 deletions

File tree

README.md

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,106 @@
1-
# TWD Example: React Router v7 Data Mode
1+
# TWD + React Router v7 (Data Mode)
22

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.
44

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).
66

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
88

9-
## Installation
9+
- Install deps
1010

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)
1216

1317
```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+
});
1557
```
1658

17-
## Running This Example
59+
## Running Tests
1860

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).
2064

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:
2367

24-
```bash
25-
npm install
26-
```
68+
```bash
69+
# Terminal 1: mock API
70+
npm run serve
2771

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
3074

31-
```bash
32-
npm run serve:dev
33-
```
75+
# Terminal 3: headless TWD runner (uses Puppeteer)
76+
npm run test:ci
3477

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.
3788

38-
---
89+
## Running the App Only
3990

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:
4192

42-
## Tools Used in This Example
93+
```bash
94+
npm run serve:dev
95+
```
4396

44-
This example leverages the following tools:
97+
## Tools
4598

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.
49104

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.
51106

0 commit comments

Comments
 (0)