@gtkx/testing 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ import * as Gtk from "@gtkx/ffi/gtk";
2
+ /**
3
+ * Extracts the accessible text content from a widget based on its role.
4
+ *
5
+ * @param widget - The widget to extract text from
6
+ * @returns The accessible text or null if none found
7
+ */
8
+ export declare const getWidgetText: (widget: Gtk.Widget) => string | null;
9
+ /**
10
+ * Gets the test ID from a widget (uses the widget's name property).
11
+ *
12
+ * @param widget - The widget to get the test ID from
13
+ * @returns The test ID or null if not set
14
+ */
15
+ export declare const getWidgetTestId: (widget: Gtk.Widget) => string | null;
16
+ /**
17
+ * Gets the checked state from toggle-like widgets.
18
+ *
19
+ * @param widget - The widget to get the checked state from
20
+ * @returns The checked state or null if not applicable
21
+ */
22
+ export declare const getWidgetCheckedState: (widget: Gtk.Widget) => boolean | null;
23
+ /**
24
+ * Gets the expanded state from expander widgets.
25
+ *
26
+ * @param widget - The widget to get the expanded state from
27
+ * @returns The expanded state or null if not applicable
28
+ */
29
+ export declare const getWidgetExpandedState: (widget: Gtk.Widget) => boolean | null;
@@ -0,0 +1,146 @@
1
+ import { getNativeObject } from "@gtkx/ffi";
2
+ import * as Gtk from "@gtkx/ffi/gtk";
3
+ const ROLES_WITH_INTERNAL_LABELS = new Set([
4
+ Gtk.AccessibleRole.BUTTON,
5
+ Gtk.AccessibleRole.TOGGLE_BUTTON,
6
+ Gtk.AccessibleRole.CHECKBOX,
7
+ Gtk.AccessibleRole.RADIO,
8
+ Gtk.AccessibleRole.MENU_ITEM,
9
+ Gtk.AccessibleRole.MENU_ITEM_CHECKBOX,
10
+ Gtk.AccessibleRole.MENU_ITEM_RADIO,
11
+ Gtk.AccessibleRole.TAB,
12
+ Gtk.AccessibleRole.LINK,
13
+ ]);
14
+ const isInternalLabel = (widget) => {
15
+ if (widget.getAccessibleRole() !== Gtk.AccessibleRole.LABEL)
16
+ return false;
17
+ const parent = widget.getParent();
18
+ if (!parent)
19
+ return false;
20
+ if (parent.getAccessibleRole === undefined)
21
+ return false;
22
+ const parentRole = parent.getAccessibleRole();
23
+ if (!parentRole)
24
+ return false;
25
+ return ROLES_WITH_INTERNAL_LABELS.has(parentRole);
26
+ };
27
+ const getLabelText = (widget) => {
28
+ const asLabel = widget;
29
+ const asInscription = widget;
30
+ return asLabel.getLabel?.() ?? asInscription.getText?.() ?? null;
31
+ };
32
+ const collectChildLabels = (widget) => {
33
+ const labels = [];
34
+ let child = widget.getFirstChild();
35
+ while (child) {
36
+ if (child.getAccessibleRole() === Gtk.AccessibleRole.LABEL) {
37
+ const labelText = getLabelText(child);
38
+ if (labelText)
39
+ labels.push(labelText);
40
+ }
41
+ labels.push(...collectChildLabels(child));
42
+ child = child.getNextSibling();
43
+ }
44
+ return labels;
45
+ };
46
+ /**
47
+ * Extracts the accessible text content from a widget based on its role.
48
+ *
49
+ * @param widget - The widget to extract text from
50
+ * @returns The accessible text or null if none found
51
+ */
52
+ export const getWidgetText = (widget) => {
53
+ if (isInternalLabel(widget))
54
+ return null;
55
+ const role = widget.getAccessibleRole();
56
+ if (role === undefined)
57
+ return null;
58
+ switch (role) {
59
+ case Gtk.AccessibleRole.BUTTON:
60
+ case Gtk.AccessibleRole.LINK:
61
+ case Gtk.AccessibleRole.TAB: {
62
+ const directLabel = widget.getLabel?.() ??
63
+ widget.getLabel?.() ??
64
+ widget.getLabel?.();
65
+ if (directLabel)
66
+ return directLabel;
67
+ const childLabels = collectChildLabels(widget);
68
+ return childLabels.length > 0 ? childLabels.join(" ") : null;
69
+ }
70
+ case Gtk.AccessibleRole.TOGGLE_BUTTON:
71
+ return widget.getLabel?.() ?? null;
72
+ case Gtk.AccessibleRole.CHECKBOX:
73
+ case Gtk.AccessibleRole.RADIO:
74
+ return widget.getLabel?.() ?? null;
75
+ case Gtk.AccessibleRole.LABEL:
76
+ return getLabelText(widget);
77
+ case Gtk.AccessibleRole.TEXT_BOX:
78
+ case Gtk.AccessibleRole.SEARCH_BOX:
79
+ case Gtk.AccessibleRole.SPIN_BUTTON:
80
+ return getNativeObject(widget.handle, Gtk.Editable).getText() ?? null;
81
+ case Gtk.AccessibleRole.GROUP:
82
+ return widget.getLabel?.() ?? null;
83
+ case Gtk.AccessibleRole.WINDOW:
84
+ case Gtk.AccessibleRole.DIALOG:
85
+ case Gtk.AccessibleRole.ALERT_DIALOG:
86
+ return widget.getTitle() ?? null;
87
+ case Gtk.AccessibleRole.TAB_PANEL: {
88
+ const parent = widget.getParent();
89
+ if (parent) {
90
+ const stack = parent;
91
+ const page = stack.getPage?.(widget);
92
+ if (page) {
93
+ return page.getTitle() ?? null;
94
+ }
95
+ }
96
+ return null;
97
+ }
98
+ default:
99
+ return null;
100
+ }
101
+ };
102
+ /**
103
+ * Gets the test ID from a widget (uses the widget's name property).
104
+ *
105
+ * @param widget - The widget to get the test ID from
106
+ * @returns The test ID or null if not set
107
+ */
108
+ export const getWidgetTestId = (widget) => {
109
+ return widget.getName();
110
+ };
111
+ /**
112
+ * Gets the checked state from toggle-like widgets.
113
+ *
114
+ * @param widget - The widget to get the checked state from
115
+ * @returns The checked state or null if not applicable
116
+ */
117
+ export const getWidgetCheckedState = (widget) => {
118
+ const role = widget.getAccessibleRole();
119
+ switch (role) {
120
+ case Gtk.AccessibleRole.CHECKBOX:
121
+ case Gtk.AccessibleRole.RADIO:
122
+ return widget.getActive();
123
+ case Gtk.AccessibleRole.TOGGLE_BUTTON:
124
+ return widget.getActive();
125
+ case Gtk.AccessibleRole.SWITCH:
126
+ return widget.getActive();
127
+ default:
128
+ return null;
129
+ }
130
+ };
131
+ /**
132
+ * Gets the expanded state from expander widgets.
133
+ *
134
+ * @param widget - The widget to get the expanded state from
135
+ * @returns The expanded state or null if not applicable
136
+ */
137
+ export const getWidgetExpandedState = (widget) => {
138
+ const role = widget.getAccessibleRole();
139
+ if (role === Gtk.AccessibleRole.BUTTON) {
140
+ const parent = widget.getParent();
141
+ if (!parent)
142
+ return null;
143
+ return parent.getExpanded?.() ?? null;
144
+ }
145
+ return null;
146
+ };
package/dist/widget.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  import * as Gtk from "@gtkx/ffi/gtk";
2
2
  export declare const isEditable: (widget: Gtk.Widget) => boolean;
