@gtkx/testing 2.0.0-beta.1 → 2.0.0-beta.11

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 (143) hide show
  1. package/README.md +8 -165
  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 +31 -10
  5. package/dist/accessible-native.js.map +1 -1
  6. package/dist/build-queries.d.ts +15 -16
  7. package/dist/build-queries.d.ts.map +1 -1
  8. package/dist/build-queries.js +25 -23
  9. package/dist/build-queries.js.map +1 -1
  10. package/dist/cleanup-registry.d.ts +2 -1
  11. package/dist/cleanup-registry.d.ts.map +1 -1
  12. package/dist/cleanup-registry.js +11 -4
  13. package/dist/cleanup-registry.js.map +1 -1
  14. package/dist/config.d.ts +3 -4
  15. package/dist/config.d.ts.map +1 -1
  16. package/dist/config.js.map +1 -1
  17. package/dist/fire-event.d.ts +3 -3
  18. package/dist/fire-event.d.ts.map +1 -1
  19. package/dist/fire-event.js +14 -4
  20. package/dist/fire-event.js.map +1 -1
  21. package/dist/matchers.d.ts +56 -62
  22. package/dist/matchers.d.ts.map +1 -1
  23. package/dist/matchers.js +6 -18
  24. package/dist/matchers.js.map +1 -1
  25. package/dist/pretty-widget.d.ts.map +1 -1
  26. package/dist/pretty-widget.js +13 -2
  27. package/dist/pretty-widget.js.map +1 -1
  28. package/dist/queries.d.ts +7 -7
  29. package/dist/queries.d.ts.map +1 -1
  30. package/dist/queries.js +21 -12
  31. package/dist/queries.js.map +1 -1
  32. package/dist/query-helpers.d.ts +2 -2
  33. package/dist/query-helpers.d.ts.map +1 -1
  34. package/dist/query-helpers.js +10 -3
  35. package/dist/query-helpers.js.map +1 -1
  36. package/dist/render-hook.js +3 -3
  37. package/dist/render-hook.js.map +1 -1
  38. package/dist/render.d.ts +17 -17
  39. package/dist/render.d.ts.map +1 -1
  40. package/dist/render.js +66 -62
  41. package/dist/render.js.map +1 -1
  42. package/dist/role-helpers.d.ts +1 -1
  43. package/dist/role-helpers.d.ts.map +1 -1
  44. package/dist/role-helpers.js +2 -1
  45. package/dist/role-helpers.js.map +1 -1
  46. package/dist/role-naming.d.ts +2 -1
  47. package/dist/role-naming.d.ts.map +1 -1
  48. package/dist/role-naming.js +2 -1
  49. package/dist/role-naming.js.map +1 -1
  50. package/dist/screenshot.d.ts +11 -12
  51. package/dist/screenshot.d.ts.map +1 -1
  52. package/dist/screenshot.js +35 -16
  53. package/dist/screenshot.js.map +1 -1
  54. package/dist/suggestions.d.ts +1 -1
  55. package/dist/suggestions.d.ts.map +1 -1
  56. package/dist/suggestions.js +5 -3
  57. package/dist/suggestions.js.map +1 -1
  58. package/dist/traversal.d.ts +7 -6
  59. package/dist/traversal.d.ts.map +1 -1
  60. package/dist/traversal.js +32 -7
  61. package/dist/traversal.js.map +1 -1
  62. package/dist/types.d.ts +13 -15
  63. package/dist/types.d.ts.map +1 -1
  64. package/dist/types.js.map +1 -1
  65. package/dist/user-event/click.d.ts +17 -4
  66. package/dist/user-event/click.d.ts.map +1 -1
  67. package/dist/user-event/click.js +28 -9
  68. package/dist/user-event/click.js.map +1 -1
  69. package/dist/user-event/controller.d.ts +5 -5
  70. package/dist/user-event/controller.d.ts.map +1 -1
  71. package/dist/user-event/controller.js +7 -4
  72. package/dist/user-event/controller.js.map +1 -1
  73. package/dist/user-event/gesture.d.ts +1 -1
  74. package/dist/user-event/gesture.d.ts.map +1 -1
  75. package/dist/user-event/gesture.js +103 -29
  76. package/dist/user-event/gesture.js.map +1 -1
  77. package/dist/user-event/index.d.ts +22 -22
  78. package/dist/user-event/index.d.ts.map +1 -1
  79. package/dist/user-event/index.js +28 -25
  80. package/dist/user-event/index.js.map +1 -1
  81. package/dist/user-event/keyboard.d.ts.map +1 -1
  82. package/dist/user-event/keyboard.js +26 -4
  83. package/dist/user-event/keyboard.js.map +1 -1
  84. package/dist/user-event/list-row-click.d.ts +1 -1
  85. package/dist/user-event/list-row-click.d.ts.map +1 -1
  86. package/dist/user-event/list-row-click.js +3 -2
  87. package/dist/user-event/list-row-click.js.map +1 -1
  88. package/dist/user-event/pointer.d.ts.map +1 -1
  89. package/dist/user-event/pointer.js +9 -11
  90. package/dist/user-event/pointer.js.map +1 -1
  91. package/dist/user-event/selection.d.ts +2 -2
  92. package/dist/user-event/selection.d.ts.map +1 -1
  93. package/dist/user-event/selection.js +3 -2
  94. package/dist/user-event/selection.js.map +1 -1
  95. package/dist/user-event/text.d.ts.map +1 -1
  96. package/dist/user-event/text.js +31 -27
  97. package/dist/user-event/text.js.map +1 -1
  98. package/dist/wait-for.d.ts +2 -2
  99. package/dist/wait-for.d.ts.map +1 -1
  100. package/dist/wait-for.js +34 -25
  101. package/dist/wait-for.js.map +1 -1
  102. package/dist/widget-accessible-properties.d.ts +5 -4
  103. package/dist/widget-accessible-properties.d.ts.map +1 -1
  104. package/dist/widget-accessible-properties.js +85 -41
  105. package/dist/widget-accessible-properties.js.map +1 -1
  106. package/dist/widget-getters.d.ts +7 -9
  107. package/dist/widget-getters.d.ts.map +1 -1
  108. package/dist/widget-getters.js.map +1 -1
  109. package/dist/widget-target.d.ts +4 -0
  110. package/dist/widget-target.d.ts.map +1 -0
  111. package/dist/widget-target.js +9 -0
  112. package/dist/widget-target.js.map +1 -0
  113. package/package.json +14 -11
  114. package/src/accessible-native.ts +47 -11
  115. package/src/build-queries.ts +84 -53
  116. package/src/cleanup-registry.ts +11 -5
  117. package/src/config.ts +3 -4
  118. package/src/fire-event.ts +32 -8
  119. package/src/matchers.ts +69 -93
  120. package/src/pretty-widget.ts +17 -2
  121. package/src/queries.ts +68 -31
  122. package/src/query-helpers.ts +17 -9
  123. package/src/render-hook.tsx +3 -3
  124. package/src/render.tsx +71 -79
  125. package/src/role-helpers.ts +3 -1
  126. package/src/role-naming.ts +2 -1
  127. package/src/screenshot.ts +58 -16
  128. package/src/suggestions.ts +5 -3
  129. package/src/traversal.ts +47 -13
  130. package/src/types.ts +17 -15
  131. package/src/user-event/click.ts +37 -17
  132. package/src/user-event/controller.ts +10 -7
  133. package/src/user-event/gesture.ts +142 -33
  134. package/src/user-event/index.ts +133 -47
  135. package/src/user-event/keyboard.ts +35 -5
  136. package/src/user-event/list-row-click.ts +3 -3
  137. package/src/user-event/pointer.ts +11 -15
  138. package/src/user-event/selection.ts +5 -4
  139. package/src/user-event/text.ts +36 -32
  140. package/src/wait-for.ts +46 -36
  141. package/src/widget-accessible-properties.ts +112 -52
  142. package/src/widget-getters.ts +11 -13
  143. package/src/widget-target.ts +11 -0
