@sprinklrjs/spaceweb 14.16.1 → 14.16.2

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,15 @@
1
+ /**
2
+ * Test-only keyboard helpers for Jest.
3
+ *
4
+ * ```ts
5
+ * import { isFocusLocked, press, tab } from '@sprinklrjs/spaceweb/test-utils/keyboard';
6
+ * ```
7
+ *
8
+ * Requires `@testing-library/user-event` v13 and `@testing-library/react`.
9
+ * The `describe*` suites also need `@testing-library/jest-dom` (`toHaveFocus`, `toBeChecked`).
10
+ * Dialog and radio suites assume the fixture names in their JSDoc (a button named
11
+ * `Second`, a button named `After`). Do not import this from application code.
12
+ */
13
+ export { isFocusLocked } from './focusLock';
14
+ export { press, shiftTab, tab, type, type KeyboardKey } from './press';
15
+ export { describeDialogKeyboard, describeRadioGroupKeyboard, describeRovingKeyboard, describeToggleKeyboard, } from './patterns';
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.describeToggleKeyboard = exports.describeRovingKeyboard = exports.describeRadioGroupKeyboard = exports.describeDialogKeyboard = exports.type = exports.tab = exports.shiftTab = exports.press = exports.isFocusLocked = void 0;
4
+ /**
5
+ * Test-only keyboard helpers for Jest.
6
+ *
7
+ * ```ts
8
+ * import { isFocusLocked, press, tab } from '@sprinklrjs/spaceweb/test-utils/keyboard';
9
+ * ```
10
+ *
11
+ * Requires `@testing-library/user-event` v13 and `@testing-library/react`.
12
+ * The `describe*` suites also need `@testing-library/jest-dom` (`toHaveFocus`, `toBeChecked`).
13
+ * Dialog and radio suites assume the fixture names in their JSDoc (a button named
14
+ * `Second`, a button named `After`). Do not import this from application code.
15
+ */
16
+ var focusLock_1 = require("./focusLock");
17
+ Object.defineProperty(exports, "isFocusLocked", { enumerable: true, get: function () { return focusLock_1.isFocusLocked; } });
18
+ var press_1 = require("./press");
19
+ Object.defineProperty(exports, "press", { enumerable: true, get: function () { return press_1.press; } });
20
+ Object.defineProperty(exports, "shiftTab", { enumerable: true, get: function () { return press_1.shiftTab; } });
21
+ Object.defineProperty(exports, "tab", { enumerable: true, get: function () { return press_1.tab; } });
22
+ Object.defineProperty(exports, "type", { enumerable: true, get: function () { return press_1.type; } });
23
+ var patterns_1 = require("./patterns");
24
+ Object.defineProperty(exports, "describeDialogKeyboard", { enumerable: true, get: function () { return patterns_1.describeDialogKeyboard; } });
25
+ Object.defineProperty(exports, "describeRadioGroupKeyboard", { enumerable: true, get: function () { return patterns_1.describeRadioGroupKeyboard; } });
26
+ Object.defineProperty(exports, "describeRovingKeyboard", { enumerable: true, get: function () { return patterns_1.describeRovingKeyboard; } });
27
+ Object.defineProperty(exports, "describeToggleKeyboard", { enumerable: true, get: function () { return patterns_1.describeToggleKeyboard; } });
@@ -0,0 +1,63 @@
1
+ import { type KeyboardKey } from './press';
2
+ /**
3
+ * Shared keyboard contracts for Spaceweb.
4
+ *
5
+ * Add a `describe*` function when a new interactive component shares a contract
6
+ * other components will reuse (menu, listbox, combobox, slider). Call it from
7
+ * that component's spec, inside `describe('keyboard interaction')`. Keep one-off branches in the component file.
8
+ * These tests press keys only — no click or hover.
9
+ *
10
+ * | Pattern | Keys | Components |
11
+ * | toggle | Tab, Space, Enter does not toggle | Checkbox, Switch |
12
+ * | radio | Tab lands on the active radio, Space selects, arrows move within the group | RadioGroup |
13
+ * | roving | next/previous, wrap, Home, End | Tabs. ButtonGroup uses the same hook |
14
+ * | dialog | Escape, Tab trap, focus return | Modal |
15
+ */
16
+ type RenderFn = () => void;
17
+ export declare function describeToggleKeyboard(options: {
18
+ role: 'checkbox' | 'switch';
19
+ /** Renders one unchecked, enabled control. Space must check it. */
20
+ renderEnabled: RenderFn;
21
+ /** Renders one unchecked, disabled control. Space must not check it. */
22
+ renderDisabled: RenderFn;
23
+ }): void;
24
+ export declare function describeRadioGroupKeyboard(options: {
25
+ /** Renders Apple and Pear. `value` selects one. Include a button named After after the group. */
26
+ renderGroup: (props?: {
27
+ value?: string;
28
+ disabled?: boolean;
29
+ }) => void;
30
+ }): void;
31
+ /**
32
+ * Roving focus across `setup()` stops. The first stop is the tab entry.
33
+ * `setup` must return at least three stops in keyboard order.
34
+ */
35
+ export declare function describeRovingKeyboard(options: {
36
+ name: string;
37
+ setup: () => HTMLElement[];
38
+ nextKey: KeyboardKey;
39
+ previousKey: KeyboardKey;
40
+ ignoredKeys?: KeyboardKey[];
41
+ }): void;
42
+ export declare function describeDialogKeyboard(options: {
43
+ /**
44
+ * Renders one dialog. `onClose` must unmount it. `first` and `last` are the
45
+ * focusable ends inside the dialog. Include a button named Second between them
46
+ * when the dialog is closeable.
47
+ */
48
+ mount: (props?: {
49
+ closeable?: boolean;
50
+ focusLock?: boolean;
51
+ autoFocus?: boolean;
52
+ }) => {
53
+ dialog: HTMLElement;
54
+ first: HTMLElement;
55
+ last: HTMLElement;
56
+ };
57
+ /**
58
+ * Renders a closed dialog and a trigger. Enter on the trigger opens the dialog.
59
+ * Escape must close it and return focus to the trigger.
60
+ */
61
+ openFromTrigger: () => HTMLElement;
62
+ }): void;
63
+ export {};
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.describeDialogKeyboard = exports.describeRovingKeyboard = exports.describeRadioGroupKeyboard = exports.describeToggleKeyboard = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var react_1 = require("@testing-library/react");
6
+ var focusLock_1 = require("./focusLock");
7
+ var press_1 = require("./press");
8
+ function describeToggleKeyboard(options) {
9
+ var role = options.role, renderEnabled = options.renderEnabled, renderDisabled = options.renderDisabled;
10
+ test('Tab moves focus onto the control', function () {
11
+ renderEnabled();
12
+ var control = react_1.screen.getByRole(role);
13
+ (0, press_1.tab)();
14
+ expect(control).toHaveFocus();
15
+ });
16
+ test('Space checks the control', function () {
17
+ renderEnabled();
18
+ var control = react_1.screen.getByRole(role);
19
+ (0, press_1.tab)();
20
+ (0, press_1.press)('Space');
21
+ expect(control).toBeChecked();
22
+ });
23
+ test('Enter does not check the control', function () {
24
+ renderEnabled();
25
+ var control = react_1.screen.getByRole(role);
26
+ (0, press_1.tab)();
27
+ (0, press_1.press)('Enter');
28
+ expect(control).not.toBeChecked();
29
+ });
30
+ test('Space does not check a disabled control', function () {
31
+ renderDisabled();
32
+ var control = react_1.screen.getByRole(role);
33
+ (0, react_1.act)(function () {
34
+ control.focus();
35
+ });
36
+ (0, press_1.press)('Space');
37
+ expect(control).not.toBeChecked();
38
+ });
39
+ }
40
+ exports.describeToggleKeyboard = describeToggleKeyboard;
41
+ function describeRadioGroupKeyboard(options) {
42
+ test('Tab focuses the first radio when none is selected', function () {
43
+ options.renderGroup();
44
+ var _a = tslib_1.__read(react_1.screen.getAllByRole('radio'), 2), first = _a[0], second = _a[1];
45
+ (0, press_1.tab)();
46
+ expect(first).toHaveFocus();
47
+ expect(second).toHaveAttribute('tabindex', '-1');
48
+ });
49
+ test('Tab focuses the selected radio', function () {
50
+ options.renderGroup({ value: 'pear' });
51
+ var _a = tslib_1.__read(react_1.screen.getAllByRole('radio'), 2), first = _a[0], second = _a[1];
52
+ (0, press_1.tab)();
53
+ expect(second).toHaveFocus();
54
+ expect(first).toHaveAttribute('tabindex', '-1');
55
+ });
56
+ test('Tab leaves the group from the active radio', function () {
57
+ options.renderGroup({ value: 'pear' });
58
+ var second = react_1.screen.getAllByRole('radio')[1];
59
+ (0, press_1.tab)();
60
+ expect(second).toHaveFocus();
61
+ (0, press_1.tab)();
62
+ expect(react_1.screen.getByRole('button', { name: 'After' })).toHaveFocus();
63
+ });
64
+ test('Space selects the active radio', function () {
65
+ options.renderGroup();
66
+ var first = react_1.screen.getAllByRole('radio')[0];
67
+ (0, press_1.tab)();
68
+ (0, press_1.press)('Space');
69
+ expect(first).toBeChecked();
70
+ });
71
+ test('Space does not select a radio in a disabled group', function () {
72
+ options.renderGroup({ disabled: true });
73
+ var first = react_1.screen.getAllByRole('radio')[0];
74
+ (0, react_1.act)(function () {
75
+ first.focus();
76
+ });
77
+ (0, press_1.press)('Space');
78
+ expect(first).not.toBeChecked();
79
+ });
80
+ }
81
+ exports.describeRadioGroupKeyboard = describeRadioGroupKeyboard;
82
+ /**
83
+ * Roving focus across `setup()` stops. The first stop is the tab entry.
84
+ * `setup` must return at least three stops in keyboard order.
85
+ */
86
+ function describeRovingKeyboard(options) {
87
+ var name = options.name, setup = options.setup, nextKey = options.nextKey, previousKey = options.previousKey, _a = options.ignoredKeys, ignoredKeys = _a === void 0 ? [] : _a;
88
+ describe(name, function () {
89
+ test("".concat(nextKey, " moves to the next stop and wraps to the first"), function () {
90
+ var stops = setup();
91
+ (0, press_1.tab)();
92
+ expect(stops[0]).toHaveFocus();
93
+ (0, press_1.press)(nextKey);
94
+ expect(stops[1]).toHaveFocus();
95
+ (0, press_1.press)(nextKey);
96
+ expect(stops[2]).toHaveFocus();
97
+ (0, press_1.press)(nextKey);
98
+ expect(stops[0]).toHaveFocus();
99
+ });
100
+ test("".concat(previousKey, " wraps from the first stop to the last"), function () {
101
+ var stops = setup();
102
+ (0, press_1.tab)();
103
+ (0, press_1.press)(previousKey);
104
+ expect(stops[stops.length - 1]).toHaveFocus();
105
+ });
106
+ test('Home moves to the first stop and End moves to the last', function () {
107
+ var stops = setup();
108
+ (0, press_1.tab)();
109
+ (0, press_1.press)('End');
110
+ expect(stops[stops.length - 1]).toHaveFocus();
111
+ (0, press_1.press)('Home');
112
+ expect(stops[0]).toHaveFocus();
113
+ });
114
+ if (ignoredKeys.length > 0) {
115
+ test("".concat(ignoredKeys.join(', '), " do not move focus"), function () {
116
+ var stops = setup();
117
+ (0, press_1.tab)();
118
+ ignoredKeys.forEach(function (key) {
119
+ (0, press_1.press)(key);
120
+ expect(stops[0]).toHaveFocus();
121
+ });
122
+ });
123
+ }
124
+ });
125
+ }
126
+ exports.describeRovingKeyboard = describeRovingKeyboard;
127
+ function describeDialogKeyboard(options) {
128
+ var _this = this;
129
+ test('Escape closes a closeable dialog', function () {
130
+ options.mount();
131
+ (0, press_1.press)('Escape');
132
+ expect(react_1.screen.queryByRole('dialog')).toBeNull();
133
+ });
134
+ test('Escape does not close a dialog that is not closeable', function () {
135
+ options.mount({ closeable: false });
136
+ (0, press_1.press)('Escape');
137
+ react_1.screen.getByRole('dialog');
138
+ });
139
+ test('autoFocus moves focus to the first control inside the dialog', function () {
140
+ var first = options.mount().first;
141
+ expect(first).toHaveFocus();
142
+ });
143
+ test('autoFocus false leaves the first control unfocused', function () {
144
+ var first = options.mount({ autoFocus: false }).first;
145
+ expect(first).not.toHaveFocus();
146
+ });
147
+ test('Tab moves through the dialog controls', function () {
148
+ var _a = options.mount(), first = _a.first, last = _a.last;
149
+ expect(first).toHaveFocus();
150
+ (0, press_1.tab)();
151
+ expect(react_1.screen.getByRole('button', { name: 'Second' })).toHaveFocus();
152
+ (0, press_1.tab)();
153
+ expect(last).toHaveFocus();
154
+ });
155
+ test('focus stays locked inside the dialog', function () {
156
+ var _a = options.mount(), first = _a.first, last = _a.last;
157
+ expect((0, focusLock_1.isFocusLocked)({ first: first, last: last })).toBe(true);
158
+ });
159
+ test('Tab does not wrap inside the dialog when focus is not locked', function () {
160
+ var _a = options.mount({ focusLock: false }), first = _a.first, last = _a.last;
161
+ expect((0, focusLock_1.isFocusLocked)({ first: first, last: last })).toBe(false);
162
+ });
163
+ test('Escape returns focus to the trigger', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
164
+ var trigger;
165
+ return tslib_1.__generator(this, function (_a) {
166
+ switch (_a.label) {
167
+ case 0:
168
+ trigger = options.openFromTrigger();
169
+ (0, press_1.tab)();
170
+ expect(trigger).toHaveFocus();
171
+ (0, press_1.press)('Enter');
172
+ expect(react_1.screen.getByRole('dialog')).toBeTruthy();
173
+ (0, press_1.press)('Escape');
174
+ return [4 /*yield*/, (0, react_1.waitFor)(function () {
175
+ expect(react_1.screen.queryByRole('dialog')).toBeNull();
176
+ expect(trigger).toHaveFocus();
177
+ })];
178
+ case 1:
179
+ _a.sent();
180
+ return [2 /*return*/];
181
+ }
182
+ });
183
+ }); });
184
+ }
185
+ exports.describeDialogKeyboard = describeDialogKeyboard;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Keys the keyboard harness can press. Add a key here when a new pattern needs it.
3
+ * Tab uses `userEvent.tab()` — `{Tab}` is not a substitute.
4
+ */
5
+ export type KeyboardKey = 'Enter' | 'Space' | 'Escape' | 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Home' | 'End';
6
+ export declare function press(key: KeyboardKey, modifiers?: {
7
+ shift?: boolean;
8
+ }): void;
9
+ /** Types into the focused element. Does not click or move focus. */
10
+ export declare function type(text: string): void;
11
+ export declare function tab(): void;
12
+ export declare function shiftTab(): void;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shiftTab = exports.tab = exports.type = exports.press = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var user_event_1 = tslib_1.__importDefault(require("@testing-library/user-event"));
6
+ function keyToken(key) {
7
+ return key === 'Space' ? '{space}' : "{".concat(key, "}");
8
+ }
9
+ function press(key, modifiers) {
10
+ var token = keyToken(key);
11
+ if (modifiers === null || modifiers === void 0 ? void 0 : modifiers.shift) {
12
+ user_event_1.default.keyboard("{Shift>}".concat(token, "{/Shift}"));
13
+ return;
14
+ }
15
+ user_event_1.default.keyboard(token);
16
+ }
17
+ exports.press = press;
18
+ /** Types into the focused element. Does not click or move focus. */
19
+ function type(text) {
20
+ user_event_1.default.keyboard(text);
21
+ }
22
+ exports.type = type;
23
+ function tab() {
24
+ user_event_1.default.tab();
25
+ }
26
+ exports.tab = tab;
27
+ function shiftTab() {
28
+ user_event_1.default.tab({ shift: true });
29
+ }
30
+ exports.shiftTab = shiftTab;