tslaws 1.1.0 → 1.2.0

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.
@@ -12,6 +12,311 @@ export declare function fire(target: EventTarget, type: string, init?: EventInit
12
12
  export declare function click(target: Element): void;
13
13
  /** Sets a field's value the way typing does, so frameworks that track `input` events notice. */
14
14
  export declare function type(field: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, text: string): void;
15
+ type WirePattern = string | {
16
+ __re: true;
17
+ source: string;
18
+ flags: string;
19
+ };
20
+ type LocatorStep = {
21
+ k: 'css';
22
+ selector: string;
23
+ } | {
24
+ k: 'role';
25
+ role: string;
26
+ options?: Record<string, unknown>;
27
+ } | {
28
+ k: 'text';
29
+ text: WirePattern;
30
+ exact?: boolean;
31
+ } | {
32
+ k: 'label';
33
+ text: WirePattern;
34
+ exact?: boolean;
35
+ } | {
36
+ k: 'placeholder';
37
+ text: WirePattern;
38
+ exact?: boolean;
39
+ } | {
40
+ k: 'testId';
41
+ testId: string;
42
+ } | {
43
+ k: 'altText';
44
+ text: WirePattern;
45
+ exact?: boolean;
46
+ } | {
47
+ k: 'title';
48
+ text: WirePattern;
49
+ exact?: boolean;
50
+ } | {
51
+ k: 'first';
52
+ } | {
53
+ k: 'last';
54
+ } | {
55
+ k: 'nth';
56
+ index: number;
57
+ } | {
58
+ k: 'filter';
59
+ hasText?: WirePattern;
60
+ hasNotText?: WirePattern;
61
+ has?: LocatorStep[];
62
+ hasNot?: LocatorStep[];
63
+ };
64
+ export interface LocatorRoleOptions {
65
+ name?: string | RegExp;
66
+ exact?: boolean;
67
+ checked?: boolean;
68
+ disabled?: boolean;
69
+ expanded?: boolean;
70
+ includeHidden?: boolean;
71
+ level?: number;
72
+ pressed?: boolean;
73
+ selected?: boolean;
74
+ }
75
+ export interface LocatorTextOptions {
76
+ exact?: boolean;
77
+ }
78
+ export interface LocatorFilterOptions {
79
+ hasText?: string | RegExp;
80
+ hasNotText?: string | RegExp;
81
+ has?: Locator;
82
+ hasNot?: Locator;
83
+ }
84
+ export interface ClickOptions {
85
+ button?: 'left' | 'right' | 'middle';
86
+ force?: boolean;
87
+ }
88
+ export interface WaitForLocatorOptions {
89
+ state?: 'visible' | 'hidden' | 'attached' | 'detached';
90
+ timeout?: number;
91
+ }
92
+ export interface ScreenshotOptions {
93
+ path?: string;
94
+ fullPage?: boolean;
95
+ }
96
+ export type InteractionTarget = Locator | Element | string;
97
+ /**
98
+ * Chainable, auto-waiting Playwright-backed browser locator (`page.getByRole(...)`, `page.getByText(...)`, etc.),
99
+ * providing Vitest Browser Mode ergonomics natively inside `tslaws` with zero external runner or Vite dependency.
100
+ */
101
+ export declare class Locator {
102
+ readonly steps: LocatorStep[];
103
+ constructor(steps: LocatorStep[]);
104
+ private next;
105
+ getByRole(role: string, options?: LocatorRoleOptions): Locator;
106
+ getByText(text: string | RegExp, options?: LocatorTextOptions): Locator;
107
+ getByLabelText(text: string | RegExp, options?: LocatorTextOptions): Locator;
108
+ getByPlaceholder(text: string | RegExp, options?: LocatorTextOptions): Locator;
109
+ getByTestId(testId: string): Locator;
110
+ getByAltText(text: string | RegExp, options?: LocatorTextOptions): Locator;
111
+ getByTitle(text: string | RegExp, options?: LocatorTextOptions): Locator;
112
+ locator(selector: string): Locator;
113
+ first(): Locator;
114
+ last(): Locator;
115
+ nth(index: number): Locator;
116
+ filter(options: LocatorFilterOptions): Locator;
117
+ click(options?: ClickOptions): Promise<void>;
118
+ dblClick(options?: ClickOptions): Promise<void>;
119
+ tripleClick(options?: ClickOptions): Promise<void>;
120
+ hover(options?: {
121
+ force?: boolean;
122
+ }): Promise<void>;
123
+ unhover(): Promise<void>;
124
+ focus(): Promise<void>;
125
+ fill(value: string): Promise<void>;
126
+ clear(): Promise<void>;
127
+ type(value: string): Promise<void>;
128
+ press(key: string): Promise<void>;
129
+ selectOptions(values: string | string[]): Promise<void>;
130
+ check(options?: {
131
+ force?: boolean;
132
+ }): Promise<void>;
133
+ uncheck(options?: {
134
+ force?: boolean;
135
+ }): Promise<void>;
136
+ dropTo(target: InteractionTarget): Promise<void>;
137
+ screenshot(options?: {
138
+ path?: string;
139
+ }): Promise<string>;
140
+ count(): Promise<number>;
141
+ isVisible(): Promise<boolean>;
142
+ isEnabled(): Promise<boolean>;
143
+ isChecked(): Promise<boolean>;
144
+ textContent(): Promise<string | null>;
145
+ inputValue(): Promise<string>;
146
+ getAttribute(name: string): Promise<string | null>;
147
+ waitFor(options?: WaitForLocatorOptions): Promise<void>;
148
+ /** Returns all currently matching live DOM elements in the page. */
149
+ elements(): Promise<Element[]>;
150
+ /** Returns the single matching DOM element, or `null` if none matched (throws if multiple match). */
151
+ query(): Promise<Element | null>;
152
+ /** Returns the single matching DOM element, throwing if zero or multiple elements match. */
153
+ element(): Promise<Element>;
154
+ }
155
+ /**
156
+ * Native Vitest-Browser-style `page` API backed directly by Playwright: auto-retrying accessible locators
157
+ * (`getByRole`, `getByText`, `getByLabelText`, `getByPlaceholder`, `getByTestId`, `getByAltText`, `getByTitle`,
158
+ * `locator`), viewport resizing, and page screenshots.
159
+ */
160
+ export declare const page: {
161
+ getByRole(role: string, options?: LocatorRoleOptions): Locator;
162
+ getByText(text: string | RegExp, options?: LocatorTextOptions): Locator;
163
+ getByLabelText(text: string | RegExp, options?: LocatorTextOptions): Locator;
164
+ getByPlaceholder(text: string | RegExp, options?: LocatorTextOptions): Locator;
165
+ getByTestId(testId: string): Locator;
166
+ getByAltText(text: string | RegExp, options?: LocatorTextOptions): Locator;
167
+ getByTitle(text: string | RegExp, options?: LocatorTextOptions): Locator;
168
+ locator(selector: string): Locator;
169
+ /** Wraps a live DOM element in a Playwright-backed `Locator`. */
170
+ elementLocator(el: Element): Locator;
171
+ /** Resizes the browser viewport (for responsive layout and media-query laws). */
172
+ viewport(width: number, height: number): Promise<void>;
173
+ /** Takes a real Playwright PNG screenshot of the page (returns base64, and writes to `path` if provided). */
174
+ screenshot(options?: ScreenshotOptions): Promise<string>;
175
+ };
176
+ /**
177
+ * Trusted browser interactions driven by Playwright (`isTrusted: true`, real hit-testing, `:hover`, `:focus-visible`,
178
+ * native form controls, keyboard navigation, and drag-and-drop), matching Vitest Browser Mode's `userEvent`.
179
+ */
180
+ export declare const userEvent: {
181
+ /** Performs a real pointer/mouse click with hit-testing and focus. */
182
+ click(target: InteractionTarget, options?: ClickOptions): Promise<void>;
183
+ /** Performs a real double click. */
184
+ dblClick(target: InteractionTarget, options?: ClickOptions): Promise<void>;
185
+ /** Performs a real triple click. */
186
+ tripleClick(target: InteractionTarget, options?: ClickOptions): Promise<void>;
187
+ /** Moves the real pointer over `target`, triggering `:hover` and pointer events. */
188
+ hover(target: InteractionTarget, options?: {
189
+ force?: boolean;
190
+ }): Promise<void>;
191
+ /** Moves the pointer away from the hovered element. */
192
+ unhover(): Promise<void>;
193
+ /** Focuses `target` through the browser engine. */
194
+ focus(target: InteractionTarget): Promise<void>;
195
+ /** Clears and fills an input/textarea with real browser input events. */
196
+ fill(target: InteractionTarget, value: string): Promise<void>;
197
+ /** Clears an input/textarea value. */
198
+ clear(target: InteractionTarget): Promise<void>;
199
+ /** Types `value` keystroke by keystroke into `target`. */
200
+ type(target: InteractionTarget, value: string): Promise<void>;
201
+ /** Presses a keyboard key (for example `'Enter'`, `'Escape'`, `'Tab'`, `'ArrowDown'`). */
202
+ press(key: string): Promise<void>;
203
+ /** Types text and special key sequences (e.g. `'hello{Enter}'`, `'{Shift>}a{/Shift}'`) through the browser keyboard. */
204
+ keyboard(keys: string): Promise<void>;
205
+ /** Moves focus via the Tab key (`{ shift: true }` for Shift+Tab). */
206
+ tab(options?: {
207
+ shift?: boolean;
208
+ }): Promise<void>;
209
+ /** Selects one or more `<option>` values on a `<select>` element. */
210
+ selectOptions(target: InteractionTarget, values: string | string[]): Promise<void>;
211
+ /** Checks a checkbox or radio control. */
212
+ check(target: InteractionTarget, options?: {
213
+ force?: boolean;
214
+ }): Promise<void>;
215
+ /** Unchecks a checkbox control. */
216
+ uncheck(target: InteractionTarget, options?: {
217
+ force?: boolean;
218
+ }): Promise<void>;
219
+ /** Drags `source` onto `target` with real browser pointer/drag events. */
220
+ dragAndDrop(source: InteractionTarget, target: InteractionTarget): Promise<void>;
221
+ };
222
+ /** Backwards-compatible alias for `userEvent`, also exposing `select` and `setViewport`. */
223
+ export declare const user: {
224
+ select: (target: InteractionTarget, values: string | string[]) => Promise<void>;
225
+ setViewport: (width: number, height: number) => Promise<void>;
226
+ /** Performs a real pointer/mouse click with hit-testing and focus. */
227
+ click(target: InteractionTarget, options?: ClickOptions): Promise<void>;
228
+ /** Performs a real double click. */
229
+ dblClick(target: InteractionTarget, options?: ClickOptions): Promise<void>;
230
+ /** Performs a real triple click. */
231
+ tripleClick(target: InteractionTarget, options?: ClickOptions): Promise<void>;
232
+ /** Moves the real pointer over `target`, triggering `:hover` and pointer events. */
233
+ hover(target: InteractionTarget, options?: {
234
+ force?: boolean;
235
+ }): Promise<void>;
236
+ /** Moves the pointer away from the hovered element. */
237
+ unhover(): Promise<void>;
238
+ /** Focuses `target` through the browser engine. */
239
+ focus(target: InteractionTarget): Promise<void>;
240
+ /** Clears and fills an input/textarea with real browser input events. */
241
+ fill(target: InteractionTarget, value: string): Promise<void>;
242
+ /** Clears an input/textarea value. */
243
+ clear(target: InteractionTarget): Promise<void>;
244
+ /** Types `value` keystroke by keystroke into `target`. */
245
+ type(target: InteractionTarget, value: string): Promise<void>;
246
+ /** Presses a keyboard key (for example `'Enter'`, `'Escape'`, `'Tab'`, `'ArrowDown'`). */
247
+ press(key: string): Promise<void>;
248
+ /** Types text and special key sequences (e.g. `'hello{Enter}'`, `'{Shift>}a{/Shift}'`) through the browser keyboard. */
249
+ keyboard(keys: string): Promise<void>;
250
+ /** Moves focus via the Tab key (`{ shift: true }` for Shift+Tab). */
251
+ tab(options?: {
252
+ shift?: boolean;
253
+ }): Promise<void>;
254
+ /** Selects one or more `<option>` values on a `<select>` element. */
255
+ selectOptions(target: InteractionTarget, values: string | string[]): Promise<void>;
256
+ /** Checks a checkbox or radio control. */
257
+ check(target: InteractionTarget, options?: {
258
+ force?: boolean;
259
+ }): Promise<void>;
260
+ /** Unchecks a checkbox control. */
261
+ uncheck(target: InteractionTarget, options?: {
262
+ force?: boolean;
263
+ }): Promise<void>;
264
+ /** Drags `source` onto `target` with real browser pointer/drag events. */
265
+ dragAndDrop(source: InteractionTarget, target: InteractionTarget): Promise<void>;
266
+ };
267
+ /** Polls `predicate` until it returns a truthy value (or non-throwing result), failing after `timeout` ms. */
268
+ export declare function waitFor<T>(predicate: () => T | Promise<T>, options?: {
269
+ timeout?: number;
270
+ interval?: number;
271
+ }): Promise<T>;
272
+ export interface MockResponse {
273
+ status?: number;
274
+ body?: string;
275
+ json?: unknown;
276
+ headers?: Record<string, string>;
277
+ contentType?: string;
278
+ }
279
+ /**
280
+ * Intercepts network requests matching `urlPattern` (e.g. `'**\/api\/user'`) in this browser law via Playwright routing,
281
+ * returning `response`. Automatically cleared after each law input.
282
+ */
283
+ export declare function mock(urlPattern: string, response: MockResponse): Promise<void>;
284
+ /** Clears all active `mock()` network routes for the current law run. */
285
+ export declare function clearMocks(): Promise<void>;
286
+ export interface ElementAssertionMatchers {
287
+ /** Auto-retries until the element is visible. */
288
+ toBeVisible(): Promise<true>;
289
+ /** Auto-retries until the element is hidden or absent. */
290
+ toBeHidden(): Promise<true>;
291
+ /** Auto-retries until at least one matching element exists in the document. */
292
+ toBeInTheDocument(): Promise<true>;
293
+ /** Auto-retries until the element is enabled. */
294
+ toBeEnabled(): Promise<true>;
295
+ /** Auto-retries until the element is disabled. */
296
+ toBeDisabled(): Promise<true>;
297
+ /** Auto-retries until the checkbox/radio is checked. */
298
+ toBeChecked(): Promise<true>;
299
+ /** Auto-retries until `textContent` matches `expected` (substring or RegExp). */
300
+ toHaveTextContent(expected: string | RegExp): Promise<true>;
301
+ /** Auto-retries until the form control's value matches `expected`. */
302
+ toHaveValue(expected: string | RegExp): Promise<true>;
303
+ /** Auto-retries until attribute `name` exists (and matches `expected` when given). */
304
+ toHaveAttribute(name: string, expected?: string | RegExp): Promise<true>;
305
+ /** Auto-retries until exactly `count` elements match. */
306
+ toHaveCount(count: number): Promise<true>;
307
+ }
308
+ export interface ElementAssertions extends ElementAssertionMatchers {
309
+ /** Negated auto-retrying assertions (like Vitest Browser Mode's `expect.element(loc).not`). */
310
+ readonly not: ElementAssertionMatchers;
311
+ }
312
+ /**
313
+ * Auto-retrying Playwright-backed DOM assertions matching Vitest Browser Mode's `expect.element(locator)`:
314
+ * polls until the condition holds (up to `timeout`, default 5000 ms), returning `true` or throwing a descriptive error.
315
+ */
316
+ export declare function expectElement(target: InteractionTarget, options?: {
317
+ timeout?: number;
318
+ interval?: number;
319
+ }): ElementAssertions;
15
320
  export interface A11yViolation {
16
321
  id: string;
17
322
  impact: string | null;