@@ -5,18 +5,28 @@ import { getConfig } from "./config.js";
5
5
  import { runWithExpensiveErrorDiagnosticsDisabled, suggestionError } from "./errors.js";
6
6
  import { getSuggestedQuery, type Variant } from "./suggestions.js";
7
7
  import { waitFor } from "./wait-for.js";
8
+ import { requireWidget } from "./widget-target.js";
8
9
 
9
10
  /**
10
11
  * The `queryAllBy` function a query family is built from, taking the scope to search followed by the
11
12
  * family's own matcher arguments. Its name determines the family name, so `queryAllByTestId` builds
12
13
  * a `TestId` family.
13
14
  */
14
- type QueryAllBy<Args extends unknown[]> = (container: Container, ...args: Args) => Gtk.Widget[];
15
+ type QueryAllBy<
16
+ Args extends unknown[],
17
+ Element extends Gtk.Accessible = Gtk.Accessible,
18
+ > = (
19
+ container: Container,
20
+ ...args: Args
21
+ ) => Element[];
15
22
 
16
23
  /** Builds the error thrown when a single-match query finds more than one widget. */
17
- type MultipleErrorBuilder<Args extends unknown[]> = (
24
+ type MultipleErrorBuilder<
25
+ Args extends unknown[],
26
+ Element extends Gtk.Accessible = Gtk.Accessible,
27
+ > = (
18
28
  container: Container,
19
- matches: Gtk.Widget[],
29
+ matches: Element[],
20
30
  ...args: Args
21
31
  ) => Error;
22
32
 
@@ -24,16 +34,22 @@ type MultipleErrorBuilder<Args extends unknown[]> = (
24
34
  type MissingErrorBuilder<Args extends unknown[]> = (container: Container, ...args: Args) => Error;
25
35
 
26
36
  /** The variants derived from one `queryAllBy` function, in the order DOM Testing Library returns them. */
27
- type BuiltQueries<Args extends unknown[]> = [
28
- queryBy: (container: Container, ...args: Args) => Gtk.Widget | null,
29
- getAllBy: QueryAllBy<Args>,
30
- getBy: (container: Container, ...args: Args) => Gtk.Widget,
31
- findAllBy: (container: Container, ...args: Args) => Promise<Gtk.Widget[]>,
32
- findBy: (container: Container, ...args: Args) => Promise<Gtk.Widget>,
37
+ type BuiltQueries<
38
+ Args extends unknown[],
39
+ Element extends Gtk.Accessible = Gtk.Accessible,
40
+ > = [
41
+ queryBy: (container: Container, ...args: Args) => Element | null,
42
+ getAllBy: QueryAllBy<Args, Element>,
43
+ getBy: (container: Container, ...args: Args) => Element,
44
+ findAllBy: (container: Container, ...args: Args) => Promise<Element[]>,
45
+ findBy: (container: Container, ...args: Args) => Promise<Element>,
33
46
  ];
34
47
 
35
48
  type FindQuery<Args extends unknown[]> = (container: Container, ...args: Args) => unknown;
36
- type SingleQuery<Args extends unknown[]> = (container: Container, ...args: Args) => Gtk.Widget | null;
49
+ type SingleQuery<Args extends unknown[], Element extends Gtk.Accessible> = (
50
+ container: Container,
51
+ ...args: Args
52
+ ) => Element | null;
37
53
 
38
54
  const getTrailingOptions = (args: unknown[]): MatcherOptions | undefined => {
39
55
  const last = args.at(-1);
@@ -50,21 +66,27 @@ const getQueryFamily = (queryAllBy: { name: string }): string => queryAllBy.name
50
66
 
51
67
  const maybeThrowSuggestion = (options: {
52
68
  container: Container;
53
- match: Gtk.Widget;
69
+ matches: readonly object[];
54
70
  queryName: string;
55
71
  variant: Variant;
56
72
  shouldSuggest: boolean | undefined;
57
73
  }): void => {
58
- const { container, match, queryName, variant, shouldSuggest } = options;
74
+ const { container, matches, queryName, variant, shouldSuggest } = options;
59
75
 
60
76
  if (!(shouldSuggest ?? getConfig().throwSuggestions)) {
61
77
  return;
62
78
  }
63
79
 
64
- const suggestion = getSuggestedQuery(match, variant);
80
+ const suggestions = matches.map((match) => getSuggestedQuery(requireWidget(match), variant));
81
+ const [suggestion] = suggestions;
65
82
 
66
- if (suggestion && suggestion.queryName !== queryName) {
67
- throw suggestionError(suggestion.toString(), container);
83
+ if (!suggestion || suggestion.queryName === queryName) {
84
+ return;
85
+ }
86
+ const suggestedQuery = suggestion.toString();
87
+
88
+ if (suggestions.every((candidate) => candidate?.toString() === suggestedQuery)) {
89
+ throw suggestionError(suggestedQuery, container);
68
90
  }
69
91
  };
70
92
 
@@ -101,10 +123,10 @@ const findOptions = <Args extends unknown[]>(
101
123
  };
102
124
 
103
125
  const singleFrom =
104
- <Args extends unknown[]>(
105
- allQuery: QueryAllBy<Args>,
106
- getMultipleError: MultipleErrorBuilder<Args>,
107
- ): SingleQuery<Args> =>
126
+ <Args extends unknown[], Element extends Gtk.Accessible>(
127
+ allQuery: QueryAllBy<Args, Element>,
128
+ getMultipleError: MultipleErrorBuilder<Args, Element>,
129
+ ): SingleQuery<Args, Element> =>
108
130
  (container, ...args) => {
109
131
  const matches = allQuery(container, ...args);
110
132
 
@@ -116,10 +138,10 @@ const singleFrom =
116
138
  };
117
139
 
118
140
  const allOrThrow =
119
- <Args extends unknown[]>(
120
- allQuery: QueryAllBy<Args>,
141
+ <Args extends unknown[], Element extends Gtk.Accessible>(
142
+ allQuery: QueryAllBy<Args, Element>,
121
143
  getMissingError: MissingErrorBuilder<Args>,
122
- ): QueryAllBy<Args> =>
144
+ ): QueryAllBy<Args, Element> =>
123
145
  (container, ...args) => {
124
146
  const matches = allQuery(container, ...args);
125
147
 
@@ -131,14 +153,18 @@ const allOrThrow =
131
153
  };
132
154
 
133
155
  const wrapSingleWithSuggestion =
134
- <Args extends unknown[]>(query: SingleQuery<Args>, queryName: string, variant: Variant): SingleQuery<Args> =>
156
+ <Args extends unknown[], Element extends Gtk.Accessible>(
157
+ query: SingleQuery<Args, Element>,
158
+ queryName: string,
159
+ variant: Variant,
160
+ ): SingleQuery<Args, Element> =>
135
161
  (container, ...args) => {
136
162
  const match = query(container, ...args);
137
163
 
138
164
  if (match) {
139
165
  maybeThrowSuggestion({
140
166
  container,
141
- match,
167
+ matches: [match],
142
168
  queryName,
143
169
  variant,
144
170
  shouldSuggest: extractShouldSuggest(args),
@@ -149,27 +175,30 @@ const wrapSingleWithSuggestion =
149
175
  };
150
176
 
151
177
  const wrapAllWithSuggestion =
152
- <Args extends unknown[]>(query: QueryAllBy<Args>, queryName: string, variant: Variant): QueryAllBy<Args> =>
178
+ <Args extends unknown[], Element extends Gtk.Accessible>(
179
+ query: QueryAllBy<Args, Element>,
180
+ queryName: string,
181
+ variant: Variant,
182
+ ): QueryAllBy<Args, Element> =>
153
183
  (container, ...args) => {
154
184
  const matches = query(container, ...args);
155
- const [first] = matches;
156
-
157
- if (first !== undefined) {
158
- maybeThrowSuggestion({
159
- container,
160
- match: first,
161
- queryName,
162
- variant,
163
- shouldSuggest: extractShouldSuggest(args),
164
- });
165
- }
185
+ maybeThrowSuggestion({
186
+ container,
187
+ matches,
188
+ queryName,
189
+ variant,
190
+ shouldSuggest: extractShouldSuggest(args),
191
+ });
166
192
 
167
193
  return matches;
168
194
  };
169
195
 
170
196
  const requireSingle =
171
- <Args extends unknown[]>(query: SingleQuery<Args>, getMissingError: MissingErrorBuilder<Args>) =>
172
- (container: Container, ...args: Args): Gtk.Widget => {
197
+ <Args extends unknown[], Element extends Gtk.Accessible>(
198
+ query: SingleQuery<Args, Element>,
199
+ getMissingError: MissingErrorBuilder<Args>,
200
+ ) =>
201
+ (container: Container, ...args: Args): Element => {
173
202
  const match = query(container, ...args);
174
203
 
175
204
  if (match === null) {
@@ -180,20 +209,22 @@ const requireSingle =
180
209
  };
181
210
 
182
211
  /**
183
- * Derives the `queryBy`, `getAllBy`, `getBy`, `findAllBy`, and `findBy` variants of a query family
184
- * from the family's `queryAllBy` function, mirroring DOM Testing Library's `buildQueries`. The family
185
- * name used when suggesting a better query comes from `queryAllBy.name`.
212
+ * Builds a query family from `queryAllBy`, following DOM Testing Library's `buildQueries`.
213
+ * Query suggestions use `queryAllBy.name` as the family name.
186
214
  *
187
- * @param queryAllBy Finds every widget the family matches.
188
- * @param getMultipleError Builds the error thrown when a single-match variant finds more than one widget.
189
- * @param getMissingError Builds the error thrown when a required match is missing.
190
- * @returns The variants, in the order `[queryBy, getAllBy, getBy, findAllBy, findBy]`.
215
+ * @param queryAllBy Finds all matching widgets.
216
+ * @param getMultipleError Builds errors for single-match queries finding multiple widgets.
217
+ * @param getMissingError Builds errors for required queries finding no widgets.
218
+ * @returns `[queryBy, getAllBy, getBy, findAllBy, findBy]`.
191
219
  */
192
- const buildQueries = <Args extends unknown[]>(
193
- queryAllBy: QueryAllBy<Args>,
194
- getMultipleError: MultipleErrorBuilder<Args>,
220
+ const buildQueries = <
221
+ Args extends unknown[],
222
+ Element extends Gtk.Accessible = Gtk.Accessible,
223
+ >(
224
+ queryAllBy: QueryAllBy<Args, Element>,
225
+ getMultipleError: MultipleErrorBuilder<Args, Element>,
195
226
  getMissingError: MissingErrorBuilder<Args>,
196
- ): BuiltQueries<Args> => {
227
+ ): BuiltQueries<Args, Element> => {
197
228
  const queryName = getQueryFamily(queryAllBy);
198
229
  const getAllBy = allOrThrow(queryAllBy, getMissingError);
199
230
  const getBy = requireSingle(singleFrom(getAllBy, getMultipleError), getMissingError);
@@ -206,12 +237,12 @@ const buildQueries = <Args extends unknown[]>(
206
237
 
207
238
  const getAllByWithSuggestion = wrapAllWithSuggestion(getAllBy, queryName, "getAll");
208
239
 
209
- const getByWithSuggestion: (container: Container, ...args: Args) => Gtk.Widget = (container, ...args) => {
240
+ const getByWithSuggestion: (container: Container, ...args: Args) => Element = (container, ...args) => {
210
241
  const match = getBy(container, ...args);
211
242
 
212
243
  maybeThrowSuggestion({
213
244
  container,
214
- match,
245
+ matches: [match],
215
246
  queryName,
216
247
  variant: "get",
217
248
  shouldSuggest: extractShouldSuggest(args),
@@ -220,13 +251,13 @@ const buildQueries = <Args extends unknown[]>(
220
251
  return match;
221
252
  };
222
253
 
223
- const findAllBy = (container: Container, ...args: Args): Promise<Gtk.Widget[]> =>
254
+ const findAllBy = (container: Container, ...args: Args): Promise<Element[]> =>
224
255
  waitFor(
225
256
  () => runWithExpensiveErrorDiagnosticsDisabled(() => getAllByWithSuggestion(container, ...args)),
226
257
  findOptions(getAllByWithSuggestion, container, args),
227
258
  );
228
259
 
229
- const findBy = (container: Container, ...args: Args): Promise<Gtk.Widget> =>
260
+ const findBy = (container: Container, ...args: Args): Promise<Element> =>
230
261
  waitFor(
231
262
  () => runWithExpensiveErrorDiagnosticsDisabled(() => getByWithSuggestion(container, ...args)),
232
263
  findOptions(getByWithSuggestion, container, args),
@@ -6,10 +6,10 @@ const addToCleanupQueue = (fn: CleanupFunction): void => {
6
6
  cleanupQueue.add(fn);
7
7
  };
8
8
 
9
- const runCleanup = async (): Promise<void> => {
9
+ const runCleanupCallbacks = async (callbacks: Iterable<CleanupFunction>): Promise<void> => {
10
10
  const failures: unknown[] = [];
11
11
 
12
- for (const fn of cleanupQueue) {
12
+ for (const fn of callbacks) {
13
13
  try {
14
14
  await fn();
15
15
  } catch (error) {
@@ -17,11 +17,17 @@ const runCleanup = async (): Promise<void> => {
17
17
  }
18
18
  }
19
19
 
20
- cleanupQueue.clear();
21
-
22
20
  if (failures.length > 0) {
23
21
  throw new AggregateError(failures, "Cleanup failed");
24
22
  }
25
23
  };
26
24
 
27
- export { addToCleanupQueue, runCleanup };
25
+ const runCleanup = async (): Promise<void> => {
26
+ try {
27
+ await runCleanupCallbacks(cleanupQueue);
28
+ } finally {
29
+ cleanupQueue.clear();
30
+ }
31
+ };
32
+
33
+ export { addToCleanupQueue, runCleanup, runCleanupCallbacks };
package/src/config.ts CHANGED
@@ -19,10 +19,9 @@ type Config = {
19
19
  actionabilityTimeout: number;
20
20
 
21
21
  /**
22
- * Timeout in milliseconds `render` waits for the window it shows the tree in to be laid out and
23
- * activated. A window becoming active is a round trip through the compositor rather than an
24
- * in-process check, so this is far longer than {@link Config.actionabilityTimeout}: it bounds
25
- * how long a window that never activates takes to report, and costs nothing when one does.
22
+ * Maximum milliseconds `render` waits for window layout and activation. Activation needs a
23
+ * compositor round trip, so this is longer than {@link Config.actionabilityTimeout}. Rendering
24
+ * resolves as soon as activation succeeds, without waiting out the timeout.
26
25
  */
27
26
  windowActivationTimeout: number;
28
27
  };
package/src/fire-event.ts CHANGED
@@ -7,13 +7,21 @@ import { runInAct } from "./act.js";
7
7
  */
8
8
  type WidgetEvent = {
9
9
  /** The GObject instance the signal is emitted on. */
10
- target: GObject.Object;
10
+ target: Pick<GObject.Object, "__type__">;
11
11
  /** Name of the signal to emit. */
12
12
  signalName: string;
13
13
  /** Arguments passed to the signal handlers. */
14
14
  args: unknown[];
15
15
  };
16
16
 
17
+ const requireObject = (target: object): GObject.Object => {
18
+ if (!(target instanceof GObject.Object)) {
19
+ throw new TypeError("Expected a GObject instance");
20
+ }
21
+
22
+ return target;
23
+ };
24
+
17
25
  /**
18
26
  * Records a signal emission without delivering it, so it can be inspected or held and passed to
19
27
  * {@link fireEvent} later.
@@ -23,18 +31,28 @@ type WidgetEvent = {
23
31
  * @param args Arguments to pass to the signal handlers.
24
32
  * @returns The recorded emission.
25
33
  */
26
- const createEvent = (target: GObject.Object, signalName: string, ...args: unknown[]): WidgetEvent => ({
27
- target,
34
+ const createEvent = (
35
+ target: Pick<GObject.Object, "__type__">,
36
+ signalName: string,
37
+ ...args: unknown[]
38
+ ): WidgetEvent => ({
39
+ target: requireObject(target),
28
40
  signalName,
29
41
  args,
30
42
  });
31
43
 
32
44
  const dispatchEvent = async (event: WidgetEvent): Promise<void> => {
45
+ const target = requireObject(event.target);
46
+
33
47
  await runInAct(() => {
34
- event.target.emit(event.signalName, ...event.args);
48
+ Reflect.apply(GObject.signalEmit, undefined, [target, event.signalName, ...event.args]);
35
49
  });
36
50
  };
37
51
 
52
+ const isWidgetEvent = (value: object): value is WidgetEvent =>
53
+ "target" in value && "signalName" in value && "args" in value &&
54
+ typeof value.signalName === "string" && Array.isArray(value.args);
55
+
38
56
  /**
39
57
  * Emits a recorded signal emission inside React's act environment, so any resulting state updates are
40
58
  * flushed before the promise resolves.
@@ -50,14 +68,18 @@ function fireEvent(event: WidgetEvent): Promise<void>;
50
68
  * @param signalName Name of the signal to emit.
51
69
  * @param args Arguments passed to the signal handlers.
52
70
  */
53
- function fireEvent(target: GObject.Object, signalName: string, ...args: unknown[]): Promise<void>;
71
+ function fireEvent(
72
+ target: Pick<GObject.Object, "__type__">,
73
+ signalName: string,
74
+ ...args: unknown[]
75
+ ): Promise<void>;
54
76
 
55
77
  function fireEvent(
56
- eventOrTarget: WidgetEvent | GObject.Object,
78
+ eventOrTarget: WidgetEvent | Pick<GObject.Object, "__type__">,
57
79
  signalName?: string,
58
80
  ...args: unknown[]
59
81
  ): Promise<void> {
60
- if (!(eventOrTarget instanceof GObject.Object)) {
82
+ if (signalName === undefined && isWidgetEvent(eventOrTarget)) {
61
83
  return dispatchEvent(eventOrTarget);
62
84
  }
63
85
 
@@ -65,7 +87,9 @@ function fireEvent(
65
87
  throw new Error("Unable to fire an event: pass a signal name, or an event built by createEvent.");
66
88
  }
67
89
 
68
- return dispatchEvent(createEvent(eventOrTarget, signalName, ...args));
90
+ const target = requireObject(eventOrTarget);
91
+
92
+ return dispatchEvent({ target, signalName, args });
69
93
  }
70
94
 
71
95
  export { createEvent, fireEvent, type WidgetEvent };
package/src/matchers.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SyncExpectationResult } from "@vitest/expect";
1
+ import type { SyncMatcherResult } from "vitest";
2
2
  import * as GObject from "@gtkx/gi/gobject";
3
3
  import * as Gtk from "@gtkx/gi/gtk";
4
4
  import type { ByRoleOptions, Matcher, MatcherOptions } from "./types.js";
@@ -57,7 +57,7 @@ type TextContentOptions = {
57
57
  /** The expected value for a style class: an exact class name or a regular expression. */
58
58
  type ClassExpectation = string | RegExp;
59
59
  /** The outcome of a matcher: whether it passed, and the failure text built on demand. */
60
- type MatcherResult = Pick<SyncExpectationResult, "message" | "pass">;
60
+ type MatcherResult = Pick<SyncMatcherResult, "message" | "pass">;
61
61
 
62
62
  /** The matcher state bound as `this`, supplying the test runner's deep equality check. */
63
63
  type MatcherContext = {
@@ -77,7 +77,7 @@ type TextQueryArgs = [text: Matcher, options?: MatcherOptions];
77
77
  type ContainmentMatcher<Args extends unknown[]> = (received: unknown, ...args: Args) => MatcherResult;
78
78
  type TextMatcherContext = { matcherName: string; widget: Gtk.Widget; actual: string | null };
79
79
  type ClassArguments = { expected: ClassExpectation[]; isExact: boolean };
80
- type QueryAll<Args extends unknown[]> = (container: Container, ...args: Args) => Gtk.Widget[];
80
+ type QueryAll<Args extends unknown[]> = (container: Container, ...args: Args) => object[];
81
81
 
82
82
  type BooleanAccessibleState =
83
83
  Gtk.AccessibleState.BUSY |
@@ -184,22 +184,20 @@ type MatcherImplementations = {
184
184
  /**
185
185
  * Asserts a widget's numeric value, or its display value when a string is given.
186
186
  *
187
- * Numbers come from the accessibility tree, which GTK keeps to a resolution of 0.001: it drops an
188
- * update whose value is nearer than that to the one already published, so a widget that moves by
189
- * less keeps its previous value. Numbers therefore match within 0.001, the same tolerance the
190
- * `value` option of the `ByRole` queries and `toHaveAccessibleProperty` apply. That option reads
191
- * the same tree, where the maximum of a paged widget such as a scrollbar is the last value it can
192
- * reach, its adjustment's upper bound less one page.
187
+ * Numeric values come from GTK's accessibility tree, which ignores updates smaller than 0.001.
188
+ * Comparisons use that tolerance, matching `ByRole`'s `value` option and
189
+ * `toHaveAccessibleProperty`. For paged widgets such as scrollbars, the accessible maximum is
190
+ * the adjustment's upper bound minus its page size.
193
191
  */
194
192
  toHaveValue: (received: unknown, expected?: number | string) => MatcherResult;
195
193
  /** Asserts the widget's accessible role. */
196
194
  toHaveRole: (received: unknown, expected: Gtk.AccessibleRole) => MatcherResult;
197
195
  /** Asserts the widget is, or is an ancestor of, the given widget. */
198
- toContainElement: (received: unknown, descendant: Gtk.Widget | null) => MatcherResult;
196
+ toContainElement: (received: unknown, descendant: Gtk.Accessible | null) => MatcherResult;
199
197
  /** Asserts the widget comes before the given widget in the widget tree, neither containing the other. */
200
- toAppearBefore: (received: unknown, other: Gtk.Widget) => MatcherResult;
198
+ toAppearBefore: (received: unknown, other: Gtk.Accessible) => MatcherResult;
201
199
  /** Asserts the widget comes after the given widget in the widget tree, neither containing the other. */
202
- toAppearAfter: (received: unknown, other: Gtk.Widget) => MatcherResult;
200
+ toAppearAfter: (received: unknown, other: Gtk.Accessible) => MatcherResult;
203
201
  /** Asserts at least one widget under the received one matches the `ByRole` query. */
204
202
  toContainAnyByRole: ContainmentMatcher<RoleQueryArgs>;
205
203
  /** Asserts exactly one widget under the received one matches the `ByRole` query. */
@@ -525,9 +523,6 @@ const isAppearingBefore = (first: Gtk.Widget, second: Gtk.Widget): boolean => {
525
523
  return isSeparateWidget(first, second) && root === getTreeRoot(second) && isPrecedingWidget(root, first, second);
526
524
  };
527
525
 
528
- const camelCase = (name: string): string =>
529
- name.replaceAll(/-([a-z])/g, (_match, letter: string) => letter.toUpperCase());
530
-
531
526
  const isAsymmetricMatcher = (value: unknown): boolean =>
532
527
  typeof value === "object" && value !== null && typeof Reflect.get(value, "asymmetricMatch") === "function";
533
528
 
@@ -544,7 +539,7 @@ const isEqualValue = (context: MatcherContext, actual: unknown, expected: unknow
544
539
  };
545
540
 
546
541
  const isClassMatch = (actual: string[], expected: ClassExpectation): boolean =>
547
- expected instanceof RegExp ? actual.some((name) => expected.test(name)) : actual.includes(expected);
542
+ actual.some((name) => isTextMatch(name, expected, "exact"));
548
543
 
549
544
  const isWidgetRooted = (widget: Gtk.Widget): boolean => {
550
545
  const root = widget.getRoot();
@@ -588,26 +583,8 @@ function readErrorMessageText(widget: Gtk.Widget): string | null {
588
583
  return texts.length > 0 ? texts.join(" ") : null;
589
584
  }
590
585
 
591
- function assertReadableProperty(object: GObject.Object, name: string, property: string, value: unknown): void {
592
- if (typeof value === "function") {
593
- throw new TypeError(
594
- `toHaveObjectProperty: "${name}" is shadowed by a method of the same name; ` +
595
- `call ${property}() and assert on its result instead`,
596
- );
597
- }
598
-
599
- if (value === undefined && !Reflect.has(object, property)) {
600
- throw new TypeError(
601
- `toHaveObjectProperty: no readable property "${name}" on ${describeObject(object)}; ` +
602
- "construct-only and write-only properties cannot be read back",
603
- );
604
- }
605
- }
606
-
607
586
  function readObjectProperty(object: GObject.Object, name: string): unknown {
608
- const property = camelCase(name);
609
- const value: unknown = Reflect.get(object, property);
610
- assertReadableProperty(object, name, property, value);
587
+ const value: unknown = Reflect.apply(GObject.getProperty, undefined, [object, name]);
611
588
 
612
589
  return value;
613
590
  }
@@ -804,14 +781,14 @@ function toBePartiallyPressed(received: unknown): MatcherResult {
804
781
  return stateResult(widget, "partially pressed", state === MIXED_TRISTATE);
805
782
  }
806
783
 
807
- function toAppearBefore(received: unknown, other: Gtk.Widget): MatcherResult {
784
+ function toAppearBefore(received: unknown, other: Gtk.Accessible): MatcherResult {
808
785
  const widget = asWidget(received, "toAppearBefore");
809
786
  const target = asWidget(other, "toAppearBefore");
810
787
 
811
788
  return orderResult(widget, target, "before", isAppearingBefore(widget, target));
812
789
  }
813
790
 
814
- function toAppearAfter(received: unknown, other: Gtk.Widget): MatcherResult {
791
+ function toAppearAfter(received: unknown, other: Gtk.Accessible): MatcherResult {
815
792
  const widget = asWidget(received, "toAppearAfter");
816
793
  const target = asWidget(other, "toAppearAfter");
817
794
 
@@ -907,7 +884,7 @@ function toBeRooted(received: unknown): MatcherResult {
907
884
  function toBeEmptyWidget(received: unknown): MatcherResult {
908
885
  const widget = asWidget(received, "toBeEmptyWidget");
909
886
 
910
- return stateResult(widget, "empty", widget.getFirstChild() === null && getWidgetLabelText(widget) === null);
887
+ return stateResult(widget, "empty", widget.getFirstChild() === null && !getWidgetLabelText(widget));
911
888
  }
912
889
 
913
890
  function toBeInvalid(received: unknown): MatcherResult {
@@ -943,15 +920,16 @@ function toHaveRole(received: unknown, expected: Gtk.AccessibleRole): MatcherRes
943
920
  };
944
921
  }
945
922
 
946
- function toContainElement(received: unknown, descendant: Gtk.Widget | null): MatcherResult {
923
+ function toContainElement(received: unknown, descendant: Gtk.Accessible | null): MatcherResult {
947
924
  const widget = asWidget(received, "toContainElement");
948
- const isPass = descendant !== null && (descendant === widget || descendant.isAncestor(widget));
925
+ const target = descendant === null ? null : asWidget(descendant, "toContainElement");
926
+ const isPass = target !== null && (target === widget || target.isAncestor(widget));
949
927
 
950
928
  return {
951
929
  pass: isPass,
952
930
  message: () =>
953
931
  `expected widget ${negationPrefix(isPass)}to contain ` +
954
- `${descendant === null ? "null" : describeWidget(descendant)}\n${describeWidget(widget)}`,
932
+ `${target === null ? "null" : describeWidget(target)}\n${describeWidget(widget)}`,
955
933
  };
956
934
  }
957
935
 
@@ -1020,64 +998,62 @@ const registerMatchers = (): void => {
1020
998
  registration.isRegistered = true;
1021
999
  };
1022
1000
 
1023
- declare module "@vitest/expect" {
1024
- /* eslint-disable @typescript-eslint/consistent-type-definitions -- declaration merging requires interfaces */
1025
- interface WidgetMatchers {
1026
- toHaveDisplayValue(expected?: TextExpectation): void;
1027
- toHaveTextContent(expected?: TextExpectation, options?: TextContentOptions): void;
1028
- toHaveAccessibleName(expected?: TextExpectation): void;
1029
- toHaveAccessibleDescription(expected?: TextExpectation): void;
1030
- toHaveAccessibleErrorMessage(expected?: TextExpectation): void;
1031
- toHaveSelection(expected?: TextExpectation): void;
1001
+ declare module "vitest" {
1002
+ /* eslint-disable @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-unused-vars --
1003
+ declaration merging requires an interface with vitest's exact type parameters */
1004
+ interface Matchers<R extends void | Promise<void> = void | Promise<void>, T = unknown> {
1005
+ toHaveDisplayValue(expected?: TextExpectation): R;
1006
+ toHaveTextContent(expected?: TextExpectation, options?: TextContentOptions): R;
1007
+ toHaveAccessibleName(expected?: TextExpectation): R;
1008
+ toHaveAccessibleDescription(expected?: TextExpectation): R;
1009
+ toHaveAccessibleErrorMessage(expected?: TextExpectation): R;
1010
+ toHaveSelection(expected?: TextExpectation): R;
1032
1011
  /* eslint-disable-next-line unicorn/consistent-boolean-name -- expected is the jest-dom matcher argument name */
1033
- toHaveAccessibleState(state: BooleanAccessibleState, expected?: boolean): void;
1034
- toHaveAccessibleState(state: TristateAccessibleState, expected?: Gtk.AccessibleTristate): void;
1035
- toHaveAccessibleState(state: Gtk.AccessibleState.INVALID, expected?: Gtk.AccessibleInvalidState): void;
1036
- toHaveAccessibleProperty(property: StringAccessibleProperty, expected?: string): void;
1012
+ toHaveAccessibleState(state: BooleanAccessibleState, expected?: boolean): R;
1013
+ toHaveAccessibleState(state: TristateAccessibleState, expected?: Gtk.AccessibleTristate): R;
1014
+ toHaveAccessibleState(state: Gtk.AccessibleState.INVALID, expected?: Gtk.AccessibleInvalidState): R;
1015
+ toHaveAccessibleProperty(property: StringAccessibleProperty, expected?: string): R;
1037
1016
  /* eslint-disable-next-line unicorn/consistent-boolean-name -- expected is the jest-dom matcher argument name */
1038
- toHaveAccessibleProperty(property: BooleanAccessibleProperty, expected?: boolean): void;
1039
- toHaveAccessibleProperty(property: NumberAccessibleProperty, expected?: number): void;
1017
+ toHaveAccessibleProperty(property: BooleanAccessibleProperty, expected?: boolean): R;
1018
+ toHaveAccessibleProperty(property: NumberAccessibleProperty, expected?: number): R;
1040
1019
  toHaveAccessibleProperty(
1041
1020
  property: Gtk.AccessibleProperty.AUTOCOMPLETE,
1042
1021
  expected?: Gtk.AccessibleAutocomplete,
1043
- ): void;
1044
- toHaveAccessibleProperty(property: Gtk.AccessibleProperty.ORIENTATION, expected?: Gtk.Orientation): void;
1045
- toHaveAccessibleProperty(property: Gtk.AccessibleProperty.SORT, expected?: Gtk.AccessibleSort): void;
1046
- toBeChecked(): void;
1047
- toBePartiallyChecked(): void;
1048
- toBePressed(): void;
1049
- toBePartiallyPressed(): void;
1050
- toBeDisabled(): void;
1051
- toBeEnabled(): void;
1052
- toBeVisible(): void;
1053
- toBeRooted(): void;
1054
- toBeEmptyWidget(): void;
1055
- toBeInvalid(): void;
1056
- toBeValid(): void;
1057
- toBeRequired(): void;
1058
- toHaveFocus(): void;
1059
- toHaveValue(expected?: number | string): void;
1060
- toHaveRole(expected: Gtk.AccessibleRole): void;
1061
- toContainElement(descendant: Gtk.Widget | null): void;
1062
- toAppearBefore(other: Gtk.Widget): void;
1063
- toAppearAfter(other: Gtk.Widget): void;
1064
- toContainAnyByRole(...args: RoleQueryArgs): void;
1065
- toContainOneByRole(...args: RoleQueryArgs): void;
1066
- toContainAnyByText(...args: TextQueryArgs): void;
1067
- toContainOneByText(...args: TextQueryArgs): void;
1068
- toContainAnyByLabelText(...args: TextQueryArgs): void;
1069
- toContainOneByLabelText(...args: TextQueryArgs): void;
1070
- toContainAnyByPlaceholderText(...args: TextQueryArgs): void;
1071
- toContainOneByPlaceholderText(...args: TextQueryArgs): void;
1072
- toContainAnyByDisplayValue(...args: TextQueryArgs): void;
1073
- toContainOneByDisplayValue(...args: TextQueryArgs): void;
1074
- toHaveClass(...args: (ClassExpectation | { exact: boolean })[]): void;
1075
- toHaveObjectProperty(name: string, expected?: unknown): void;
1022
+ ): R;
1023
+ toHaveAccessibleProperty(property: Gtk.AccessibleProperty.ORIENTATION, expected?: Gtk.Orientation): R;
1024
+ toHaveAccessibleProperty(property: Gtk.AccessibleProperty.SORT, expected?: Gtk.AccessibleSort): R;
1025
+ toBeChecked(): R;
1026
+ toBePartiallyChecked(): R;
1027
+ toBePressed(): R;
1028
+ toBePartiallyPressed(): R;
1029
+ toBeDisabled(): R;
1030
+ toBeEnabled(): R;
1031
+ toBeVisible(): R;
1032
+ toBeRooted(): R;
1033
+ toBeEmptyWidget(): R;
1034
+ toBeInvalid(): R;
1035
+ toBeValid(): R;
1036
+ toBeRequired(): R;
1037
+ toHaveFocus(): R;
1038
+ toHaveValue(expected?: number | string): R;
1039
+ toHaveRole(expected: Gtk.AccessibleRole): R;
1040
+ toContainElement(descendant: Gtk.Accessible | null): R;
1041
+ toAppearBefore(other: Gtk.Accessible): R;
1042
+ toAppearAfter(other: Gtk.Accessible): R;
1043
+ toContainAnyByRole(...args: RoleQueryArgs): R;
1044
+ toContainOneByRole(...args: RoleQueryArgs): R;
1045
+ toContainAnyByText(...args: TextQueryArgs): R;
1046
+ toContainOneByText(...args: TextQueryArgs): R;
1047
+ toContainAnyByLabelText(...args: TextQueryArgs): R;
1048
+ toContainOneByLabelText(...args: TextQueryArgs): R;
1049
+ toContainAnyByPlaceholderText(...args: TextQueryArgs): R;
1050
+ toContainOneByPlaceholderText(...args: TextQueryArgs): R;
1051
+ toContainAnyByDisplayValue(...args: TextQueryArgs): R;
1052
+ toContainOneByDisplayValue(...args: TextQueryArgs): R;
1053
+ toHaveClass(...args: (ClassExpectation | { exact: boolean })[]): R;
1054
+ toHaveObjectProperty(name: string, expected?: unknown): R;
1076
1055
  }
1077
-
1078
- interface Assertion extends WidgetMatchers {}
1079
- interface AsymmetricMatchersContaining extends WidgetMatchers {}
1080
- /* eslint-enable @typescript-eslint/consistent-type-definitions */
1056
+ /* eslint-enable @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-unused-vars */
1081
1057
  }
1082
1058
 
1083
1059
  export { matchers, registerMatchers, type ClassExpectation, type TextContentOptions, type TextExpectation };