@gtkx/testing 2.0.0-beta.10 → 2.0.0-beta.12

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.
Files changed (170) hide show
  1. package/README.md +8 -168
  2. package/dist/accessible-native.d.ts +1 -1
  3. package/dist/accessible-native.d.ts.map +1 -1
  4. package/dist/accessible-native.js +2 -2
  5. package/dist/accessible-native.js.map +1 -1
  6. package/dist/act.d.ts.map +1 -1
  7. package/dist/build-queries.d.ts +7 -8
  8. package/dist/build-queries.d.ts.map +1 -1
  9. package/dist/build-queries.js +25 -24
  10. package/dist/build-queries.js.map +1 -1
  11. package/dist/cleanup-registry.d.ts +2 -1
  12. package/dist/cleanup-registry.d.ts.map +1 -1
  13. package/dist/cleanup-registry.js +11 -4
  14. package/dist/cleanup-registry.js.map +1 -1
  15. package/dist/config.d.ts +3 -4
  16. package/dist/config.d.ts.map +1 -1
  17. package/dist/config.js.map +1 -1
  18. package/dist/editable.d.ts.map +1 -1
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js.map +1 -1
  21. package/dist/fire-event.d.ts.map +1 -1
  22. package/dist/fire-event.js +5 -2
  23. package/dist/fire-event.js.map +1 -1
  24. package/dist/frame-sync.d.ts.map +1 -1
  25. package/dist/frame-sync.js +1 -10
  26. package/dist/frame-sync.js.map +1 -1
  27. package/dist/harness-window.d.ts.map +1 -1
  28. package/dist/index.d.ts +2 -2
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +1 -1
  31. package/dist/index.js.map +1 -1
  32. package/dist/matchers.d.ts +4 -6
  33. package/dist/matchers.d.ts.map +1 -1
  34. package/dist/matchers.js +2 -2
  35. package/dist/matchers.js.map +1 -1
  36. package/dist/normalize.d.ts +1 -1
  37. package/dist/normalize.d.ts.map +1 -1
  38. package/dist/normalize.js +1 -1
  39. package/dist/normalize.js.map +1 -1
  40. package/dist/pretty-widget.d.ts.map +1 -1
  41. package/dist/pretty-widget.js +13 -2
  42. package/dist/pretty-widget.js.map +1 -1
  43. package/dist/production.d.ts.map +1 -1
  44. package/dist/queries.d.ts.map +1 -1
  45. package/dist/queries.js +6 -7
  46. package/dist/queries.js.map +1 -1
  47. package/dist/render-hook.js +3 -3
  48. package/dist/render-hook.js.map +1 -1
  49. package/dist/render.d.ts +17 -17
  50. package/dist/render.d.ts.map +1 -1
  51. package/dist/render.js +62 -62
  52. package/dist/render.js.map +1 -1
  53. package/dist/role-helpers.d.ts.map +1 -1
  54. package/dist/role-naming.d.ts +2 -1
  55. package/dist/role-naming.d.ts.map +1 -1
  56. package/dist/role-naming.js +2 -1
  57. package/dist/role-naming.js.map +1 -1
  58. package/dist/screen.d.ts.map +1 -1
  59. package/dist/screenshot.d.ts +10 -11
  60. package/dist/screenshot.d.ts.map +1 -1
  61. package/dist/screenshot.js +10 -11
  62. package/dist/screenshot.js.map +1 -1
  63. package/dist/suggestions.d.ts +1 -1
  64. package/dist/suggestions.d.ts.map +1 -1
  65. package/dist/suggestions.js +3 -3
  66. package/dist/suggestions.js.map +1 -1
  67. package/dist/timers.d.ts.map +1 -1
  68. package/dist/traversal.d.ts +4 -3
  69. package/dist/traversal.d.ts.map +1 -1
  70. package/dist/traversal.js +11 -4
  71. package/dist/traversal.js.map +1 -1
  72. package/dist/types.d.ts +3 -5
  73. package/dist/types.d.ts.map +1 -1
  74. package/dist/types.js.map +1 -1
  75. package/dist/user-event/adjustment.d.ts.map +1 -1
  76. package/dist/user-event/adjustment.js.map +1 -1
  77. package/dist/user-event/click.d.ts.map +1 -1
  78. package/dist/user-event/click.js +0 -1
  79. package/dist/user-event/click.js.map +1 -1
  80. package/dist/user-event/column-header-click.d.ts.map +1 -1
  81. package/dist/user-event/column-header-click.js +1 -1
  82. package/dist/user-event/column-header-click.js.map +1 -1
  83. package/dist/user-event/controller.d.ts +1 -1
  84. package/dist/user-event/controller.d.ts.map +1 -1
  85. package/dist/user-event/controller.js +3 -3
  86. package/dist/user-event/controller.js.map +1 -1
  87. package/dist/user-event/dispatch.d.ts.map +1 -1
  88. package/dist/user-event/event-wrapper.d.ts.map +1 -1
  89. package/dist/user-event/event-wrapper.js.map +1 -1
  90. package/dist/user-event/gesture.d.ts +1 -1
  91. package/dist/user-event/gesture.d.ts.map +1 -1
  92. package/dist/user-event/gesture.js +104 -29
  93. package/dist/user-event/gesture.js.map +1 -1
  94. package/dist/user-event/index.d.ts.map +1 -1
  95. package/dist/user-event/index.js +1 -1
  96. package/dist/user-event/index.js.map +1 -1
  97. package/dist/user-event/indexed-children.d.ts.map +1 -1
  98. package/dist/user-event/keyboard.d.ts.map +1 -1
  99. package/dist/user-event/keyboard.js +43 -16
  100. package/dist/user-event/keyboard.js.map +1 -1
  101. package/dist/user-event/list-row-click.d.ts.map +1 -1
  102. package/dist/user-event/list-row-click.js +1 -1
  103. package/dist/user-event/list-row-click.js.map +1 -1
  104. package/dist/user-event/native-click.d.ts.map +1 -1
  105. package/dist/user-event/native-click.js +1 -3
  106. package/dist/user-event/native-click.js.map +1 -1
  107. package/dist/user-event/notebook-tab-click.d.ts.map +1 -1
  108. package/dist/user-event/notebook-tab-click.js +1 -1
  109. package/dist/user-event/notebook-tab-click.js.map +1 -1
  110. package/dist/user-event/pointer.d.ts.map +1 -1
  111. package/dist/user-event/selection.d.ts.map +1 -1
  112. package/dist/user-event/selection.js +1 -6
  113. package/dist/user-event/selection.js.map +1 -1
  114. package/dist/user-event/text.d.ts.map +1 -1
  115. package/dist/user-event/text.js +31 -27
  116. package/dist/user-event/text.js.map +1 -1
  117. package/dist/wait-for.d.ts.map +1 -1
  118. package/dist/wait-for.js +28 -21
  119. package/dist/wait-for.js.map +1 -1
  120. package/dist/widget-accessible-properties.d.ts +1 -1
  121. package/dist/widget-accessible-properties.d.ts.map +1 -1
  122. package/dist/widget-accessible-properties.js +64 -28
  123. package/dist/widget-accessible-properties.js.map +1 -1
  124. package/dist/widget-getters.d.ts.map +1 -1
  125. package/dist/widget-getters.js +0 -1
  126. package/dist/widget-getters.js.map +1 -1
  127. package/dist/widget-target.d.ts.map +1 -1
  128. package/dist/window-state.d.ts.map +1 -1
  129. package/dist/window-state.js +3 -1
  130. package/dist/window-state.js.map +1 -1
  131. package/dist/within.d.ts.map +1 -1
  132. package/dist/within.js.map +1 -1
  133. package/package.json +16 -23
  134. package/src/accessible-native.ts +5 -5
  135. package/src/build-queries.ts +71 -88
  136. package/src/cleanup-registry.ts +11 -5
  137. package/src/config.ts +3 -4
  138. package/src/errors.ts +6 -6
  139. package/src/fire-event.ts +6 -7
  140. package/src/frame-sync.ts +1 -13
  141. package/src/index.ts +2 -12
  142. package/src/matchers.ts +34 -42
  143. package/src/normalize.ts +1 -4
  144. package/src/pretty-widget.ts +17 -2
  145. package/src/queries.ts +11 -20
  146. package/src/render-hook.tsx +4 -4
  147. package/src/render.tsx +67 -80
  148. package/src/role-naming.ts +2 -1
  149. package/src/screenshot.ts +13 -19
  150. package/src/suggestions.ts +3 -3
  151. package/src/traversal.ts +16 -6
  152. package/src/types.ts +14 -20
  153. package/src/user-event/adjustment.ts +1 -3
  154. package/src/user-event/click.ts +2 -9
  155. package/src/user-event/column-header-click.ts +1 -1
  156. package/src/user-event/controller.ts +3 -3
  157. package/src/user-event/event-wrapper.ts +1 -1
  158. package/src/user-event/gesture.ts +146 -37
  159. package/src/user-event/index.ts +10 -22
  160. package/src/user-event/keyboard.ts +61 -19
  161. package/src/user-event/list-row-click.ts +1 -1
  162. package/src/user-event/native-click.ts +1 -3
  163. package/src/user-event/notebook-tab-click.ts +1 -1
  164. package/src/user-event/selection.ts +4 -20
  165. package/src/user-event/text.ts +37 -36
  166. package/src/wait-for.ts +38 -34
  167. package/src/widget-accessible-properties.ts +80 -36
  168. package/src/widget-getters.ts +1 -3
  169. package/src/window-state.ts +4 -3
  170. package/src/within.ts +2 -2
