@testing-library/user-event 14.0.0-alpha.15 → 14.0.0-alpha.16

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.
@@ -1,5 +1,5 @@
1
1
  import { UserEvent } from '../setup';
2
- import { releaseAllKeys } from './keyboardImplementation';
2
+ import { releaseAllKeys } from './keyboardAction';
3
3
  import type { keyboardState, keyboardKey } from './types';
4
4
  export { releaseAllKeys };
5
5
  export type { keyboardKey, keyboardState };
@@ -8,16 +8,22 @@ exports.keyboard = keyboard;
8
8
  Object.defineProperty(exports, "releaseAllKeys", {
9
9
  enumerable: true,
10
10
  get: function () {
11
- return _keyboardImplementation.releaseAllKeys;
11
+ return _keyboardAction.releaseAllKeys;
12
12
  }
13
13
  });
14
14
 
15
15
  var _setup = require("../setup");
16
16
 
17
- var _keyboardImplementation = require("./keyboardImplementation");
17
+ var _keyboardAction = require("./keyboardAction");
18
+
19
+ var _parseKeyDef = require("./parseKeyDef");
18
20
 
19
21
  async function keyboard(text) {
20
- return (0, _keyboardImplementation.keyboardImplementation)(this[_setup.Config], text);
22
+ const {
23
+ keyboardMap
24
+ } = this[_setup.Config];
25
+ const actions = (0, _parseKeyDef.parseKeyDef)(keyboardMap, text);
26
+ return (0, _keyboardAction.keyboardAction)(this[_setup.Config], actions);
21
27
  }
22
28
 
