Skip to content

Commit 00f59ef

Browse files
authored
Feat/selector rta (#85)
* feat: add examples react testing library selects * fix: error api definition * docs: update mocks * test: add screen dom testing * feat: add tests and should as assertion command * docs: add testing library selectors docs * docs: more selector docs * docs: simplify pr template
1 parent 10015ea commit 00f59ef

23 files changed

Lines changed: 1402 additions & 166 deletions

.github/pull_request_template.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ Please delete options that are not relevant:
1616
- [ ] Performance improvement
1717
- [ ] Test addition or update
1818

19-
## Related Issue
20-
21-
Closes # (issue number)
22-
2319
## Changes Made
2420

2521
-
@@ -39,14 +35,6 @@ Closes # (issue number)
3935

4036
Describe the tests you've added or updated:
4137

42-
```typescript
43-
// Example of test added
44-
describe("New feature", () => {
45-
it("should work correctly", () => {
46-
// ...
47-
});
48-
});
49-
```
5038

5139
## Documentation
5240

@@ -63,17 +51,6 @@ List any documentation files you've updated:
6351
- [ ] docs/...
6452
- [ ] API documentation
6553

66-
## Checklist
67-
68-
- [ ] My code follows the project's style guidelines
69-
- [ ] I have performed a self-review of my own code
70-
- [ ] I have commented my code, particularly in hard-to-understand areas
71-
- [ ] I have made corresponding changes to the documentation
72-
- [ ] My changes generate no new warnings
73-
- [ ] I have added tests that prove my fix is effective or that my feature works
74-
- [ ] New and existing unit tests pass locally with my changes
75-
- [ ] Any dependent changes have been merged and published
76-
7754
## Screenshots (if applicable)
7855

7956
If your changes affect the UI or add visual features, please add screenshots:

README.md

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,35 @@ pnpm add twd-js
109109

110110
```ts
111111
// src/app.twd.test.ts
112-
import { twd, userEvent } from "twd-js";
112+
import { twd, userEvent, screenDom } from "twd-js";
113113
import { describe, it } from "twd-js/runner";
114114

115115
describe("Hello World Page", () => {
116116
it("should display the welcome title and counter button", async () => {
117117
await twd.visit("/");
118118

119-
const title = await twd.get("[data-testid='welcome-title']");
119+
// Option 1: Use TWD's native selectors
120+
const title = await twd.get("h1");
120121
title.should("be.visible").should("have.text", "Welcome to TWD");
121122

122-
const counterButton = await twd.get("[data-testid='counter-button']");
123+
const counterButton = await twd.get("button");
123124
counterButton.should("be.visible").should("have.text", "Count is 0");
124125

125-
await userEvent.click(counterButton.el);
126+
const user = userEvent.setup();
127+
await user.click(counterButton.el);
126128
counterButton.should("have.text", "Count is 1");
129+
130+
// Option 2: Use Testing Library queries (semantic, accessible)
131+
// const title = screenDom.getByRole("heading", { name: /welcome to twd/i });
132+
// twd.should(title, "be.visible");
133+
// twd.should(title, "have.text", "Welcome to TWD");
134+
//
135+
// const counterButton = screenDom.getByRole("button", { name: /count is/i });
136+
// twd.should(counterButton, "be.visible");
137+
// twd.should(counterButton, "have.text", "Count is 0");
138+
//
139+
// await user.click(counterButton);
140+
// twd.should(counterButton, "have.text", "Count is 1");
127141
});
128142
});
129143
```
@@ -216,8 +230,9 @@ describe("User authentication", () => {
216230

217231
### Element Selection
218232

219-
TWD provides two main methods for selecting elements:
233+
TWD provides multiple ways to select elements:
220234

235+
**TWD Native Selectors:**
221236
```ts
222237
// Select a single element
223238
const button = await twd.get("button");
@@ -228,6 +243,22 @@ const items = await twd.getAll(".item");
228243
items[0].should("be.visible");
229244
```
230245

246+
**Testing Library Queries (also supported):**
247+
```ts
248+
import { screenDom, twd } from "twd-js";
249+
250+
// Semantic, accessible queries
251+
const button = screenDom.getByRole("button", { name: /submit/i });
252+
const input = screenDom.getByLabelText("Email:");
253+
const heading = screenDom.getByRole("heading", { name: "Welcome" });
254+
255+
// Use twd.should() for assertions (not .should() method)
256+
twd.should(button, "be.visible");
257+
twd.should(input, "have.value", "user@example.com");
258+
```
259+
260+
Both approaches work together seamlessly. See the [Testing Library docs](/api/react-testing-library) for more details.
261+
231262
### Assertions
232263

233264
TWD includes a comprehensive set of assertions for testing element states:
@@ -265,21 +296,21 @@ twd.url().should("contain.url", "/contact");
265296
TWD integrates with `@testing-library/user-event` for realistic user interactions:
266297

267298
```ts
268-
import { userEvent } from "twd-js";
299+
import { userEvent, screenDom } from "twd-js";
269300
270301
const user = userEvent.setup();
271-
const button = await twd.get("button");
272-
const input = await twd.get("input");
273302
274-
// Click interactions
303+
// With TWD selectors
304+
const button = await twd.get("button");
275305
await user.click(button.el);
276-
await user.dblClick(button.el);
277306
278-
// Typing
279-
await user.type(input.el, "Hello World");
307+
// With React Testing Library queries
308+
const input = screenDom.getByLabelText("Email:");
309+
await user.type(input, "user@example.com");
280310
281311
// Form interactions
282-
await user.selectOptions(selectElement.el, "option-value");
312+
const select = screenDom.getByRole("combobox");
313+
await user.selectOptions(select, "option-value");
283314
```
284315

285316
## API Mocking
@@ -323,7 +354,7 @@ This plugin will automatically remove `mock-sw.js` from your build output during
323354
Use `twd.mockRequest()` to define API mocks in your tests:
324355

325356
```ts
326-
import { twd } from "twd-js";
357+
import { twd, screenDom, userEvent } from "twd-js";
327358
328359
// Initialize mocking when loading tests
329360
// await twd.initRequestMocking();
@@ -343,8 +374,9 @@ it("fetches user data", async () => {
343374
});
344375
345376
// Trigger the request in your app
346-
const button = await twd.get("button[data-testid='load-user']");
347-
await userEvent.click(button.el);
377+
const button = screenDom.getByRole("button", { name: /load user/i });
378+
const user = userEvent.setup();
379+
await user.click(button);
348380
349381
// Wait for the mock to be called
350382
const rule = await twd.waitForRequest("getUser");
@@ -391,7 +423,10 @@ console.log("Response:", rule.response);
391423
| `twd.getAll(selector)` | Select multiple elements | `await twd.getAll(".item")` |
392424
| `twd.visit(url)` | Navigate to URL | `twd.visit("/contact")` |
393425
| `twd.wait(ms)` | Wait for specified time | `await twd.wait(500)` |
426+
| `twd.should(el, assertion, ...args)` | Assert on any element | `twd.should(button, "be.visible")` |
394427
| `twd.url()` | Get URL API for assertions | `twd.url().should("contain.url", "/home")` |
428+
| `screenDom` | Testing Library queries | `screenDom.getByRole("button")` |
429+
| `userEvent` | User interaction library | `userEvent.setup().click(element)` |
395430

396431
### Assertions
397432

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default defineConfig({
7777
{ text: 'Overview', link: '/api/' },
7878
{ text: 'Test Functions', link: '/api/test-functions' },
7979
{ text: 'TWD Commands', link: '/api/twd-commands' },
80+
{ text: 'Testing Library', link: '/api/react-testing-library' },
8081
{ text: 'Assertions', link: '/api/assertions' },
8182
]
8283
}

docs/api/index.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Complete reference documentation for all TWD functions, methods, and types.
88
|---------|-------------|
99
| [Test Functions](/api/test-functions) | `describe`, `it`, `beforeEach`, `it.only`, `it.skip`, `afterEach` |
1010
| [TWD Commands](/api/twd-commands) | `twd.get()`, `twd.visit()`, `twd.mockRequest()`, etc. |
11+
| [Testing Library](/api/react-testing-library) | `screenDom`, `userEvent` - Full Testing Library support |
1112
| [Assertions](/api/assertions) | All available assertions and their usage |
1213

1314
## Import Reference
@@ -21,6 +22,7 @@ import {
2122
afterEach,
2223
twd,
2324
userEvent,
25+
screenDom,
2426
expect
2527
} from "twd-js";
2628

@@ -121,17 +123,22 @@ describe("Test Suite", () => {
121123
### Element Selection and Interaction
122124

123125
```ts
124-
// Select elements
126+
// TWD native selectors
125127
const element = await twd.get("selector");
126128
const elements = await twd.getAll("selector");
127129

130+
// Testing Library queries (also available)
131+
const button = screenDom.getByRole("button", { name: /submit/i });
132+
const input = screenDom.getByLabelText("Email:");
133+
128134
// Make assertions
129-
element.should("assertion", ...args);
135+
element.should("assertion", ...args); // For twd.get() elements
136+
twd.should(button, "be.visible"); // For screenDom elements
130137

131138
// User interactions
132139
const user = userEvent.setup();
133140
await user.click(element.el);
134-
await user.type(element.el, "text");
141+
await user.type(input, "text");
135142
```
136143

137144
### API Mocking
@@ -226,13 +233,22 @@ message.should('contain.text', 'Success');
226233

227234
#### From Testing Library
228235

236+
TWD now supports Testing Library queries directly! You can use the same `screenDom` API:
237+
229238
```ts
230239
// Testing Library
231240
const button = screen.getByTestId('button');
232241
fireEvent.click(button);
233242
expect(screen.getByTestId('message')).toHaveTextContent('Success');
234243

235-
// TWD
244+
// TWD - Same API!
245+
import { screenDom, userEvent, twd } from 'twd-js';
246+
const button = screenDom.getByTestId('button');
247+
await userEvent.click(button);
248+
const message = screenDom.getByTestId('message');
249+
twd.should(message, 'contain.text', 'Success');
250+
251+
// Or use TWD's native selectors
236252
const button = await twd.get('[data-testid="button"]');
237253
await userEvent.click(button.el);
238254
const message = await twd.get('[data-testid="message"]');

0 commit comments

Comments
 (0)