package/src/queries.ts CHANGED
@@ -1,12 +1,5 @@
1
1
  import * as Gtk from "@gtkx/gi/gtk";
2
- import type {
3
- ByRoleOptions,
4
- ByRoleValue,
5
- Matcher,
6
- MatcherOptions,
7
- NormalizerFn,
8
- QueryFamilies,
9
- } from "./types.js";
2
+ import type { ByRoleOptions, ByRoleValue, Matcher, MatcherOptions, NormalizerFn, QueryFamilies } from "./types.js";
10
3
  import { buildQueries, type BuiltQueries, type QueryAllBy } from "./build-queries.js";
11
4
  import { multipleFoundError, notFoundError } from "./errors.js";
12
5
  import { getDefaultNormalizer } from "./normalize.js";
@@ -177,8 +170,8 @@ const buildNormalizer = (options?: MatcherOptions): NormalizerFn => {
177
170
  if (trim !== undefined || collapseWhitespace !== undefined) {
178
171
  throw new Error(
179
172
  "trim and collapseWhitespace are not supported with a normalizer. " +
180
- "If you want to use the default trim and collapseWhitespace logic in your normalizer, " +
181
- "use \"getDefaultNormalizer({ trim, collapseWhitespace })\" and compose that into your normalizer",
173
+ "If you want to use the default trim and collapseWhitespace logic in your normalizer, " +
174
+ 'use "getDefaultNormalizer({ trim, collapseWhitespace })" and compose that into your normalizer',
182
175
  );
183
176
  }
184
177
 
@@ -287,18 +280,14 @@ const hasMatchingAccessibleStates = (widget: Gtk.Widget, options: ByRoleOptions)
287
280
  const isMatchingWidgetType = <T extends Gtk.Accessible>(
288
281
  widget: Gtk.Widget,
289
282
  options?: MatcherOptions<T>,
290
- ): widget is Gtk.Widget & T =>
291
- options?.as === undefined || widget instanceof options.as;
283
+ ): widget is Gtk.Widget & T => options?.as === undefined || widget instanceof options.as;
292
284
 
