@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.
- package/README.md +8 -165
- package/dist/accessible-native.d.ts +1 -1
- package/dist/accessible-native.d.ts.map +1 -1
- package/dist/accessible-native.js +31 -10
- package/dist/accessible-native.js.map +1 -1
- package/dist/build-queries.d.ts +15 -16
- package/dist/build-queries.d.ts.map +1 -1
- package/dist/build-queries.js +25 -23
- package/dist/build-queries.js.map +1 -1
- package/dist/cleanup-registry.d.ts +2 -1
- package/dist/cleanup-registry.d.ts.map +1 -1
- package/dist/cleanup-registry.js +11 -4
- package/dist/cleanup-registry.js.map +1 -1
- package/dist/config.d.ts +3 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/fire-event.d.ts +3 -3
- package/dist/fire-event.d.ts.map +1 -1
- package/dist/fire-event.js +14 -4
- package/dist/fire-event.js.map +1 -1
- package/dist/matchers.d.ts +56 -62
- package/dist/matchers.d.ts.map +1 -1
- package/dist/matchers.js +6 -18
- package/dist/matchers.js.map +1 -1
- package/dist/pretty-widget.d.ts.map +1 -1
- package/dist/pretty-widget.js +13 -2
- package/dist/pretty-widget.js.map +1 -1
- package/dist/queries.d.ts +7 -7
- package/dist/queries.d.ts.map +1 -1
- package/dist/queries.js +21 -12
- package/dist/queries.js.map +1 -1
- package/dist/query-helpers.d.ts +2 -2
- package/dist/query-helpers.d.ts.map +1 -1
- package/dist/query-helpers.js +10 -3
- package/dist/query-helpers.js.map +1 -1
- package/dist/render-hook.js +3 -3
- package/dist/render-hook.js.map +1 -1
- package/dist/render.d.ts +17 -17
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +66 -62
- package/dist/render.js.map +1 -1
- package/dist/role-helpers.d.ts +1 -1
- package/dist/role-helpers.d.ts.map +1 -1
- package/dist/role-helpers.js +2 -1
- package/dist/role-helpers.js.map +1 -1
- package/dist/role-naming.d.ts +2 -1
- package/dist/role-naming.d.ts.map +1 -1
- package/dist/role-naming.js +2 -1
- package/dist/role-naming.js.map +1 -1
- package/dist/screenshot.d.ts +11 -12
- package/dist/screenshot.d.ts.map +1 -1
- package/dist/screenshot.js +35 -16
- package/dist/screenshot.js.map +1 -1
- package/dist/suggestions.d.ts +1 -1
- package/dist/suggestions.d.ts.map +1 -1
- package/dist/suggestions.js +5 -3
- package/dist/suggestions.js.map +1 -1
- package/dist/traversal.d.ts +7 -6
- package/dist/traversal.d.ts.map +1 -1
- package/dist/traversal.js +32 -7
- package/dist/traversal.js.map +1 -1
- package/dist/types.d.ts +13 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/user-event/click.d.ts +17 -4
- package/dist/user-event/click.d.ts.map +1 -1
- package/dist/user-event/click.js +28 -9
- package/dist/user-event/click.js.map +1 -1
- package/dist/user-event/controller.d.ts +5 -5
- package/dist/user-event/controller.d.ts.map +1 -1
- package/dist/user-event/controller.js +7 -4
- package/dist/user-event/controller.js.map +1 -1
- package/dist/user-event/gesture.d.ts +1 -1
- package/dist/user-event/gesture.d.ts.map +1 -1
- package/dist/user-event/gesture.js +103 -29
- package/dist/user-event/gesture.js.map +1 -1
- package/dist/user-event/index.d.ts +22 -22
- package/dist/user-event/index.d.ts.map +1 -1
- package/dist/user-event/index.js +28 -25
- package/dist/user-event/index.js.map +1 -1
- package/dist/user-event/keyboard.d.ts.map +1 -1
- package/dist/user-event/keyboard.js +26 -4
- package/dist/user-event/keyboard.js.map +1 -1
- package/dist/user-event/list-row-click.d.ts +1 -1
- package/dist/user-event/list-row-click.d.ts.map +1 -1
- package/dist/user-event/list-row-click.js +3 -2
- package/dist/user-event/list-row-click.js.map +1 -1
- package/dist/user-event/pointer.d.ts.map +1 -1
- package/dist/user-event/pointer.js +9 -11
- package/dist/user-event/pointer.js.map +1 -1
- package/dist/user-event/selection.d.ts +2 -2
- package/dist/user-event/selection.d.ts.map +1 -1
- package/dist/user-event/selection.js +3 -2
- package/dist/user-event/selection.js.map +1 -1
- package/dist/user-event/text.d.ts.map +1 -1
- package/dist/user-event/text.js +31 -27
- package/dist/user-event/text.js.map +1 -1
- package/dist/wait-for.d.ts +2 -2
- package/dist/wait-for.d.ts.map +1 -1
- package/dist/wait-for.js +34 -25
- package/dist/wait-for.js.map +1 -1
- package/dist/widget-accessible-properties.d.ts +5 -4
- package/dist/widget-accessible-properties.d.ts.map +1 -1
- package/dist/widget-accessible-properties.js +85 -41
- package/dist/widget-accessible-properties.js.map +1 -1
- package/dist/widget-getters.d.ts +7 -9
- package/dist/widget-getters.d.ts.map +1 -1
- package/dist/widget-getters.js.map +1 -1
- package/dist/widget-target.d.ts +4 -0
- package/dist/widget-target.d.ts.map +1 -0
- package/dist/widget-target.js +9 -0
- package/dist/widget-target.js.map +1 -0
- package/package.json +14 -11
- package/src/accessible-native.ts +47 -11
- package/src/build-queries.ts +84 -53
- package/src/cleanup-registry.ts +11 -5
- package/src/config.ts +3 -4
- package/src/fire-event.ts +32 -8
- package/src/matchers.ts +69 -93
- package/src/pretty-widget.ts +17 -2
- package/src/queries.ts +68 -31
- package/src/query-helpers.ts +17 -9
- package/src/render-hook.tsx +3 -3
- package/src/render.tsx +71 -79
- package/src/role-helpers.ts +3 -1
- package/src/role-naming.ts +2 -1
- package/src/screenshot.ts +58 -16
- package/src/suggestions.ts +5 -3
- package/src/traversal.ts +47 -13
- package/src/types.ts +17 -15
- package/src/user-event/click.ts +37 -17
- package/src/user-event/controller.ts +10 -7
- package/src/user-event/gesture.ts +142 -33
- package/src/user-event/index.ts +133 -47
- package/src/user-event/keyboard.ts +35 -5
- package/src/user-event/list-row-click.ts +3 -3
- package/src/user-event/pointer.ts +11 -15
- package/src/user-event/selection.ts +5 -4
- package/src/user-event/text.ts +36 -32
- package/src/wait-for.ts +46 -36
- package/src/widget-accessible-properties.ts +112 -52
- package/src/widget-getters.ts +11 -13
- package/src/widget-target.ts +11 -0
package/src/build-queries.ts
CHANGED
|
@@ -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<
|
|
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<
|
|
24
|
+
type MultipleErrorBuilder<
|
|
25
|
+
Args extends unknown[],
|
|
26
|
+
Element extends Gtk.Accessible = Gtk.Accessible,
|
|
27
|
+
> = (
|
|
18
28
|
container: Container,
|
|
19
|
-
matches:
|
|
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<
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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[]
|
|
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
|
-
|
|
69
|
+
matches: readonly object[];
|
|
54
70
|
queryName: string;
|
|
55
71
|
variant: Variant;
|
|
56
72
|
shouldSuggest: boolean | undefined;
|
|
57
73
|
}): void => {
|
|
58
|
-
const { container,
|
|
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
|
|
80
|
+
const suggestions = matches.map((match) => getSuggestedQuery(requireWidget(match), variant));
|
|
81
|
+
const [suggestion] = suggestions;
|
|
65
82
|
|
|
66
|
-
if (suggestion
|
|
67
|
-
|
|
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[]
|
|
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[]
|
|
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
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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[]
|
|
172
|
-
|
|
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
|
-
*
|
|
184
|
-
*
|
|
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
|
|
188
|
-
* @param getMultipleError Builds
|
|
189
|
-
* @param getMissingError Builds
|
|
190
|
-
* @returns
|
|
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 = <
|
|
193
|
-
|
|
194
|
-
|
|
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) =>
|
|
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<
|
|
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<
|
|
260
|
+
const findBy = (container: Container, ...args: Args): Promise<Element> =>
|
|
230
261
|
waitFor(
|
|
231
262
|
() => runWithExpensiveErrorDiagnosticsDisabled(() => getByWithSuggestion(container, ...args)),
|
|
232
263
|
findOptions(getByWithSuggestion, container, args),
|
package/src/cleanup-registry.ts
CHANGED
|
@@ -6,10 +6,10 @@ const addToCleanupQueue = (fn: CleanupFunction): void => {
|
|
|
6
6
|
cleanupQueue.add(fn);
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
const
|
|
9
|
+
const runCleanupCallbacks = async (callbacks: Iterable<CleanupFunction>): Promise<void> => {
|
|
10
10
|
const failures: unknown[] = [];
|
|
11
11
|
|
|
12
|
-
for (const fn of
|
|
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
|
-
|
|
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
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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 = (
|
|
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
|
-
|
|
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(
|
|
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 (
|
|
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
|
-
|
|
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 {
|
|
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<
|
|
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) =>
|
|
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
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
|
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.
|
|
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.
|
|
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)
|
|
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.
|
|
923
|
+
function toContainElement(received: unknown, descendant: Gtk.Accessible | null): MatcherResult {
|
|
947
924
|
const widget = asWidget(received, "toContainElement");
|
|
948
|
-
const
|
|
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
|
-
`${
|
|
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 "
|
|
1024
|
-
/* eslint-disable @typescript-eslint/consistent-type-definitions --
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
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):
|
|
1034
|
-
toHaveAccessibleState(state: TristateAccessibleState, expected?: Gtk.AccessibleTristate):
|
|
1035
|
-
toHaveAccessibleState(state: Gtk.AccessibleState.INVALID, expected?: Gtk.AccessibleInvalidState):
|
|
1036
|
-
toHaveAccessibleProperty(property: StringAccessibleProperty, expected?: string):
|
|
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):
|
|
1039
|
-
toHaveAccessibleProperty(property: NumberAccessibleProperty, expected?: number):
|
|
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
|
-
):
|
|
1044
|
-
toHaveAccessibleProperty(property: Gtk.AccessibleProperty.ORIENTATION, expected?: Gtk.Orientation):
|
|
1045
|
-
toHaveAccessibleProperty(property: Gtk.AccessibleProperty.SORT, expected?: Gtk.AccessibleSort):
|
|
1046
|
-
toBeChecked():
|
|
1047
|
-
toBePartiallyChecked():
|
|
1048
|
-
toBePressed():
|
|
1049
|
-
toBePartiallyPressed():
|
|
1050
|
-
toBeDisabled():
|
|
1051
|
-
toBeEnabled():
|
|
1052
|
-
toBeVisible():
|
|
1053
|
-
toBeRooted():
|
|
1054
|
-
toBeEmptyWidget():
|
|
1055
|
-
toBeInvalid():
|
|
1056
|
-
toBeValid():
|
|
1057
|
-
toBeRequired():
|
|
1058
|
-
toHaveFocus():
|
|
1059
|
-
toHaveValue(expected?: number | string):
|
|
1060
|
-
toHaveRole(expected: Gtk.AccessibleRole):
|
|
1061
|
-
toContainElement(descendant: Gtk.
|
|
1062
|
-
toAppearBefore(other: Gtk.
|
|
1063
|
-
toAppearAfter(other: Gtk.
|
|
1064
|
-
toContainAnyByRole(...args: RoleQueryArgs):
|
|
1065
|
-
toContainOneByRole(...args: RoleQueryArgs):
|
|
1066
|
-
toContainAnyByText(...args: TextQueryArgs):
|
|
1067
|
-
toContainOneByText(...args: TextQueryArgs):
|
|
1068
|
-
toContainAnyByLabelText(...args: TextQueryArgs):
|
|
1069
|
-
toContainOneByLabelText(...args: TextQueryArgs):
|
|
1070
|
-
toContainAnyByPlaceholderText(...args: TextQueryArgs):
|
|
1071
|
-
toContainOneByPlaceholderText(...args: TextQueryArgs):
|
|
1072
|
-
toContainAnyByDisplayValue(...args: TextQueryArgs):
|
|
1073
|
-
toContainOneByDisplayValue(...args: TextQueryArgs):
|
|
1074
|
-
toHaveClass(...args: (ClassExpectation | { exact: boolean })[]):
|
|
1075
|
-
toHaveObjectProperty(name: string, expected?: unknown):
|
|
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 };
|