@testing-library/user-event 7.0.0 → 7.1.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.
@@ -113,6 +113,33 @@
113
113
  "bug",
114
114
  "code"
115
115
  ]
116
+ },
117
+ {
118
+ "login": "zperrault",
119
+ "name": "Zach Perrault",
120
+ "avatar_url": "https://avatars0.githubusercontent.com/u/1699281?v=4",
121
+ "profile": "https://zach.website",
122
+ "contributions": [
123
+ "doc"
124
+ ]
125
+ },
126
+ {
127
+ "login": "FLGMwt",
128
+ "name": "Ryan Stelly",
129
+ "avatar_url": "https://avatars1.githubusercontent.com/u/4138357?v=4",
130
+ "profile": "https://twitter.com/ryanastelly",
131
+ "contributions": [
132
+ "doc"
133
+ ]
134
+ },
135
+ {
136
+ "login": "benmonro",
137
+ "name": "Ben Monro",
138
+ "avatar_url": "https://avatars3.githubusercontent.com/u/399236?v=4",
139
+ "profile": "https://github.com/benmonro",
140
+ "contributions": [
141
+ "code"
142
+ ]
116
143
  }
117
144
  ],
118
145
  "commitConvention": "none"
package/README.md CHANGED
@@ -20,7 +20,7 @@
20
20
  [![Build Status](https://travis-ci.org/testing-library/user-event.svg?branch=master)](https://travis-ci.org/testing-library/user-event)
21
21
  [![Maintainability](https://api.codeclimate.com/v1/badges/75f1ff4397e994c6004e/maintainability)](https://codeclimate.com/github/testing-library/user-event/maintainability)
22
22
  [![Test Coverage](https://api.codeclimate.com/v1/badges/75f1ff4397e994c6004e/test_coverage)](https://codeclimate.com/github/testing-library/user-event/test_coverage)
23
- [![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors-)
23
+ [![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
24
24
 
25
25
  ## The problem
26
26
 
@@ -74,17 +74,17 @@ import React from "react";
74
74
  import { render } from "@testing-library/react";
75
75
  import userEvent from "@testing-library/user-event";
76
76
 
77
- const { getByText, getByTestId } = test("click", () => {
78
- render(
77
+ test("click", () => {
78
+ const { getByText, getByTestId } = render(
79
79
  <div>
80
80
  <label htmlFor="checkbox">Check</label>
81
81
  <input id="checkbox" data-testid="checkbox" type="checkbox" />
82
82
  </div>
83
83
  );
84
- });
85
84
 
86
- userEvent.click(getByText("Check"));
87
- expect(getByTestId("checkbox")).toHaveAttribute("checked", true);
85
+ userEvent.click(getByText("Check"));
86
+ expect(getByTestId("checkbox")).toHaveAttribute("checked", true);
87
+ });
88
88
  ```
89
89
 
90
90
  ### `dblClick(element)`
@@ -118,7 +118,7 @@ import React from "react";
118
118
  import { render } from "@testing-library/react";
119
119
  import userEvent from "@testing-library/user-event";
120
120
 
121
- const { getByText } = test("click", () => {
121
+ const { getByTestId } = test("click", () => {
122
122
  render(<textarea data-testid="email" />);
123
123
  });
124
124
 
@@ -189,12 +189,14 @@ Thanks goes to these wonderful people
189
189
  <td align="center"><a href="https://afontcu.dev"><img src="https://avatars0.githubusercontent.com/u/9197791?v=4" width="100px;" alt="Adrià Fontcuberta"/><br /><sub><b>Adrià Fontcuberta</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Aafontcu" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/user-event/commits?author=afontcu" title="Tests">⚠️</a> <a href="https://github.com/testing-library/user-event/commits?author=afontcu" title="Code">💻</a></td>
190
190
  <td align="center"><a href="https://github.com/skywickenden"><img src="https://avatars2.githubusercontent.com/u/4930551?v=4" width="100px;" alt="Sky Wickenden"/><br /><sub><b>Sky Wickenden</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Askywickenden" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/user-event/commits?author=skywickenden" title="Code">💻</a></td>
191
191
  <td align="center"><a href="https://github.com/bogdanbodnar"><img src="https://avatars2.githubusercontent.com/u/9034868?v=4" width="100px;" alt="Bodnar Bogdan"/><br /><sub><b>Bodnar Bogdan</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Abogdanbodnar" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/user-event/commits?author=bogdanbodnar" title="Code">💻</a></td>
192
+ <td align="center"><a href="https://zach.website"><img src="https://avatars0.githubusercontent.com/u/1699281?v=4" width="100px;" alt="Zach Perrault"/><br /><sub><b>Zach Perrault</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=zperrault" title="Documentation">📖</a></td>
193
+ <td align="center"><a href="https://twitter.com/ryanastelly"><img src="https://avatars1.githubusercontent.com/u/4138357?v=4" width="100px;" alt="Ryan Stelly"/><br /><sub><b>Ryan Stelly</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=FLGMwt" title="Documentation">📖</a></td>
194
+ <td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4" width="100px;" alt="Ben Monro"/><br /><sub><b>Ben Monro</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=benmonro" title="Code">💻</a></td>
192
195
  </tr>
193
196
  </table>
194
197
 
195
198
  <!-- markdownlint-enable -->
196
199
  <!-- prettier-ignore-end -->
197
-
198
200
  <!-- ALL-CONTRIBUTORS-LIST:END -->
199
201
 
200
202
  This project follows the
@@ -0,0 +1,159 @@
1
+ import React from "react";
2
+ import { render } from "@testing-library/react";
3
+ import "@testing-library/jest-dom/extend-expect";
4
+ import userEvent from "../../src";
5
+
6
+ describe("userEvent.tab", () => {
7
+ it("should cycle elements in document tab order", () => {
8
+ const { getAllByTestId } = render(
9
+ <div>
10
+ <input data-testid="element" type="checkbox" />
11
+ <input data-testid="element" type="radio" />
12
+ <input data-testid="element" type="number" />
13
+ </div>
14
+ );
15
+
16
+ const [checkbox, radio, number] = getAllByTestId("element");
17
+
18
+ expect(document.activeElement).toBe(document.body);
19
+
20
+ userEvent.tab();
21
+
22
+ expect(document.activeElement).toBe(checkbox);
23
+
24
+ userEvent.tab();
25
+
26
+ expect(document.activeElement).toBe(radio);
27
+
28
+ userEvent.tab();
29
+
30
+ expect(document.activeElement).toBe(number);
31
+
32
+ userEvent.tab();
33
+
34
+ // cycle goes back to first element
35
+ expect(document.activeElement).toBe(checkbox);
36
+ });
37
+
38
+ it("should go backwards when shift = true", () => {
39
+ const { getAllByTestId } = render(
40
+ <div>
41
+ <input data-testid="element" type="checkbox" />
42
+ <input data-testid="element" type="radio" />
43
+ <input data-testid="element" type="number" />
44
+ </div>
45
+ );
46
+
47
+ const [checkbox, radio, number] = getAllByTestId("element");
48
+
49
+ radio.focus();
50
+
51
+ userEvent.tab({ shift: true });
52
+
53
+ expect(document.activeElement).toBe(checkbox);
54
+
55
+ userEvent.tab({ shift: true });
56
+
57
+ expect(document.activeElement).toBe(number);
58
+ });
59
+
60
+ it("should respect tabindex, regardless of dom position", () => {
61
+ const { getAllByTestId } = render(
62
+ <div>
63
+ <input data-testid="element" tabIndex={2} type="checkbox" />
64
+ <input data-testid="element" tabIndex={1} type="radio" />
65
+ <input data-testid="element" tabIndex={3} type="number" />
66
+ </div>
67
+ );
68
+
69
+ const [checkbox, radio, number] = getAllByTestId("element");
70
+
71
+ userEvent.tab();
72
+
73
+ expect(document.activeElement).toBe(radio);
74
+
75
+ userEvent.tab();
76
+
77
+ expect(document.activeElement).toBe(checkbox);
78
+
79
+ userEvent.tab();
80
+
81
+ expect(document.activeElement).toBe(number);
82
+
83
+ userEvent.tab();
84
+
85
+ expect(document.activeElement).toBe(radio);
86
+ });
87
+
88
+ it('should respect dom order when tabindex are all the same', () => {
89
+ const { getAllByTestId } = render(
90
+ <div>
91
+ <input data-testid="element" tabIndex={0} type="checkbox" />
92
+ <input data-testid="element" tabIndex={1} type="radio" />
93
+ <input data-testid="element" tabIndex={0} type="number" />
94
+ </div>
95
+ );
96
+
97
+ const [checkbox, radio, number] = getAllByTestId("element");
98
+
99
+ userEvent.tab();
100
+
101
+ expect(document.activeElement).toBe(checkbox);
102
+
103
+ userEvent.tab();
104
+
105
+ expect(document.activeElement).toBe(number);
106
+
107
+ userEvent.tab();
108
+
109
+ expect(document.activeElement).toBe(radio);
110
+
111
+ userEvent.tab();
112
+
113
+ expect(document.activeElement).toBe(checkbox);
114
+ });
115
+
116
+ it('should suport a mix of elements with/without tab index', () => {
117
+ const { getAllByTestId } = render(
118
+ <div>
119
+ <input data-testid="element" tabIndex={0} type="checkbox" />
120
+ <input data-testid="element" tabIndex={1} type="radio" />
121
+ <input data-testid="element" type="number" />
122
+ </div>
123
+ );
124
+
125
+ const [checkbox, radio, number] = getAllByTestId("element");
126
+
127
+ userEvent.tab();
128
+
129
+ expect(document.activeElement).toBe(checkbox);
130
+ userEvent.tab();
131
+
132
+ expect(document.activeElement).toBe(number);
133
+ userEvent.tab();
134
+
135
+ expect(document.activeElement).toBe(radio);
136
+
137
+ });
138
+
139
+ it('should not tab to <a> with no href', () => {
140
+ const { getAllByTestId } = render(
141
+ <div>
142
+ <input data-testid="element" tabIndex={0} type="checkbox" />
143
+ <a>ignore this</a>
144
+ <a data-testid="element" href="http://www.testingjavascript.com">a link</a>
145
+ </div>
146
+ );
147
+
148
+ const [checkbox, link] = getAllByTestId("element");
149
+
150
+ userEvent.tab();
151
+
152
+ expect(document.activeElement).toBe(checkbox);
153
+
154
+ userEvent.tab();
155
+
156
+ expect(document.activeElement).toBe(link);
157
+ });
158
+
159
+ });
package/dist/index.js CHANGED
@@ -278,6 +278,32 @@ const userEvent = {
278
278
  }
279
279
 
280
280
  element.addEventListener("blur", fireChangeEvent);
281
+ },
282
+
283
+ tab({
284
+ shift = false
285
+ } = {}) {
286
+ const focusableElements = document.querySelectorAll("input, button, select, textarea, a[href], [tabindex]");
287
+ const list = Array.prototype.filter.call(focusableElements, function (item) {
288
+ return item.getAttribute("tabindex") !== "-1";
289
+ }).sort((a, b) => {
290
+ const tabIndexA = a.getAttribute("tabindex");
291
+ const tabIndexB = b.getAttribute("tabindex");
292
+ return tabIndexA < tabIndexB ? -1 : tabIndexA > tabIndexB ? 1 : 0;
293
+ });
294
+ const index = list.indexOf(document.activeElement);
295
+ let nextIndex = shift ? index - 1 : index + 1;
296
+ let defaultIndex = shift ? list.length - 1 : 0;
297
+ const next = list[nextIndex] || list[defaultIndex];
298
+
299
+ if (next.getAttribute("tabindex") === null) {
300
+ next.setAttribute("tabindex", "0"); // jsdom requires tabIndex=0 for an item to become 'document.activeElement' (the browser does not)
301
+
302
+ next.focus();
303
+ next.removeAttribute("tabindex"); // leave no trace. :)
304
+ } else {
305
+ next.focus();
306
+ }
281
307
  }
282
308
 
283
309
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "7.0.0",
3
+ "version": "7.1.2",
4
4
  "description": "Simulate user events for react-testing-library",
5
5
  "keywords": [
6
6
  "react-testing-library",
@@ -32,24 +32,24 @@
32
32
  },
33
33
  "homepage": "https://github.com/testing-library/user-event#readme",
34
34
  "devDependencies": {
35
- "@babel/cli": "7.5.5",
36
- "@babel/core": "7.5.5",
37
- "@babel/preset-env": "7.5.5",
35
+ "@babel/cli": "7.6.2",
36
+ "@babel/core": "7.6.2",
37
+ "@babel/preset-env": "7.6.2",
38
38
  "@babel/preset-react": "7.0.0",
39
- "@testing-library/dom": "6.1.0",
39
+ "@testing-library/dom": "6.5.0",
40
40
  "@testing-library/jest-dom": "4.1.0",
41
- "@testing-library/react": "9.1.3",
41
+ "@testing-library/react": "9.2.0",
42
42
  "@testing-library/vue": "3.0.0",
43
- "all-contributors-cli": "6.8.1",
43
+ "all-contributors-cli": "6.9.1",
44
44
  "babel-core": "7.0.0-bridge.0",
45
45
  "babel-jest": "24.9.0",
46
- "codecov": "3.5.0",
47
- "husky": "3.0.4",
46
+ "codecov": "3.6.1",
47
+ "husky": "3.0.8",
48
48
  "jest": "24.9.0",
49
- "lint-staged": "9.2.5",
49
+ "lint-staged": "9.4.1",
50
50
  "prettier": "1.18.2",
51
- "react": "16.9.0",
52
- "react-dom": "16.9.0",
51
+ "react": "16.10.1",
52
+ "react-dom": "16.10.1",
53
53
  "regenerator-runtime": "0.13.3",
54
54
  "semantic-release": "15.13.24",
55
55
  "travis-deploy-once": "5.0.11",
package/src/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { fireEvent } from "@testing-library/dom";
2
2
 
3
3
  function wait(time) {
4
- return new Promise(function(resolve) {
4
+ return new Promise(function (resolve) {
5
5
  setTimeout(() => resolve(), time);
6
6
  });
7
7
  }
@@ -224,6 +224,35 @@ const userEvent = {
224
224
  }
225
225
  }
226
226
  element.addEventListener("blur", fireChangeEvent);
227
+ },
228
+
229
+ tab({ shift = false } = {}) {
230
+ const focusableElements = document.querySelectorAll(
231
+ "input, button, select, textarea, a[href], [tabindex]"
232
+ );
233
+ const list = Array.prototype.filter
234
+ .call(focusableElements, function (item) {
235
+ return item.getAttribute("tabindex") !== "-1";
236
+ })
237
+ .sort((a, b) => {
238
+ const tabIndexA = a.getAttribute("tabindex");
239
+ const tabIndexB = b.getAttribute("tabindex");
240
+ return tabIndexA < tabIndexB ? -1 : tabIndexA > tabIndexB ? 1 : 0;
241
+ });
242
+ const index = list.indexOf(document.activeElement);
243
+
244
+ let nextIndex = shift ? index - 1 : index + 1;
245
+ let defaultIndex = shift ? list.length - 1 : 0;
246
+
247
+ const next = list[nextIndex] || list[defaultIndex];
248
+
249
+ if (next.getAttribute("tabindex") === null) {
250
+ next.setAttribute("tabindex", "0"); // jsdom requires tabIndex=0 for an item to become 'document.activeElement' (the browser does not)
251
+ next.focus();
252
+ next.removeAttribute("tabindex"); // leave no trace. :)
253
+ } else {
254
+ next.focus();
255
+ }
227
256
  }
228
257
  };
229
258