twd-js 1.6.2 → 1.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,603 +1 @@
1
- import { configure } from '@testing-library/dom';
2
- import { default as default_2 } from '@testing-library/user-event';
3
- import { expect } from 'chai';
4
- import { JSX } from 'react/jsx-runtime';
5
- import { screen as screen_2 } from '@testing-library/dom';
6
-
7
- /**
8
- * All assertion names, including negated ones.
9
- */
10
- declare type AnyAssertion = Negatable_2<AssertionName>;
11
-
12
- /**
13
- * All assertion names, including negated ones.
14
- */
15
- declare type AnyURLAssertion = Negatable<URLAssertionName>;
16
-
17
- /**
18
- * Maps assertion name to its argument tuple.
19
- */
20
- declare type ArgsFor<A extends AnyAssertion> = A extends `not.${infer Base extends AssertionName}` ? AssertionArgs[Base] : A extends AssertionName ? AssertionArgs[A] : never;
21
-
22
- /**
23
- * Argument types for each assertion.
24
- */
25
- declare type AssertionArgs = {
26
- "have.text": [expected: string];
27
- "contain.text": [expected: string];
28
- "be.empty": [];
29
- "have.attr": [attr: string, value: string];
30
- "have.value": [value: string];
31
- "be.disabled": [];
32
- "be.enabled": [];
33
- "be.checked": [];
34
- "be.selected": [];
35
- "be.focused": [];
36
- "be.visible": [];
37
- "have.class": [className: string];
38
- };
39
-
40
- /**
41
- * Types and interfaces for the TWD testing library.
42
- *
43
- * @module twd-types
44
- */
45
- /**
46
- * All supported assertion names for the `should` function.
47
- *
48
- * @example
49
- * api.should("have.text", "Hello");
50
- * api.should("be.empty");
51
- */
52
- declare type AssertionName = "have.text" | "contain.text" | "be.empty" | "have.attr" | "have.value" | "be.disabled" | "be.enabled" | "be.checked" | "be.selected" | "be.focused" | "be.visible" | "have.class";
53
-
54
- export declare const configureScreenDom: typeof configure;
55
-
56
- export declare const defaultTheme: TWDTheme;
57
-
58
- export { expect }
59
-
60
- /**
61
- * Initialize Vite test loading.
62
- * @param testModules - The test modules to load.
63
- * @param component - The React component to render the sidebar.
64
- * @param createRoot - Function to create a React root.
65
- * @param theme - Optional theme customization
66
- * @example
67
- * ```ts
68
- * if (import.meta.env.DEV) {
69
- * const testModules = import.meta.glob("./example.twd.test.ts");
70
- * const { initTests, TWDSidebar } = await import('twd-js');
71
- * await initTests(testModules, <TWDSidebar open={true} position="left" />, createRoot, {
72
- * primary: '#ff0000',
73
- * background: '#ffffff'
74
- * });
75
- * }
76
- * ```
77
- */
78
- export declare const initTests: (testModules: TestModule, Component: React.ReactNode, createRoot: (el: HTMLElement) => {
79
- render: (el: React.ReactNode) => void;
80
- }, theme?: Partial<TWDTheme>) => Promise<void>;
81
-
82
- /**
83
- * Injects theme CSS variables into the document
84
- * This should be called once when the sidebar is initialized
85
- */
86
- export declare function injectTheme(theme?: Partial<TWDTheme>): void;
87
-
88
- /**
89
- * Negatable assertion names (e.g., 'not.have.text').
90
- */
91
- declare type Negatable<T extends string> = T | `not.${T}`;
92
-
93
- /**
94
- * Negatable assertion names (e.g., 'not.have.text').
95
- */
96
- declare type Negatable_2<T extends string> = T | `not.${T}`;
97
-
98
- declare interface Options {
99
- method: string;
100
- url: string | RegExp;
101
- response: unknown;
102
- status?: number;
103
- responseHeaders?: Record<string, string>;
104
- urlRegex?: boolean;
105
- delay?: number;
106
- }
107
-
108
- declare type Rule = {
109
- method: string;
110
- url: string | RegExp;
111
- response: unknown;
112
- alias: string;
113
- executed?: boolean;
114
- request?: any;
115
- status?: number;
116
- responseHeaders?: Record<string, string>;
117
- urlRegex?: boolean;
118
- delay?: number;
119
- hitCount?: number;
120
- };
121
-
122
- declare type ScreenDom = typeof screen_2;
123
-
124
- /**
125
- * screenDom - Scoped queries that exclude the TWD sidebar
126
- *
127
- * Searches only within the main app container (typically #root).
128
- * Use this for most queries to avoid matching elements in the sidebar.
129
- *
130
- * Note: This will NOT find portal-rendered elements (modals, dialogs) that are
131
- * rendered outside the root container. For portals, use `screenDomGlobal` instead.
132
- */
133
- export declare const screenDom: ScreenDom;
134
-
135
- /**
136
- * screenDomGlobal - Global queries that search the entire document.body
137
- *
138
- * Searches all elements in document.body, including portal-rendered elements
139
- * (modals, dialogs, tooltips, etc.).
140
- *
141
- * ⚠️ WARNING: This may also match elements inside the TWD sidebar if your selectors
142
- * are not specific enough. Use more specific queries (e.g., getByRole with name)
143
- * to avoid matching sidebar elements.
144
- *
145
- * Use this when:
146
- * - Querying portal-rendered elements (modals, dialogs)
147
- * - You need to search outside the root container
148
- *
149
- * Example:
150
- * ```ts
151
- * // For a modal rendered via portal
152
- * const modal = screenDomGlobal.getByRole('dialog', { name: 'Confirm' });
153
- * ```
154
- */
155
- export declare const screenDomGlobal: ScreenDom;
156
-
157
- /**
158
- * Overloads for the `should` function, for best IntelliSense.
159
- *
160
- * @example
161
- * twd.should("have.text", "Hello");
162
- * twd.should("be.empty");
163
- * twd.should("have.class", "active");
164
- */
165
- declare type ShouldFn = {
166
- (name: "have.text", expected: string): TWDElemAPI;
167
- (name: "not.have.text", expected: string): TWDElemAPI;
168
- (name: "contain.text", expected: string): TWDElemAPI;
169
- (name: "not.contain.text", expected: string): TWDElemAPI;
170
- (name: "be.empty"): TWDElemAPI;
171
- (name: "not.be.empty"): TWDElemAPI;
172
- (name: "have.attr", attr: string, value: string): TWDElemAPI;
173
- (name: "not.have.attr", attr: string, value: string): TWDElemAPI;
174
- (name: "have.value", value: string): TWDElemAPI;
175
- (name: "not.have.value", value: string): TWDElemAPI;
176
- (name: "be.disabled"): TWDElemAPI;
177
- (name: "not.be.disabled"): TWDElemAPI;
178
- (name: "be.enabled"): TWDElemAPI;
179
- (name: "not.be.enabled"): TWDElemAPI;
180
- (name: "be.checked"): TWDElemAPI;
181
- (name: "not.be.checked"): TWDElemAPI;
182
- (name: "be.selected"): TWDElemAPI;
183
- (name: "not.be.selected"): TWDElemAPI;
184
- (name: "be.focused"): TWDElemAPI;
185
- (name: "not.be.focused"): TWDElemAPI;
186
- (name: "be.visible"): TWDElemAPI;
187
- (name: "not.be.visible"): TWDElemAPI;
188
- (name: "have.class", className: string): TWDElemAPI;
189
- (name: "not.have.class", className: string): TWDElemAPI;
190
- };
191
-
192
- /**
193
- * A record of test module paths to their loader functions.
194
- * Each function returns a promise that resolves when the module is loaded.
195
- * This is typically used with Vite's `import.meta.glob` to dynamically import test modules.
196
- * @example
197
- * ```ts
198
- * const testModules = {
199
- * './test1.twd.test.ts': () => import('./test1.twd.test.ts'),
200
- * './test2.twd.test.ts': () => import('./test2.twd.test.ts'),
201
- * };
202
- * ```
203
- */
204
- declare type TestModule = Record<string, () => Promise<unknown>>;
205
-
206
- /**
207
- * Mini Cypress-style helpers for DOM testing.
208
- * @namespace twd
209
- */
210
- export declare const twd: TWDAPI;
211
-
212
- declare interface TWDAPI {
213
- /**
214
- * Finds an element by selector and returns the TWD API for it.
215
- * @param selector CSS selector
216
- * @returns {Promise<TWDElemAPI>} The TWD API for the element
217
- *
218
- * @example
219
- * ```ts
220
- * const btn = await twd.get("button");
221
- *
222
- * ```
223
- *
224
- */
225
- get: (selector: string) => Promise<TWDElemAPI>;
226
- /**
227
- * Sets the value of an input element and dispatches an input event. We recommend using this only for range, color, time inputs.
228
- * @param el The input element
229
- * @param value The value to set
230
- *
231
- * @example
232
- * ```ts
233
- * const input = await twd.get("input[type='time']");
234
- * twd.setInputValue(input.el, "13:30");
235
- *
236
- * ```
237
- */
238
- setInputValue: (el: Element, value: string) => void;
239
- /**
240
- * Finds multiple elements by selector and returns an array of TWD APIs for them.
241
- * @param selector CSS selector
242
- * @returns {Promise<TWDElemAPI[]>} Array of TWD APIs for the elements
243
- *
244
- * @example
245
- * ```ts
246
- * const items = await twd.getAll(".item");
247
- * items.at(0).should("be.visible");
248
- * items.at(1).should("contain.text", "Hello");
249
- * expect(items).to.have.length(3);
250
- * ```
251
- */
252
- getAll: (selector: string) => Promise<TWDElemAPI[]>;
253
- /**
254
- * Simulates visiting a URL (SPA navigation).
255
- * @param url The URL to visit
256
- * @param [reload] Whether to force a reload even if already on the URL (optional)
257
- *
258
- * @example
259
- * ```ts
260
- * twd.visit("/contact");
261
- * // visit with reload
262
- * twd.visit("/contact", true);
263
- * ```
264
- */
265
- visit: (url: string, reload?: boolean) => Promise<void>;
266
- /**
267
- * Mock a network request.
268
- *
269
- * @param alias Identifier for the mock rule. Useful for `waitFor()`.
270
- * @param options Options to configure the mock:
271
- * - `method`: HTTP method ("GET", "POST", …)
272
- * - `url`: URL string or RegExp to match
273
- * - `response`: Body of the mocked response
274
- * - `status`: (optional) HTTP status code (default: 200)
275
- * - `responseHeaders`: (optional) Response headers
276
- * - `delay`: (optional) Delay in ms before returning the response
277
- *
278
- * @example
279
- * ```ts
280
- * mockRequest("getUser", {
281
- * method: "GET",
282
- * url: /\/api\/user\/\d+/,
283
- * response: { id: 1, name: "Kevin" },
284
- * status: 200,
285
- * responseHeaders: { "x-mock": "true" }
286
- * });
287
- * ```
288
- */
289
- mockRequest: (alias: string, options: Options) => Promise<void>;
290
- /**
291
- * Wait for a mocked request to be made.
292
- * @param alias The alias of the mock rule to wait for
293
- * @param retries The number of retries to make
294
- * @param retryDelay The delay between retries
295
- * @return The matched rule (with body if applicable)
296
- *
297
- * @example
298
- * ```ts
299
- * const rule = await twd.waitFor("aliasId");
300
- * console.log(rule.body);
301
- * const rule = await twd.waitFor("aliasId", 5, 100);
302
- * console.log(rule.body);
303
- *
304
- * ```
305
- */
306
- waitForRequest: (alias: string, retries?: number, retryDelay?: number) => Promise<Rule>;
307
- /**
308
- * wait for a list of mocked requests to be made.
309
- * @param aliases The aliases of the mock rules to wait for
310
- * @returns The matched rules (with body if applicable)
311
- * @example
312
- * ```ts
313
- * const rules = await waitForRequests(["getUser", "postComment"]);
314
- * ```
315
- */
316
- waitForRequests: (aliases: string[]) => Promise<Rule[]>;
317
- /**
318
- * URL-related assertions.
319
- *
320
- * @example
321
- * ```ts
322
- * twd.url().should("eq", "http://localhost:3000/contact");
323
- * twd.url().should("contain.url", "/contact");
324
- *
325
- * ```
326
- */
327
- url: () => URLCommandAPI;
328
- /**
329
- * Initializes request mocking (registers the service worker).
330
- * @param [path] service worker absolute path (optional)
331
- *
332
- * @example
333
- * ```ts
334
- * await twd.initRequestMocking();
335
- * // init with custom service worker path
336
- * await twd.initRequestMocking('/test-path/mock-sw.js');
337
- * ```
338
- */
339
- initRequestMocking: (path?: string) => Promise<void>;
340
- /**
341
- * Clears all request mock rules.
342
- *
343
- * @example
344
- * ```ts
345
- * twd.clearRequestMockRules();
346
- *
347
- * ```
348
- */
349
- clearRequestMockRules: () => void;
350
- /**
351
- * Gets all current request mock rules.
352
- *
353
- * @example
354
- * ```ts
355
- * const rules = twd.getRequestMockRules();
356
- * console.log(rules);
357
- * ```
358
- */
359
- getRequestMockRules: () => Rule[];
360
- /**
361
- * Gets the number of times a specific mock rule was hit.
362
- * @param alias The alias of the mock rule
363
- * @returns The number of times the rule was matched
364
- *
365
- * @example
366
- * ```ts
367
- * const count = twd.getRequestCount("getUser");
368
- * expect(count).to.equal(2);
369
- * ```
370
- */
371
- getRequestCount: (alias: string) => number;
372
- /**
373
- * Gets a snapshot of all mock rule hit counts.
374
- * @returns An object mapping rule aliases to their hit counts
375
- *
376
- * @example
377
- * ```ts
378
- * const counts = twd.getRequestCounts();
379
- * expect(counts).to.deep.equal({ getUser: 2, listPosts: 1 });
380
- * ```
381
- */
382
- getRequestCounts: () => Record<string, number>;
383
- /**
384
- * Waits for a specified time.
385
- * @param time Time in milliseconds to wait
386
- * @returns A promise that resolves after the specified time
387
- * @example
388
- * ```ts
389
- * await twd.wait(500); // wait for 500ms
390
- * ```
391
- */
392
- wait: (time: number) => Promise<void>;
393
- /**
394
- * Asserts something about the element.
395
- * @param el The element to assert on
396
- * @param name The name of the assertion.
397
- * @param args Arguments for the assertion.
398
- * @returns The same API for chaining.
399
- * @example
400
- * ```ts
401
- * const button = await twd.get("button");
402
- * const text = screenDom.getByText("Hello");
403
- * twd.should(button.el, "have.text", "Hello");
404
- * twd.should(text, "be.empty");
405
- * twd.should(button.el, "have.class", "active");
406
- * ```
407
- */
408
- should: (el: Element, name: AnyAssertion, ...args: ArgsFor<AnyAssertion>) => void;
409
- /**
410
- * Mock a component.
411
- * @param name The name of the component to mock
412
- * @param component The component to mock
413
- * @returns The mocked component
414
- * @example
415
- * ```ts
416
- * twd.mockComponent("Button", Button);
417
- * ```
418
- */
419
- mockComponent: (name: string, component: React.ComponentType<any>) => void;
420
- /**
421
- * Clears all component mocks.
422
- *
423
- * @example
424
- * ```ts
425
- * twd.clearComponentMocks();
426
- * ```
427
- */
428
- clearComponentMocks: () => void;
429
- /**
430
- * Asserts that an element does not exist in the DOM.
431
- * @param selector CSS selector of the element to check
432
- * @returns A promise that resolves if the element does not exist, or rejects if it does
433
- *
434
- * @example
435
- * ```ts
436
- * await twd.notExists(".non-existent");
437
- * ```
438
- */
439
- notExists: (selector: string) => Promise<void>;
440
- /**
441
- * Simulates a viewport size by constraining body dimensions, overriding
442
- * `window.innerWidth`/`window.innerHeight` and `window.matchMedia()`, and
443
- * rewriting CSS `@media` rules to match the simulated dimensions.
444
- * Call with no arguments to reset to the original viewport.
445
- *
446
- * @param width Viewport width in pixels
447
- * @param height Viewport height in pixels (optional — omit to leave height unconstrained)
448
- *
449
- * @example
450
- * ```ts
451
- * twd.viewport(375, 667); // mobile
452
- * twd.viewport(768); // tablet width, height unconstrained
453
- * twd.viewport(); // reset
454
- * ```
455
- */
456
- viewport: (width?: number, height?: number) => void;
457
- /**
458
- * Resets the viewport to its original size (undoes a previous `twd.viewport()` call).
459
- *
460
- * @example
461
- * ```ts
462
- * twd.resetViewport();
463
- * ```
464
- */
465
- resetViewport: () => void;
466
- }
467
-
468
- /**
469
- * The main API returned by `twd.get()`.
470
- *
471
- * @example
472
- * ```ts
473
- * const btn = await twd.get("button");
474
- * btn.should("have.text", "Clicked").click();
475
- *
476
- * ```
477
- *
478
- */
479
- declare interface TWDElemAPI {
480
- /** The underlying DOM element. */
481
- el: Element;
482
- /**
483
- * Asserts something about the element.
484
- * @param name The name of the assertion.
485
- * @param args Arguments for the assertion.
486
- * @returns The same API for chaining.
487
- *
488
- * @example
489
- * ```ts
490
- * const btn = await twd.get("button");
491
- * btn.should("have.text", "Click me").should("not.be.disabled");
492
- *
493
- * ```
494
- *
495
- */
496
- should: ShouldFn;
497
- }
498
-
499
- export declare const TWDSidebar: ({ open, position, search }: TWDSidebarProps) => JSX.Element;
500
-
501
- declare interface TWDSidebarProps {
502
- /**
503
- * Whether the sidebar is open by default
504
- */
505
- open: boolean;
506
- /**
507
- * Sidebar position
508
- * - left: Sidebar on the left side (default)
509
- * - right: Sidebar on the right side
510
- *
511
- * @default "left"
512
- */
513
- position?: "left" | "right";
514
- /**
515
- * Whether to show the search/filter input
516
- */
517
- search?: boolean;
518
- }
519
-
520
- /**
521
- * TWD Theme Configuration
522
- *
523
- * This file defines CSS variables that can be customized by users
524
- * to personalize their TWD UI experience.
525
- *
526
- * Users can override these variables by setting them in their CSS:
527
- *
528
- * ```css
529
- * :root {
530
- * --twd-primary: #2563eb;
531
- * --twd-background: #1e293b;
532
- * ... other variables
533
- * }
534
- *
535
- */
536
- export declare interface TWDTheme {
537
- primary: string;
538
- background: string;
539
- backgroundSecondary: string;
540
- border: string;
541
- borderLight: string;
542
- text: string;
543
- textSecondary: string;
544
- textMuted: string;
545
- describeBg: string;
546
- describeText: string;
547
- describeBorder: string;
548
- success: string;
549
- successBg: string;
550
- error: string;
551
- errorBg: string;
552
- warning: string;
553
- warningBg: string;
554
- skip: string;
555
- skipBg: string;
556
- buttonPrimary: string;
557
- buttonPrimaryText: string;
558
- buttonSecondary: string;
559
- buttonSecondaryText: string;
560
- buttonBorder: string;
561
- spacingXs: string;
562
- spacingSm: string;
563
- spacingMd: string;
564
- spacingLg: string;
565
- spacingXl: string;
566
- fontSizeXs: string;
567
- fontSizeSm: string;
568
- fontSizeMd: string;
569
- fontSizeLg: string;
570
- fontWeightNormal: string;
571
- fontWeightMedium: string;
572
- fontWeightBold: string;
573
- sidebarWidth: string;
574
- borderRadius: string;
575
- borderRadiusLg: string;
576
- shadow: string;
577
- shadowSm: string;
578
- zIndexSidebar: string;
579
- zIndexSticky: string;
580
- animationDuration: string;
581
- iconColor: string;
582
- iconColorSecondary: string;
583
- }
584
-
585
- /**
586
- * All supported assertion names for the `should` function in url command
587
- *
588
- * @example
589
- * twd.url().should("contain.url", "/new-page");
590
- * twd.url().should("eq", "http://localhost:3000/new-page");
591
- */
592
- declare type URLAssertionName = "eq" | "contain.url";
593
-
594
- declare type URLCommandAPI = {
595
- location: Location;
596
- should: (name: AnyURLAssertion, value: string, retries?: number) => Promise<string>;
597
- };
598
-
599
- declare type UserEvent = typeof default_2;
600
-
601
- export declare const userEvent: UserEvent;
602
-
603
1
  export { }