@testing-library/user-event 7.1.0 → 7.2.1

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.
@@ -131,6 +131,15 @@
131
131
  "contributions": [
132
132
  "doc"
133
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
+ ]
134
143
  }
135
144
  ],
136
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-12-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
 
@@ -167,6 +167,57 @@ expect(getByTestId("val3").selected).toBe(true);
167
167
  The `values` parameter can be either an array of values or a singular scalar
168
168
  value.
169
169
 
170
+ ### `tab({shift, focusTrap})`
171
+
172
+ Fires a tab event changing the document.activeElement in the same way the
173
+ browser does.
174
+
175
+ Options:
176
+
177
+ - `shift` (default `false`) can be true or false to invert tab direction.
178
+ - `focusTrap` (default `document`) a container element to restrict the tabbing
179
+ within.
180
+
181
+ > **A note about tab**: [jsdom does not support tabbing](https://github.com/jsdom/jsdom/issues/2102), so this feature is a way to enable tests to verify tabbing from the end user's perspective. However, this limitation in jsdom will mean that components like [focus-trap-react](https://github.com/davidtheclark/focus-trap-react) will not work with `userEvent.tab()` or jsdom. For that reason, the `focusTrap` option is available to let you ensure your user is restricted within a focus-trap.
182
+
183
+ ```jsx
184
+ import React from "react";
185
+ import { render } from "@testing-library/react";
186
+ import "@testing-library/jest-dom/extend-expect";
187
+ import userEvent from "@testing-library/user-event";
188
+
189
+ it("should cycle elements in document tab order", () => {
190
+ const { getAllByTestId } = render(
191
+ <div>
192
+ <input data-testid="element" type="checkbox" />
193
+ <input data-testid="element" type="radio" />
194
+ <input data-testid="element" type="number" />
195
+ </div>
196
+ );
197
+
198
+ const [checkbox, radio, number] = getAllByTestId("element");
199
+
200
+ expect(document.body).toHaveFocus();
201
+
202
+ userEvent.tab();
203
+
204
+ expect(checkbox).toHaveFocus();
205
+
206
+ userEvent.tab();
207
+
208
+ expect(radio).toHaveFocus();
209
+
210
+ userEvent.tab();
211
+
212
+ expect(number).toHaveFocus();
213
+
214
+ userEvent.tab();
215
+
216
+ // cycle goes back to first element
217
+ expect(checkbox).toHaveFocus();
218
+ });
219
+ ```
220
+
170
221
  ## Contributors
171
222
 
172
223
  Thanks goes to these wonderful people
@@ -191,11 +242,13 @@ Thanks goes to these wonderful people
191
242
  <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
243
  <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
244
  <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>
245
+ <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>
194
246
  </tr>
195
247
  </table>
196
248
 
197
249
  <!-- markdownlint-enable -->
198
250
  <!-- prettier-ignore-end -->
251
+
199
252
  <!-- ALL-CONTRIBUTORS-LIST:END -->
200
253
 
201
254
  This project follows the
@@ -213,7 +213,8 @@ describe("userEvent.selectOptions", () => {
213
213
  <option data-testid="val3" value="3">
214
214
  3
215
215
  </option>
216
- </select>);
216
+ </select>
217
+ );
217
218
 
218
219
  userEvent.selectOptions(getByTestId("element"), "2");
219
220
 
@@ -4,156 +4,219 @@ import "@testing-library/jest-dom/extend-expect";
4
4
  import userEvent from "../../src";
5
5
 
6
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
- );
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
15
 
16
- const [checkbox, radio, number] = getAllByTestId("element");
16
+ const [checkbox, radio, number] = getAllByTestId("element");
17
17
 
18
- expect(document.activeElement).toBe(document.body);
18
+ expect(document.body).toHaveFocus();
19
19
 
20
- userEvent.tab();
20
+ userEvent.tab();
21
21
 
22
- expect(document.activeElement).toBe(checkbox);
22
+ expect(checkbox).toHaveFocus();
23
23
 
24
- userEvent.tab();
24
+ userEvent.tab();
25
25
 
26
- expect(document.activeElement).toBe(radio);
26
+ expect(radio).toHaveFocus();
27
27
 
28
- userEvent.tab();
28
+ userEvent.tab();
29
29
 
30
- expect(document.activeElement).toBe(number);
30
+ expect(number).toHaveFocus();
31
31
 
32
- userEvent.tab();
32
+ userEvent.tab();
33
33
 
34
- // cycle goes back to first element
35
- expect(document.activeElement).toBe(checkbox);
36
- });
34
+ // cycle goes back to first element
35
+ expect(checkbox).toHaveFocus();
36
+ });
37
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
- );
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
46
 
47
- const [checkbox, radio, number] = getAllByTestId("element");
47
+ const [checkbox, radio, number] = getAllByTestId("element");
48
48
 
49
- radio.focus();
49
+ radio.focus();
50
50
 
51
- userEvent.tab({ shift: true });
51
+ userEvent.tab({ shift: true });
52
52
 