23
29
  function createKeyboardState() {
@@ -0,0 +1,10 @@
1
+ import { Config } from '../setup';
2
+ import { keyboardKey } from './types';
3
+ export interface KeyboardAction {
4
+ keyDef: keyboardKey;
5
+ releasePrevious: boolean;
6
+ releaseSelf: boolean;
7
+ repeat: number;
8
+ }
9
+ export declare function keyboardAction(config: Config, actions: KeyboardAction[]): Promise<void>;
10
+ export declare function releaseAllKeys(config: Config): Promise<void>;
@@ -3,15 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.keyboardImplementation = keyboardImplementation;
6
+ exports.keyboardAction = keyboardAction;
7
7
  exports.releaseAllKeys = releaseAllKeys;
8
8
 
9
9
  var _dom = require("@testing-library/dom");
10
10
 
11
11
  var _utils = require("../utils");
12
12
 
13
- var _getNextKeyDef = require("./getNextKeyDef");
14
-
15
13
  var plugins = _interopRequireWildcard(require("./plugins"));
16
14
 
17
15
  var _getEventProps = require("./getEventProps");
@@ -20,67 +18,57 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
20
18
 
21
19
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
22
20
 
23
- async function keyboardImplementation(config, text) {
24
- var _keyboardState$repeat;
21
+ async function keyboardAction(config, actions) {
22
+ for (let i = 0; i < actions.length; i++) {
23
+ await keyboardKeyAction(config, actions[i]);
24
+
25
+ if (typeof config.delay === 'number' && i < actions.length - 1) {
26
+ await (0, _utils.wait)(config.delay);
27
+ }
28
+ }
29
+ }
25
30
 
31
+ async function keyboardKeyAction(config, {
32
+ keyDef,
33
+ releasePrevious,
34
+ releaseSelf,
35
+ repeat
36
+ }) {
26
37
  const {
27
38
  document,
28
39
  keyboardState,
29
- keyboardMap,
30
40
  delay
31
41
  } = config;
32
42
 
33
- const getCurrentElement = () => getActive(document);
43
+ const getCurrentElement = () => getActive(document); // Release the key automatically if it was pressed before.
34
44
 
35
- const {
36
- keyDef,
37
- consumedLength,
38
- releasePrevious,
39
- releaseSelf,
40
- repeat
41
- } = (_keyboardState$repeat = keyboardState.repeatKey) != null ? _keyboardState$repeat : (0, _getNextKeyDef.getNextKeyDef)(keyboardMap, text);
42
- const pressed = keyboardState.pressed.find(p => p.keyDef === keyDef); // Release the key automatically if it was pressed before.
43
- // Do not release the key on iterations on `state.repeatKey`.
44
-
45
- if (pressed && !keyboardState.repeatKey) {
45
+
46
+ const pressed = keyboardState.pressed.find(p => p.keyDef === keyDef);
47
+
48
+ if (pressed) {
46
49
  await keyup(keyDef, getCurrentElement, config, pressed.unpreventedDefault);
47
50
  }
48
51
 
49
52
  if (!releasePrevious) {
50
- const unpreventedDefault = await keydown(keyDef, getCurrentElement, config);
53
+ let unpreventedDefault = true;
51
54
 
52
- if (unpreventedDefault && hasKeyPress(keyDef, config)) {
53
- await keypress(keyDef, getCurrentElement, config);
54
- } // Release the key only on the last iteration on `state.repeatKey`.
55
+ for (let i = 1; i <= repeat; i++) {
56
+ unpreventedDefault = await keydown(keyDef, getCurrentElement, config);
55
57
 
58
+ if (unpreventedDefault && hasKeyPress(keyDef, config)) {
59
+ await keypress(keyDef, getCurrentElement, config);
60
+ }
56
61
 
57
- if (releaseSelf && repeat <= 1) {
58
- await keyup(keyDef, getCurrentElement, config, unpreventedDefault);
59
- }
60
- }
62
+ if (typeof delay === 'number' && i < repeat) {
63
+ await (0, _utils.wait)(delay);
64
+ }
65
+ } // Release the key only on the last iteration on `state.repeatKey`.
61
66
 
62
- if (repeat > 1) {
63
- keyboardState.repeatKey = {
64
- // don't consume again on the next iteration
65
- consumedLength: 0,
66
- keyDef,
67
- releasePrevious,
68
- releaseSelf,
69
- repeat: repeat - 1
70
- };
71
- } else {
72
- delete keyboardState.repeatKey;
73
- }
74
67
 
75
- if (text.length > consumedLength || repeat > 1) {
76
- if (typeof delay === 'number') {
77
- await (0, _utils.wait)(delay);
68
+ if (releaseSelf) {
69
+ await keyup(keyDef, getCurrentElement, config, unpreventedDefault);
78
70
  }
79
-
80
- return keyboardImplementation(config, text.slice(consumedLength));
81
71
  }
82
-
83
- return void undefined;
84
72
  }
85
73
 
86
74
  function getActive(document) {
@@ -1,6 +1,6 @@
1
1
  import { keyboardKey } from './types';
2
2
  /**
3
- * Get the next key from keyMap
3
+ * Parse key defintions per `keyboardMap`
4
4
  *
5
5
  * Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
6
6
  * Everything else will be interpreted as a typed character - e.g. `a`.
@@ -9,10 +9,9 @@ import { keyboardKey } from './types';
9
9
  * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
10
10
  * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
11
11
  */
12
- export declare function getNextKeyDef(keyboardMap: keyboardKey[], text: string): {
12
+ export declare function parseKeyDef(keyboardMap: keyboardKey[], text: string): {
13
13
  keyDef: keyboardKey;
14
- consumedLength: number;
15
14
  releasePrevious: boolean;
16
15
  releaseSelf: boolean;
17
16
  repeat: number;
18
- };
17
+ }[];
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parseKeyDef = parseKeyDef;
7
+
8
+ var _utils = require("../utils");
9
+
10
+ /**
11
+ * Parse key defintions per `keyboardMap`
12
+ *
13
+ * Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
14
+ * Everything else will be interpreted as a typed character - e.g. `a`.
15
+ * Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
16
+ * Keeping the key pressed can be written as `{key>}`.
17
+ * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
18
+ * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
19
+ */
20
+ function parseKeyDef(keyboardMap, text) {
21
+ const defs = [];
22
+
23
+ do {
24
+ var _keyboardMap$find;
25
+
26
+ const {
27
+ type,
28
+ descriptor,
29
+ consumedLength,
30
+ releasePrevious,
31
+ releaseSelf = true,
32
+ repeat
33
+ } = (0, _utils.readNextDescriptor)(text);
34
+ const keyDef = (_keyboardMap$find = keyboardMap.find(def => {
35
+ if (type === '[') {
36
+ var _def$code;
37
+
38
+ return ((_def$code = def.code) == null ? void 0 : _def$code.toLowerCase()) === descriptor.toLowerCase();
39
+ } else if (type === '{') {
40
+ var _def$key;
41
+
42
+ return ((_def$key = def.key) == null ? void 0 : _def$key.toLowerCase()) === descriptor.toLowerCase();
43
+ }
44
+
45
+ return def.key === descriptor;
46
+ })) != null ? _keyboardMap$find : {
47
+ key: 'Unknown',
48
+ code: 'Unknown',
49
+ [type === '[' ? 'code' : 'key']: descriptor
50
+ };
51
+ defs.push({
52
+ keyDef,
53
+ releasePrevious,
54
+ releaseSelf,
55
+ repeat
56
+ });
57
+ text = text.slice(consumedLength);
58
+ } while (text);
59
+
60
+ return defs;
61
+ }
@@ -1,5 +1,4 @@
1
1
  import { Config } from '../setup';
2
- import { getNextKeyDef } from './getNextKeyDef';
3
2
  /**
4
3
  * @internal Do not create/alter this by yourself as this type might be subject to changes.
5
4
  */
@@ -40,10 +39,6 @@ export declare type keyboardState = {
40
39
  E.g. ^1
41
40
  */
42
41
  carryChar: string;
43
- /**
44
- Repeat keydown and keypress event
45
- */
46
- repeatKey?: ReturnType<typeof getNextKeyDef>;
47
42
  };
48
43
  export declare enum DOM_KEY_LOCATION {
49
44
  STANDARD = 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.0.0-alpha.15",
3
+ "version": "14.0.0-alpha.16",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [
@@ -1,55 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getNextKeyDef = getNextKeyDef;
7
-
8
- var _utils = require("../utils");
9
-
10
- /**
11
- * Get the next key from keyMap
12
- *
13
- * Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
14
- * Everything else will be interpreted as a typed character - e.g. `a`.
15
- * Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
16
- * Keeping the key pressed can be written as `{key>}`.
17
- * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
18
- * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
19
- */
20
- function getNextKeyDef(keyboardMap, text) {
21
- var _keyboardMap$find;
22
-
23
- const {
24
- type,
25
- descriptor,
26
- consumedLength,
27
- releasePrevious,
28
- releaseSelf = true,
29
- repeat
30
- } = (0, _utils.readNextDescriptor)(text);
31
- const keyDef = (_keyboardMap$find = keyboardMap.find(def => {
32
- if (type === '[') {
33
- var _def$code;
34
-
35
- return ((_def$code = def.code) == null ? void 0 : _def$code.toLowerCase()) === descriptor.toLowerCase();
36
- } else if (type === '{') {
37
- var _def$key;
38
-
39
- return ((_def$key = def.key) == null ? void 0 : _def$key.toLowerCase()) === descriptor.toLowerCase();
40
- }
41
-
42
- return def.key === descriptor;
43
- })) != null ? _keyboardMap$find : {
44
- key: 'Unknown',
45
- code: 'Unknown',
46
- [type === '[' ? 'code' : 'key']: descriptor
47
- };
48
- return {
49
- keyDef,
50
- consumedLength,
51
- releasePrevious,
52
- releaseSelf,
53
- repeat
54
- };
55
- }
@@ -1,3 +0,0 @@
1
- import { Config } from '../setup';
2
- export declare function keyboardImplementation(config: Config, text: string): Promise<void>;
3
- export declare function releaseAllKeys(config: Config): Promise<void>;