293
285
  const hasMatchingAccessibleOptions = (widget: Gtk.Widget, options?: ByRoleOptions): boolean => {
294
286
  if (!options) {
295
287
  return true;
296
288
  }
297
289
 
298
- return (
299
- hasMatchingAccessibleName(widget, options) &&
300
- hasMatchingAccessibleStates(widget, options)
301
- );
290
+ return hasMatchingAccessibleName(widget, options) && hasMatchingAccessibleStates(widget, options);
302
291
  };
303
292
 
304
293
  function nameQueryFamily<Args extends unknown[], Element extends Gtk.Accessible>(
@@ -410,12 +399,15 @@ function queryAllByLabelText<T extends Gtk.Accessible = Gtk.Widget>(
410
399
  options?: MatcherOptions<T>,
411
400
  ): T[] {
412
401
  const results: Set<Gtk.Widget> = new Set();
402
+ const widgets = new Set(traverse(container));
413
403
 
414
- for (const widget of traverse(container)) {
404
+ for (const widget of widgets) {
415
405
  collectLabelMatches(results, widget, text, options);
416
406
  }
417
407
 
418
- return [...results].filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
408
+ return [...results].filter(
409
+ (widget): widget is Gtk.Widget & T => widgets.has(widget) && isMatchingWidgetType(widget, options),
410
+ );
419
411
  }
420
412
 
421
413
  /**
@@ -496,8 +488,7 @@ function queryAllByDisplayValue<T extends Gtk.Accessible = Gtk.Widget>(
496
488
  const matches = findAll(
497
489
  container,
498
490
  (widget) =>
499
- isMatchingWidgetType(widget, options) &&
500
- isTextMatch(getWidgetDisplayValue(widget), value, widget, options),
491
+ isMatchingWidgetType(widget, options) && isTextMatch(getWidgetDisplayValue(widget), value, widget, options),
501
492
  );
502
493
 
503
494
  return matches.filter((widget): widget is Gtk.Widget & T => isMatchingWidgetType(widget, options));
@@ -32,7 +32,7 @@ async function renderHook<Result, Props>(
32
32
  callback: (props: Props) => Result,
33
33
  options?: RenderHookOptions<Props>,
34
34
  ): Promise<RenderHookResult<Result, Props>> {
35
- const initialProps = (options as { initialProps?: Props } | undefined)?.initialProps as Props;
35
+ const initialProps = options?.initialProps as Props;
36
36
  const resultRef: { current: Result | undefined } = { current: undefined };
37
37
  let currentProps: Props = initialProps;
38
38
 
@@ -50,9 +50,9 @@ async function renderHook<Result, Props>(
50
50
 
51
51
  return {
52
52
  result: resultRef as { current: Result },
53
- rerender: async (newProps?: Props) => {
54
- if (newProps !== undefined) {
55
- currentProps = newProps;
53
+ rerender: async (...args: [] | [Props]) => {
54
+ if (args.length === 1) {
55
+ [currentProps] = args;
56
56
  }
57
57
 
58
58
  await renderResult.rerender(<TestComponent props={currentProps} />);
package/src/render.tsx CHANGED
@@ -1,17 +1,12 @@
1
- import type { CaughtErrorInfo, RootElement } from "@gtkx/react";
1
+ import type { CaughtErrorInfo, Root, RootElement } from "@gtkx/react";
2
2
  import * as Gtk from "@gtkx/gi/gtk";
3
- import {
4
- createReconcilerRoot,
5
- isRootElement,
6
- type ReconcilerRoot,
7
- setReconcilerErrorHandler,
8
- settleAccessible,
9
- } from "@gtkx/react/internal";
3
+ import { createRoot } from "@gtkx/react";
4
+ import { isRootElement, settleAccessible } from "@gtkx/react/internal";
10
5
  import { type ErrorInfo, type ReactNode, StrictMode } from "react";
11
6
  import type { RenderResult } from "./bound-queries.js";
12
7
  import type { QueryMap, RenderOptions, ScreenshotOptions } from "./types.js";
13
8
  import { runInAct, runWithActEnvironment } from "./act.js";
14
- import { addToCleanupQueue, runCleanup } from "./cleanup-registry.js";
9
+ import { addToCleanupQueue, runCleanup, runCleanupCallbacks } from "./cleanup-registry.js";
15
10
  import { getConfig } from "./config.js";
16
11
  import { scheduleWhenWindowReady } from "./frame-sync.js";
17
12
  import { createHarnessWindow, presentHarnessWindow } from "./harness-window.js";
@@ -25,12 +20,10 @@ import { requireWidget } from "./widget-target.js";
25
20
  import { findPresentedWindowFailure, findRenderedWindowFailure, mappedToplevels } from "./window-state.js";
26
21
  import { within } from "./within.js";
27
22
 
28
- /** A mounted render tracked so cleanup can unmount it. */
29
23
  type ActiveRender = {
30
- /** Root that rerenders drive and that cleanup tears the tree down through. */
31
- root: ReconcilerRoot;
32
- /** Harness window created for the render, null when it renders into a container the caller supplied. */
24
+ root: Root;
33
25
  window: Gtk.Window | null;
26
+ errors: RenderErrorState;
34
27
  };
35
28
 
36
29
  type ResolvedContainer = {
@@ -38,15 +31,13 @@ type ResolvedContainer = {
38
31
  window: Gtk.Window | null;
39
32
  };
40
33
 
41
- type ReconcilerErrorState = {
34
+ type RenderErrorState = {
42
35
  lastError: Error | null;
43
- isHandlerInstalled: boolean;
44
36
  };
45
37
 
46
38
  type WindowFailureReporter = (window: Gtk.Window) => string | null;
47
39
  type SettleAction = "render" | "rerender";
48
40
 
49
- const reconcilerErrors: ReconcilerErrorState = { lastError: null, isHandlerInstalled: false };
50
41
  const activeRenders: Set<ActiveRender> = new Set();
51
42
 
52
43
  const settleWindow = async (
@@ -70,20 +61,23 @@ const settleWindow = async (
70
61
  if (failure !== null) {
71
62
  throw new Error(
72
63
  `${action} timed out after ${String(timeout)}ms waiting for the window it rendered into: ${failure}. ` +
73
- "Platform state such as focus is only readable once that window is allocated and active.",
64
+ "Platform state such as focus is only readable once that window is allocated and active.",
74
65
  );
75
66
  }
76
67
  };
77
68
 
78
- const update = async (element: ReactNode, root: ReconcilerRoot): Promise<void> => {
79
- await runInAct(() => {
80
- root.update(element);
81
- });
82
-
83
- if (reconcilerErrors.lastError) {
84
- const captured = reconcilerErrors.lastError;
85
- reconcilerErrors.lastError = null;
86
- throw captured;
69
+ const flushRender = async (active: ActiveRender, action: () => void): Promise<void> => {
70
+ try {
71
+ await runInAct(async () => {
72
+ action();
73
+ await Promise.resolve();
74
+ });
75
+
76
+ if (active.errors.lastError !== null) {
77
+ throw active.errors.lastError;
78
+ }
79
+ } finally {
80
+ active.errors.lastError = null;
87
81
  }
88
82
  };
89
83
 
@@ -92,29 +86,15 @@ const disposeActiveRender = async (active: ActiveRender): Promise<void> => {
92
86
  return;
93
87
  }
94
88
 
95
- await active.root.unmount(async (root) => {
96
- await update(null, root);
89
+ try {
90
+ await flushRender(active, active.root.unmount);
91
+ } finally {
97
92
  active.window?.destroy();
98
- });
99
- };
100
-
101
- const disposeAllActiveRenders = async (): Promise<void> => {
102
- for (const active of activeRenders) {
103
- await disposeActiveRender(active);
104
93
  }
105
94
  };
106
95
 
107
- const handleError = (error: unknown): void => {
108
- reconcilerErrors.lastError = error instanceof Error ? error : new Error(String(error));
109
- };
110
-
111
- const installErrorHandler = (): void => {
112
- if (reconcilerErrors.isHandlerInstalled) {
113
- return;
114
- }
115
-
116
- setReconcilerErrorHandler(handleError);
117
- reconcilerErrors.isHandlerInstalled = true;
96
+ const disposeAllActiveRenders = async (): Promise<void> => {
97
+ await runCleanupCallbacks(activeRenders.values().map((active) => () => disposeActiveRender(active)));
118
98
  };
119
99
 
120
100
  const resolveContainer = (container: RenderOptions["container"]): ResolvedContainer => {
@@ -191,16 +171,22 @@ const settleRender = async (
191
171
  await settleWindow(settleTarget(resolved, container), findFailure, action);
192
172
  };
193
173
 
194
- const renderErrorHandlers = <Q extends QueryMap>(options: RenderOptions<Q> | undefined) => ({
195
- onUncaughtError: handleError,
196
- onCaughtError: (error: unknown, errorInfo: CaughtErrorInfo): void => {
197
- handleError(error);
198
- options?.onCaughtError?.(error, errorInfo);
199
- },
200
- onRecoverableError: (error: unknown, errorInfo: ErrorInfo): void => {
201
- options?.onRecoverableError?.(error, errorInfo);
202
- },
203
- });
174
+ const renderErrorHandlers = <Q extends QueryMap>(errors: RenderErrorState, options: RenderOptions<Q> | undefined) => {
175
+ const handleError = (error: unknown): void => {
176
+ errors.lastError = error instanceof Error ? error : new Error(String(error));
177
+ };
178
+
179
+ return {
180
+ onUncaughtError: handleError,
181
+ onCaughtError: (error: unknown, errorInfo: CaughtErrorInfo): void => {
182
+ handleError(error);
183
+ options?.onCaughtError?.(error, errorInfo);
184
+ },
185
+ onRecoverableError: (error: unknown, errorInfo: ErrorInfo): void => {
186
+ options?.onRecoverableError?.(error, errorInfo);
187
+ },
188
+ };
189
+ };
204
190
 
205
191
  const applyEnableAnimations = (areAnimationsEnabled: boolean): void => {
206
192
  const settings = Gtk.Settings.getDefault();
@@ -211,41 +197,38 @@ const applyEnableAnimations = (areAnimationsEnabled: boolean): void => {
211
197
  };
212
198
 
213
199
  /**
214
- * Renders a React element into a GTK4 widget tree and returns queries
215
- * scoped to it along with controls for rerendering and unmounting. When no
216
- * container is supplied, a harness window is created and presented, and the editable it hands the
217
- * focus to keeps its text unselected, with its caret at the end. The returned promise settles once
218
- * the window the tree is shown in has been laid out and activation has arrived, so platform state
219
- * such as focus is already readable when it resolves. A presented harness window is waited on until
220
- * it is itself active; a window the caller or the tree owns is waited on until the application holds
221
- * activation, since only a present can claim it. Rendering into a container that is not shown waits
222
- * for nothing, and a window that never becomes readable within `windowActivationTimeout` throws an
223
- * error naming the condition that failed rather than resolving with unreadable platform state. A
224
- * tree mounted into a container that sits outside every toplevel, or inside a window that is not
225
- * visible, is out of reach of a pointer and a keyboard, so every `userEvent` helper aimed at it
226
- * rejects after `actionabilityTimeout` naming that condition, and `fireEvent` drives it instead.
200
+ * Renders React into native widgets and returns queries, debug helpers, and lifecycle controls.
227
201
  *
228
- * @param element The React element to render.
229
- * @param options Optional container, wrapper, custom queries, and other render settings.
230
- * @returns A render result with bound queries, debug helpers, and lifecycle controls.
231
- * @throws When the window the tree is shown in is not laid out and activated in time.
202
+ * @remarks
203
+ * Without a container, creates and presents a harness window. Its initially focused editable
204
+ * keeps its text unselected, with the caret at the end.
205
+ *
206
+ * Waits for layout and activation before resolving, so focus and other platform state can be
207
+ * read immediately. A new harness waits for its own activation; a caller-owned or rendered
208
+ * window waits for application activation, since only presentation can claim it. A container
209
+ * that is not shown requires no activation wait.
210
+ *
211
+ * Widgets outside all toplevels or inside a hidden window cannot receive pointer or keyboard
212
+ * input. `userEvent` rejects after `actionabilityTimeout`; use `fireEvent` to drive them directly.
213
+ *
214
+ * @param element React element to render.
215
+ * @param options Container, wrapper, custom queries, and render settings.
216
+ * @returns Bound queries, debug helpers, and rerender/unmount controls.
217
+ * @throws If a shown window is not laid out and activated within `windowActivationTimeout`.
232
218
  */
233
219
  const render = async <Q extends QueryMap = Record<never, never>>(
234
220
  element: ReactNode,
235
221
  options?: RenderOptions<Q>,
236
222
  ): Promise<RenderResult<Q>> => {
237
- installErrorHandler();
238
223
  applyEnableAnimations(options?.areAnimationsEnabled === true);
239
224
  const baseElement: Container = options?.baseElement ?? TOPLEVELS;
240
225
  const Wrapper = options?.wrapper;
241
226
  const resolved = resolveContainer(options?.container);
242
227
 
243
- const root = createReconcilerRoot({
244
- containerInfo: resolved.containerInfo,
245
- ...renderErrorHandlers(options),
246
- });
228
+ const errors: RenderErrorState = { lastError: null };
229
+ const root = createRoot(resolved.containerInfo, renderErrorHandlers(errors, options));
247
230
 
248
- const active: ActiveRender = { root, window: resolved.window };
231
+ const active: ActiveRender = { root, window: resolved.window, errors };
249
232
  activeRenders.add(active);
250
233
  addToCleanupQueue(disposeAllActiveRenders);
251
234
  addToCleanupQueue(clearScreen);
@@ -257,7 +240,9 @@ const render = async <Q extends QueryMap = Record<never, never>>(
257
240
  return options?.isReactStrictMode ? <StrictMode>{wrapped}</StrictMode> : wrapped;
258
241
  };
259
242
 
260
- await update(wrap(element), root);
243
+ await flushRender(active, () => {
244
+ root.render(wrap(element));
245
+ });
261
246
  presentHarnessWindow(resolved.window);
262
247
  await settleRender(resolved, options?.container, "render");
263
248
  const container = resolveResultContainer(resolved, options?.container, baseElement);
@@ -270,7 +255,9 @@ const render = async <Q extends QueryMap = Record<never, never>>(
270
255
  await disposeActiveRender(active);
271
256
  },
272
257
  rerender: async (newElement: ReactNode) => {
273
- await update(wrap(newElement), root);
258
+ await flushRender(active, () => {
259
+ root.render(wrap(newElement));
260
+ });
274
261
  await settleRender(resolved, options?.container, "rerender");
275
262
  },
276
263
  debug: (element: Container | Container[] = baseElement, debugOptions?: PrettyWidgetOptions) => {
@@ -95,5 +95,6 @@ const NAMING: Partial<Record<Gtk.AccessibleRole, number>> = {
95
95
  const namingFor = (role: Gtk.AccessibleRole): number => NAMING[role] ?? 0;
96
96
  const isNameProhibited = (role: Gtk.AccessibleRole): boolean => (namingFor(role) & PROHIBITED) !== 0;
97
97
  const isNameFromAuthor = (role: Gtk.AccessibleRole): boolean => (namingFor(role) & FROM_AUTHOR) !== 0;
98
+ const isNameFromContent = (role: Gtk.AccessibleRole): boolean => (namingFor(role) & FROM_CONTENT) !== 0;
98
99
 
99
- export { isNameFromAuthor, isNameProhibited };
100
+ export { isNameFromAuthor, isNameFromContent, isNameProhibited };
package/src/screenshot.ts CHANGED
@@ -24,13 +24,9 @@ type CaptureState = {
24
24
  failure: CaptureFailure | null;
25
25
  };
26
26
 
27
- type CaptureResult =
28
- { status: "captured"; result: ScreenshotResult } |
29
- { status: "failed"; failure: CaptureFailure };
27
+ type CaptureResult = { status: "captured"; result: ScreenshotResult } | { status: "failed"; failure: CaptureFailure };
30
28
 
31
- type CaptureOutcome =
32
- { status: "captured"; result: ScreenshotResult } |
33
- { status: "stalled"; failure: CaptureFailure };
29
+ type CaptureOutcome = { status: "captured"; result: ScreenshotResult } | { status: "stalled"; failure: CaptureFailure };
34
30
 
35
31
  type BackgroundSnapshot = {
36
32
  renderBackground(...args: [context: object, x: number, y: number, width: number, height: number]): void;
@@ -57,8 +53,7 @@ const NOT_PRESENTING_MESSAGE =
57
53
  const EMPTY_WIDGET_HINT =
58
54
  "The display is presenting frames to this window, so the widget itself is empty: it painted nothing.";
59
55
 
60
- const OTHER_FAILURE_HINT =
61
- "The display is presenting frames to this window, so the capture failed for another reason.";
56
+ const OTHER_FAILURE_HINT = "The display is presenting frames to this window, so the capture failed for another reason.";
62
57
 
63
58
  const NOTHING_ON_SCREEN_MESSAGE = "Nothing is on screen to capture: no toplevel window is mapped";
64
59
 
@@ -330,18 +325,17 @@ const captureUntilPaintable = (
330
325
  };
331
326
 
332
327
  /**
333
- * Captures a PNG snapshot of a widget, retrying until it has a paintable size,
334
- * and writes the image to `options.path` when one is given, creating any
335
- * missing parent directories. Popovers open inside the widget render on their
336
- * own surfaces, so they are composited into the image at their on-screen
337
- * positions. A display that never presents a frame leaves GTK without a cached
338
- * render node, so the widget's contents are then snapshotted directly; when
339
- * even that yields nothing, the failure names the display instead of blaming
340
- * the widget.
328
+ * Captures a widget as PNG, retrying until it has a paintable size. Writes to `options.path`
329
+ * when supplied, creating missing parent directories.
341
330
  *
342
- * @param widget The widget to render to an image.
343
- * @param options Optional scale, timeout, retry interval, and output path.
344
- * @returns The base64-encoded PNG data along with its mime type and dimensions.
331
+ * @remarks
332
+ * Open popovers are composited at their on-screen positions. If the display has not presented
333
+ * a frame, captures widget contents directly instead of using GTK's cached render node.
334
+ * If that also fails, the error identifies the display's presentation failure.
335
+ *
336
+ * @param target Widget to capture.
337
+ * @param options Scale, timeout, retry interval, and output path.
338
+ * @returns Base64-encoded PNG data, MIME type, and dimensions.
345
339
  */
346
340
  const screenshot = async (target: Gtk.Accessible, options?: ScreenshotOptions): Promise<ScreenshotResult> => {
347
341
  const widget = requireWidget(target);
@@ -56,7 +56,7 @@ const roleSuggestion = (widget: Gtk.Widget, variant: Variant): Suggestion | unde
56
56
  return makeSuggestion("Role", variant, roleText);
57
57
  }
58
58
 
59
- return makeSuggestion("Role", variant, `${roleText}, { name: '${name}' }`);
59
+ return makeSuggestion("Role", variant, `${roleText}, { name: ${JSON.stringify(name)} }`);
60
60
  };
61
61
 
62
62
  const textSuggestion = (queryName: Method, variant: Variant, value: string | null): Suggestion | undefined => {
@@ -64,13 +64,13 @@ const textSuggestion = (queryName: Method, variant: Variant, value: string | nul
64
64
  return undefined;
65
65
  }
66
66
 
67
- return makeSuggestion(queryName, variant, `'${value}'`);
67
+ return makeSuggestion(queryName, variant, JSON.stringify(value));
68
68
  };
69
69
 
70
70
  /**
71
71
  * Computes the recommended query for reaching the given widget, preferring role, then label text,
72
72
  * placeholder text, text, display value, and name.
73
- * @param widget Widget to build a suggestion for.
73
+ * @param target Widget to build a suggestion for.
74
74
  * @param variant Query variant the suggestion should use.
75
75
  * @param method Restrict the suggestion to a specific query family instead of choosing by priority.
76
76
  * @returns The best available suggestion, or undefined when no query applies.
package/src/traversal.ts CHANGED
@@ -13,6 +13,7 @@ type QueryContainer = Gtk.Accessible | Gtk.EventController | Gtk.LayoutManager |
13
13
  */
14
14
  type Container = QueryContainer | Gtk.Application | typeof TOPLEVELS;
15
15
  type WidgetClass<T extends object> = abstract new (...args: never[]) => T;
16
+ type ChildContainer = Pick<Gtk.Widget, "getFirstChild">;
16
17
 
17
18
  /** Container sentinel that widens a query to every toplevel window currently open. */
18
19
  const TOPLEVELS: unique symbol = Symbol("gtkx.toplevels");
@@ -52,7 +53,7 @@ const traverseWidgetTree = function* (
52
53
  }
53
54
  };
54
55
 
55
- const children = function* (widget: Gtk.Widget): Generator<Gtk.Widget> {
56
+ const children = function* (widget: ChildContainer): Generator<Gtk.Widget> {
56
57
  let child = widget.getFirstChild();
57
58
 
58
59
  while (child) {
@@ -98,11 +99,13 @@ const mappedWidgets = function* (widget: Gtk.Widget, isParentMapped: boolean): G
98
99
  }
99
100
  };
100
101
 
101
- const relationCandidates = (widget: Gtk.Widget): Iterable<Gtk.Accessible>[] => {
102
+ const relationCandidates = function* (widget: Gtk.Widget): Generator<Iterable<Gtk.Accessible>> {
103
+ yield widget.listMnemonicLabels();
104
+ yield descendants(widget);
102
105
  const root = widget.getRoot();
103
- const tree: Iterable<Gtk.Accessible> = root instanceof Gtk.Widget ? traverseWidgetTree(root, isAnyWidget) : [];
104
-
105
- return [widget.listMnemonicLabels(), descendants(widget), tree];
106
+ if (root instanceof Gtk.Widget) {
107
+ yield traverseWidgetTree(root, isAnyWidget);
108
+ }
106
109
  };
107
110
 
108
111
  const resolveRoot = (container: QueryContainer): Gtk.Widget | null => {
@@ -126,12 +129,18 @@ const resolveRoot = (container: QueryContainer): Gtk.Widget | null => {
126
129
  };
127
130
 
128
131
  const roots = function* (container: Container): Generator<Gtk.Widget> {
129
- if (container === TOPLEVELS || isApplication(container)) {
132
+ if (container === TOPLEVELS) {
130
133
  yield* Gtk.Window.listToplevels();
131
134
 
132
135
  return;
133
136
  }
134
137
 
138
+ if (isApplication(container)) {
139
+ yield* container.getWindows();
140
+
141
+ return;
142
+ }
143
+
135
144
  const root = resolveRoot(container);
136
145
 
137
146
  if (root) {
@@ -168,5 +177,6 @@ export {
168
177
  relationCandidates,
169
178
  roots,
170
179
  traverse,
180
+ type ChildContainer,
171
181
  type Container,
172
182
  };
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type * as Gtk from "@gtkx/gi/gtk";
2
2
  import type { CaughtErrorInfo, RootElement } from "@gtkx/react";
3
- import type { ComponentType, ErrorInfo, ReactNode } from "react";
3
+ import type { ComponentType, ErrorInfo, PropsWithChildren } from "react";
4
4
  import type { PrettyWidgetOptions } from "./pretty-widget.js";
5
5
  import type { Container } from "./traversal.js";
6
6
 
@@ -17,9 +17,7 @@ type NormalizerFn = (text: string) => string;
17
17
  * A widget class usable as a query's `as` constraint, such as `Gtk.Button`. Abstract classes and
18
18
  * generated GInterface pseudo-classes that extend `Gtk.Accessible` are accepted.
19
19
  */
20
- type WidgetType<T extends Gtk.Accessible = Gtk.Accessible> = abstract new (
21
- ...args: never[]
22
- ) => T;
20
+ type WidgetType<T extends Gtk.Accessible = Gtk.Accessible> = abstract new (...args: never[]) => T;
23
21
 
24
22
  /** Options controlling the default text normalizer. */
25
23
  type NormalizerOptions = {
@@ -100,9 +98,7 @@ type ByRoleOptions<T extends Gtk.Accessible = Gtk.Accessible> = MatcherOptions<T
100
98
  };
101
99
 
102
100
  /** A React component that wraps rendered content, receiving it as its children. */
103
- type WrapperComponent = ComponentType<{
104
- children: ReactNode;
105
- }>;
101
+ type WrapperComponent = ComponentType<Required<PropsWithChildren>>;
106
102
 
107
103
  /** A custom query, taking the scope to search as its first argument and its matcher arguments after it. */
108
104
  type Query = (container: Container, ...args: never[]) => unknown;
@@ -140,19 +136,17 @@ type QueryKind = "role" | "text" | "name" | "value";
140
136
  type QueryArgs<Kind extends QueryKind, T extends Gtk.Accessible> = Kind extends "role"
141
137
  ? [role: Gtk.AccessibleRole, options?: ByRoleOptions<T>]
142
138
  : Kind extends "name"
143
- ? [name: Matcher, options?: MatcherOptions<T>]
144
- : Kind extends "value"
145
- ? [value: Matcher, options?: MatcherOptions<T>]
146
- : [text: Matcher, options?: MatcherOptions<T>];
139
+ ? [name: Matcher, options?: MatcherOptions<T>]
140
+ : Kind extends "value"
141
+ ? [value: Matcher, options?: MatcherOptions<T>]
142
+ : [text: Matcher, options?: MatcherOptions<T>];
147
143
 
148
144
  /**
149
145
  * One family's query variants, each named for its variant followed by `Suffix` and taking `Head`
150
146
  * ahead of the family's own arguments.
151
147
  */
152
148
  type QueryFamily<Suffix extends string, Kind extends QueryKind, Head extends unknown[]> = {
153
- [K in keyof QueryFamilyReturns<Gtk.Widget> as `${K & string}${Suffix}`]: <
154
- T extends Gtk.Accessible = Gtk.Widget,
155
- >(
149
+ [K in keyof QueryFamilyReturns<Gtk.Widget> as `${K & string}${Suffix}`]: <T extends Gtk.Accessible = Gtk.Widget>(
156
150
  ...args: [...Head, ...QueryArgs<Kind, T>]
157
151
  ) => QueryFamilyReturns<T>[K];
158
152
  };
@@ -241,12 +235,12 @@ type RenderHookOptions<Props> = {
241
235
  wrapper?: WrapperComponent;
242
236
  } & (undefined extends Props
243
237
  ? {
244
- /** Props the hook is invoked with until `rerender` is given new ones. */
245
- initialProps?: Props;
246
- }
238
+ /** Props the hook is invoked with until `rerender` is given new ones. */
239
+ initialProps?: Props;
240
+ }
247
241
  : {
248
- initialProps: Props;
249
- });
242
+ initialProps: Props;
243
+ });
250
244
 
251
245
  /**
252
246
  * The result of {@link renderHook}: the latest hook return value plus functions to rerender with new
@@ -259,7 +253,7 @@ type RenderHookResult<Result, Props> = {
259
253
  current: Result;
260
254
  };
261
255
  /** Re-invokes the hook, keeping the previous props when none are given. */
262
- rerender: (newProps?: Props) => Promise<void>;
256
+ rerender: (...args: [] | [newProps: Props]) => Promise<void>;
263
257
  /** Unmounts the component that calls the hook. */
264
258
  unmount: () => Promise<void>;
265
259
  };
@@ -20,9 +20,7 @@ type ScrollAdjustments = {
20
20
  const slide = (widget: Gtk.Widget, value: number): Promise<void> =>
21
21
  wrapEvent(widget, () => {
22
22
  if (!(widget instanceof Gtk.Range)) {
23
- throw new TypeError(
24
- `userEvent.slide requires a Gtk.Range (e.g. Gtk.Scale), got ${getTypeTag(widget)}`,
25
- );
23
+ throw new TypeError(`userEvent.slide requires a Gtk.Range (e.g. Gtk.Scale), got ${getTypeTag(widget)}`);
26
24
  }
27
25
 
28
26
  widget.emit("change-value", Gtk.ScrollType.JUMP, value);
@@ -49,12 +49,7 @@ const getClickPoint = (clicked: Gtk.Widget, carrier: Gtk.Widget): PressPoint =>
49
49
  return { x: bounds.getX() + bounds.getWidth() / 2, y: bounds.getY() + bounds.getHeight() / 2 };
50
50
  };
51
51
 
52
- const emitGesture = (
53
- point: PressPoint,
54
- controllers: Gtk.GestureClick[],
55
- nPress: number,
56
- signal: ClickPhase,
57
- ): void => {
52
+ const emitGesture = (point: PressPoint, controllers: Gtk.GestureClick[], nPress: number, signal: ClickPhase): void => {
58
53
  for (const controller of controllers) {
59
54
  controller.emit(signal, nPress, point.x, point.y);
60
55
  }
@@ -235,8 +230,7 @@ const applyContainerClick: NativeClick = (widget, nPress) => {
235
230
  const directClickFor = (widget: Gtk.Widget): DirectClick => {
236
231
  const native = nativeClickFor(widget, true);
237
232
  const container = native === null ? containerFor(widget) : null;
238
- const gestures =
239
- native !== null || container !== null ? getAuthoredClickGestures(widget) : clickGestures(widget);
233
+ const gestures = native !== null || container !== null ? getAuthoredClickGestures(widget) : clickGestures(widget);
240
234
  const target: ClickTarget = { widget, container, gestures, isClaiming: true, native };
241
235
 
242
236
  return { sites: targetSites(widget, target), outcome: native ?? (container === null ? null : applyContainerClick) };
@@ -256,7 +250,6 @@ const deliverClick = (widget: Gtk.Widget, nPress: number): Promise<void> =>
256
250
  applyClickOutcomes(targets, nPress);
257
251
  });
258
252
 
259
- /* eslint-disable-next-line unicorn/consistent-boolean-name -- the boolean reports whether activation succeeded */
260
253
  const tryActivate = async (widget: Gtk.Widget): Promise<boolean> => {
261
254
  if (widget.getAccessibleRole() === Gtk.AccessibleRole.LABEL) {
262
255
  return false;
@@ -26,7 +26,7 @@ const headerIndexFor = (header: Gtk.Widget): number => {
26
26
  const titledColumnFor = (columns: Gtk.ColumnViewColumn[], title: string | null): Gtk.ColumnViewColumn | null => {
27
27
  const matches = columns.filter((column) => column.getTitle() === title);
28
28
 
29
- return matches.length === 1 ? matches[0] ?? null : null;
29
+ return matches.length === 1 ? (matches[0] ?? null) : null;
30
30
  };
31
31
 
32
32
  const columnFor = (header: Gtk.Widget, view: Gtk.ColumnView): Gtk.ColumnViewColumn | null => {
@@ -7,7 +7,7 @@ type ControllerConstructor<T extends Gtk.EventController> = new () => T;
7
7
  /**
8
8
  * Returns every controller of the given type attached to a widget, in the order GTK4 reports them.
9
9
  *
10
- * @param widget The widget whose controllers are inspected.
10
+ * @param target The widget whose controllers are inspected.
11
11
  * @param controllerType The controller class to match.
12
12
  * @returns The matching controllers, or an empty array when the widget has none.
13
13
  */
@@ -74,9 +74,9 @@ const getController = <T extends Gtk.EventController>(
74
74
  widget: Gtk.Accessible,
75
75
  controllerType: ControllerConstructor<T>,
76
76
  ): T => {
77
- const [controller] = getAllControllers(widget, controllerType);
77
+ const controller = queryController(widget, controllerType);
78
78
 
79
- if (controller === undefined) {
79
+ if (controller === null) {
80
80
  throw new Error(`No ${controllerType.name} controller is attached to the widget`);
81
81
  }
82
82
 
@@ -82,7 +82,7 @@ const waitForActionable = async (widget: Gtk.Widget): Promise<void> => {
82
82
  if (failure !== null) {
83
83
  throw new Error(
84
84
  `Cannot dispatch user event: ${describeWidget(widget)} did not become actionable ` +
85
- `within ${String(timeout)}ms because ${failure}`,
85
+ `within ${String(timeout)}ms because ${failure}`,
86
86
  );
87
87
  }
88
88
  };