53
- expect(document.activeElement).toBe(checkbox);
53
+ expect(checkbox).toHaveFocus();
54
54
 
55
- userEvent.tab({ shift: true });
55
+ userEvent.tab({ shift: true });
56
56
 
57
- expect(document.activeElement).toBe(number);
58
- });
57
+ expect(number).toHaveFocus();
58
+ });
59
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
- );
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
68
 
69
- const [checkbox, radio, number] = getAllByTestId("element");
69
+ const [checkbox, radio, number] = getAllByTestId("element");
70
70
 
71
- userEvent.tab();
71
+ userEvent.tab();
72
72
 
73
- expect(document.activeElement).toBe(radio);
73
+ expect(radio).toHaveFocus();
74
74
 
75
- userEvent.tab();
75
+ userEvent.tab();
76
76
 
77
- expect(document.activeElement).toBe(checkbox);
77
+ expect(checkbox).toHaveFocus();
78
78
 
79
- userEvent.tab();
79
+ userEvent.tab();
80
80
 
81
- expect(document.activeElement).toBe(number);
81
+ expect(number).toHaveFocus();
82
82
 
83
- userEvent.tab();
83
+ userEvent.tab();
84
84
 
85
- expect(document.activeElement).toBe(radio);
86
- });
85
+ expect(radio).toHaveFocus();
86
+ });
87
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
- );
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
96
 
97
- const [checkbox, radio, number] = getAllByTestId("element");
97
+ const [checkbox, radio, number] = getAllByTestId("element");
98
98
 
99
- userEvent.tab();
99
+ userEvent.tab();
100
100
 
101
- expect(document.activeElement).toBe(checkbox);
101
+ expect(checkbox).toHaveFocus();
102
102
 
103
- userEvent.tab();
103
+ userEvent.tab();
104
104
 
105
- expect(document.activeElement).toBe(number);
105
+ expect(number).toHaveFocus();
106
106
 
107
- userEvent.tab();
107
+ userEvent.tab();
108
108
 
109
- expect(document.activeElement).toBe(radio);
109
+ expect(radio).toHaveFocus();
110
110
 
111
- userEvent.tab();
111
+ userEvent.tab();
112
112
 
113
- expect(document.activeElement).toBe(checkbox);
114
- });
113
+ expect(checkbox).toHaveFocus();
114
+ });
115
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
- );
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
124
 
125
- const [checkbox, radio, number] = getAllByTestId("element");
125
+ const [checkbox, radio, number] = getAllByTestId("element");
126
126
 
127
- userEvent.tab();
127
+ userEvent.tab();
128
128
 
129
- expect(document.activeElement).toBe(checkbox);
130
- userEvent.tab();
129
+ expect(checkbox).toHaveFocus();
130
+ userEvent.tab();
131
131
 
132
- expect(document.activeElement).toBe(number);
133
- userEvent.tab();
132
+ expect(number).toHaveFocus();
133
+ userEvent.tab();
134
134
 
135
- expect(document.activeElement).toBe(radio);
135
+ expect(radio).toHaveFocus();
136
+ });
136
137
 
137
- });
138
+ it("should not tab to <a> with no href", () => {
139
+ const { getAllByTestId } = render(
140
+ <div>
141
+ <input data-testid="element" tabIndex={0} type="checkbox" />
142
+ <a>ignore this</a>
143
+ <a data-testid="element" href="http://www.testingjavascript.com">
144
+ a link
145
+ </a>
146
+ </div>
147
+ );
138
148
 
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
- );
149
+ const [checkbox, link] = getAllByTestId("element");
147
150
 
148
- const [checkbox, link] = getAllByTestId("element");
151
+ userEvent.tab();
149
152
 
150
- userEvent.tab();
153
+ expect(checkbox).toHaveFocus();
151
154
 
152
- expect(document.activeElement).toBe(checkbox);
155
+ userEvent.tab();
153
156
 
154
- userEvent.tab();
157
+ expect(link).toHaveFocus();
158
+ });
155
159
 