3
- export declare const hasLabel: (widget: Gtk.Widget) => boolean;
package/dist/widget.js CHANGED
@@ -7,16 +7,3 @@ const EDITABLE_ROLES = new Set([
7
7
  export const isEditable = (widget) => {
8
8
  return EDITABLE_ROLES.has(widget.getAccessibleRole());
9
9
  };
10
- const LABEL_ROLES = new Set([
11
- Gtk.AccessibleRole.BUTTON,
12
- Gtk.AccessibleRole.TOGGLE_BUTTON,
13
- Gtk.AccessibleRole.CHECKBOX,
14
- Gtk.AccessibleRole.RADIO,
15
- Gtk.AccessibleRole.LABEL,
16
- Gtk.AccessibleRole.MENU_ITEM,
17
- Gtk.AccessibleRole.MENU_ITEM_CHECKBOX,
18
- Gtk.AccessibleRole.MENU_ITEM_RADIO,
19
- ]);
20
- export const hasLabel = (widget) => {
21
- return LABEL_ROLES.has(widget.getAccessibleRole());
22
- };
package/dist/within.js CHANGED
@@ -1,4 +1,4 @@
1
- import * as queries from "./queries.js";
1
+ import { bindQueries } from "./bind-queries.js";
2
2
  /**
3
3
  * Creates scoped query methods for a container widget.
4
4
  *
@@ -25,13 +25,4 @@ import * as queries from "./queries.js";
25
25
  *
26
26
  * @see {@link screen} for global queries
27
27
  */
28
- export const within = (container) => ({
29
- findByRole: (role, options) => queries.findByRole(container, role, options),
30
- findByLabelText: (text, options) => queries.findByLabelText(container, text, options),
31
- findByText: (text, options) => queries.findByText(container, text, options),
32
- findByTestId: (testId, options) => queries.findByTestId(container, testId, options),
33
- findAllByRole: (role, options) => queries.findAllByRole(container, role, options),
34
- findAllByLabelText: (text, options) => queries.findAllByLabelText(container, text, options),
35
- findAllByText: (text, options) => queries.findAllByText(container, text, options),
36
- findAllByTestId: (testId, options) => queries.findAllByTestId(container, testId, options),
37
- });
28
+ export const within = (container) => bindQueries(container);
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "@gtkx/testing",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Testing utilities for GTKX applications",
5
5
  "keywords": [
6
+ "gtkx",
6
7
  "gtk",
7
8
  "gtk4",
8
9
  "react",
10
+ "typescript",
9
11
  "testing",
10
- "testing-library"
12
+ "testing-library",
13
+ "linux",
14
+ "desktop"
11
15
  ],
12
16
  "homepage": "https://eugeniodepalo.github.io/gtkx",
13
17
  "bugs": {
@@ -32,14 +36,14 @@
32
36
  "dist"
33
37
  ],
34
38
  "dependencies": {
35
- "@gtkx/ffi": "0.11.0",
36
- "@gtkx/native": "0.11.0",
37
- "@gtkx/react": "0.11.0"
39
+ "@gtkx/ffi": "0.12.0",
40
+ "@gtkx/native": "0.12.0",
41
+ "@gtkx/react": "0.12.0"
38
42
  },
39
43
  "devDependencies": {
40
44
  "@types/react-reconciler": "^0.32.3",
41
45
  "react-reconciler": "^0.33.0",
42
- "@gtkx/vitest": "0.11.0"
46
+ "@gtkx/vitest": "0.12.0"
43
47
  },
44
48
  "scripts": {
45
49
  "build": "tsc -b && cp ../../README.md .",