156
- expect(document.activeElement).toBe(link);
157
- });
160
+ it("should stay within a focus trab", () => {
161
+ const { getAllByTestId, getByTestId } = render(
162
+ <>
163
+ <div data-testid="div1">
164
+ <input data-testid="element" type="checkbox" />
165
+ <input data-testid="element" type="radio" />
166
+ <input data-testid="element" type="number" />
167
+ </div>
168
+ <div data-testid="div2">
169
+ <input data-testid="element" foo="bar" type="checkbox" />
170
+ <input data-testid="element" foo="bar" type="radio" />
171
+ <input data-testid="element" foo="bar" type="number" />
172
+ </div>
173
+ </>
174
+ );
158
175
 
176
+ const [div1, div2] = [getByTestId("div1"), getByTestId("div2")];
177
+ const [
178
+ checkbox1,
179
+ radio1,
180
+ number1,
181
+ checkbox2,
182
+ radio2,
183
+ number2
184
+ ] = getAllByTestId("element");
185
+
186
+ expect(document.body).toHaveFocus();
187
+
188
+ userEvent.tab({ focusTrap: div1 });
189
+
190
+ expect(checkbox1).toHaveFocus();
191
+
192
+ userEvent.tab({ focusTrap: div1 });
193
+
194
+ expect(radio1).toHaveFocus();
195
+
196
+ userEvent.tab({ focusTrap: div1 });
197
+
198
+ expect(number1).toHaveFocus();
199
+
200
+ userEvent.tab({ focusTrap: div1 });
201
+
202
+ // cycle goes back to first element
203
+ expect(checkbox1).toHaveFocus();
204
+
205
+ userEvent.tab({ focusTrap: div2 });
206
+
207
+ expect(checkbox2).toHaveFocus();
208
+
209
+ userEvent.tab({ focusTrap: div2 });
210
+
211
+ expect(radio2).toHaveFocus();
212
+
213
+ userEvent.tab({ focusTrap: div2 });
214
+
215
+ expect(number2).toHaveFocus();
216
+
217
+ userEvent.tab({ focusTrap: div2 });
218
+
219
+ // cycle goes back to first element
220
+ expect(checkbox2).toHaveFocus();
221
+ });
159
222
  });
package/dist/index.js CHANGED
@@ -281,9 +281,10 @@ const userEvent = {
281
281
  },
282
282
 
283
283
  tab({
284
- shift = false
284
+ shift = false,
285
+ focusTrap = document
285
286
  } = {}) {
286
- const focusableElements = document.querySelectorAll("input, button, select, textarea, a[href], [tabindex]");
287
+ const focusableElements = focusTrap.querySelectorAll("input, button, select, textarea, a[href], [tabindex]");
287
288
  const list = Array.prototype.filter.call(focusableElements, function (item) {
288
289
  return item.getAttribute("tabindex") !== "-1";
289
290
  }).sort((a, b) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "7.1.0",
3
+ "version": "7.2.1",
4
4
  "description": "Simulate user events for react-testing-library",
5
5
  "keywords": [
6
6
  "react-testing-library",
@@ -32,29 +32,29 @@
32
32
  },
33
33
  "homepage": "https://github.com/testing-library/user-event#readme",
34
34
  "devDependencies": {
35
- "@babel/cli": "7.6.0",
36
- "@babel/core": "7.6.0",
37
- "@babel/preset-env": "7.6.0",
38
- "@babel/preset-react": "7.0.0",
39
- "@testing-library/dom": "6.2.0",
40
- "@testing-library/jest-dom": "4.1.0",
41
- "@testing-library/react": "9.1.4",
42
- "@testing-library/vue": "3.0.0",
43
- "all-contributors-cli": "6.9.0",
35
+ "@babel/cli": "^7.7.5",
36
+ "@babel/core": "^7.7.5",
37
+ "@babel/preset-env": "^7.7.6",
38
+ "@babel/preset-react": "^7.7.4",
39
+ "@testing-library/dom": "^6.11.0",
40
+ "@testing-library/jest-dom": "^4.2.4",
41
+ "@testing-library/react": "^9.4.0",
42
+ "@testing-library/vue": "^4.1.0",
43
+ "all-contributors-cli": "^6.11.2",
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.5",
46
+ "codecov": "3.6.1",
47
+ "husky": "^3.1.0",
48
48
  "jest": "24.9.0",
49
- "lint-staged": "9.2.5",
50
- "prettier": "1.18.2",
51
- "react": "16.9.0",
52
- "react-dom": "16.9.0",
49
+ "lint-staged": "^9.5.0",
50
+ "prettier": "^1.19.1",
51
+ "react": "^16.12.0",
52
+ "react-dom": "^16.12.0",
53
53
  "regenerator-runtime": "0.13.3",
54
- "semantic-release": "15.13.24",
54
+ "semantic-release": "^15.13.31",
55
55
  "travis-deploy-once": "5.0.11",
56
- "vue": "^2.6.10",
57
- "vue-template-compiler": "^2.6.10"
56
+ "vue": "^2.6.11",
57
+ "vue-template-compiler": "^2.6.11"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "@testing-library/dom": ">=5"
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
  }
@@ -226,12 +226,12 @@ const userEvent = {
226
226
  element.addEventListener("blur", fireChangeEvent);
227
227
  },
228
228
 
229
- tab({ shift = false } = {}) {
230
- const focusableElements = document.querySelectorAll(
229
+ tab({ shift = false, focusTrap = document } = {}) {
230
+ const focusableElements = focusTrap.querySelectorAll(
231
231
  "input, button, select, textarea, a[href], [tabindex]"
232
232
  );
233
233
  const list = Array.prototype.filter
234
- .call(focusableElements, function (item) {
234
+ .call(focusableElements, function(item) {
235
235
  return item.getAttribute("tabindex") !== "-1";
236
236
  })
237
237
  .sort((a, b) => {