@testing-library/user-event
Advanced tools
Comparing version 8.0.2 to 8.0.3
@@ -160,3 +160,3 @@ import React from "react"; | ||
it("should stay within a focus trab", () => { | ||
it("should stay within a focus trap", () => { | ||
const { getAllByTestId, getByTestId } = render( | ||
@@ -223,2 +223,27 @@ <> | ||
}); | ||
// prior to node 11, Array.sort was unstable for arrays w/ length > 10. | ||
// https://twitter.com/mathias/status/1036626116654637057 | ||
// for this reason, the tab() function needs to account for this in it's sorting. | ||
// for example under node 10 in this test: | ||
// > 'abcdefghijklmnopqrstuvwxyz'.split('').sort(() => 0).join('') | ||
// will give you 'nacdefghijklmbopqrstuvwxyz' | ||
it("should support unstable sorting environments like node 10", () => { | ||
const letters = "abcdefghijklmnopqrstuvwxyz"; | ||
const { getByTestId } = render( | ||
<> | ||
{letters.split("").map(letter => ( | ||
<input key={letter} type="text" data-testid={letter} /> | ||
))} | ||
</> | ||
); | ||
expect.assertions(26); | ||
letters.split("").forEach(letter => { | ||
userEvent.tab(); | ||
expect(getByTestId(letter)).toHaveFocus(); | ||
}); | ||
}); | ||
}); |
@@ -292,13 +292,21 @@ "use strict"; | ||
const focusableElements = focusTrap.querySelectorAll("input, button, select, textarea, a[href], [tabindex]"); | ||
const list = Array.prototype.filter.call(focusableElements, function (item) { | ||
let list = Array.prototype.filter.call(focusableElements, function (item) { | ||
return item.getAttribute("tabindex") !== "-1"; | ||
}).sort((a, b) => { | ||
const tabIndexA = a.getAttribute("tabindex"); | ||
const tabIndexB = b.getAttribute("tabindex"); | ||
return tabIndexA < tabIndexB ? -1 : tabIndexA > tabIndexB ? 1 : 0; | ||
}).map((el, idx) => ({ | ||
el, | ||
idx | ||
})).sort((a, b) => { | ||
const tabIndexA = a.el.getAttribute("tabindex"); | ||
const tabIndexB = b.el.getAttribute("tabindex"); | ||
const diff = tabIndexA - tabIndexB; | ||
return diff !== 0 ? diff : a.idx - b.idx; | ||
}); | ||
const index = list.indexOf(document.activeElement); | ||
const index = list.findIndex(({ | ||
el | ||
}) => el === document.activeElement); | ||
let nextIndex = shift ? index - 1 : index + 1; | ||
let defaultIndex = shift ? list.length - 1 : 0; | ||
const next = list[nextIndex] || list[defaultIndex]; | ||
const { | ||
el: next | ||
} = list[nextIndex] || list[defaultIndex]; | ||
@@ -305,0 +313,0 @@ if (next.getAttribute("tabindex") === null) { |
{ | ||
"name": "@testing-library/user-event", | ||
"version": "8.0.2", | ||
"version": "8.0.3", | ||
"description": "Simulate user events for react-testing-library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -181,3 +181,9 @@ <div align="center"> | ||
> **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. | ||
> **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. | ||
@@ -232,18 +238,23 @@ ```jsx | ||
<tr> | ||
<td align="center"><a href="https://twitter.com/Gpx"><img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;" alt="Giorgio Polvara"/><br /><sub><b>Giorgio Polvara</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3AGpx" title="Bug reports">๐</a> <a href="https://github.com/testing-library/user-event/commits?author=Gpx" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/commits?author=Gpx" title="Documentation">๐</a> <a href="#ideas-Gpx" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#infra-Gpx" title="Infrastructure (Hosting, Build-Tools, etc)">๐</a> <a href="#review-Gpx" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/testing-library/user-event/commits?author=Gpx" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/weyert"><img src="https://avatars3.githubusercontent.com/u/7049?v=4" width="100px;" alt="Weyert de Boer"/><br /><sub><b>Weyert de Boer</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=weyert" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/commits?author=weyert" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/twhitbeck"><img src="https://avatars2.githubusercontent.com/u/762471?v=4" width="100px;" alt="Tim Whitbeck"/><br /><sub><b>Tim Whitbeck</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Atwhitbeck" title="Bug reports">๐</a> <a href="https://github.com/testing-library/user-event/commits?author=twhitbeck" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4" width="100px;" alt="Michaรซl De Boey"/><br /><sub><b>Michaรซl De Boey</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=MichaelDeBoey" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://github.com/michaellasky"><img src="https://avatars2.githubusercontent.com/u/6646599?v=4" width="100px;" alt="Michael Lasky"/><br /><sub><b>Michael Lasky</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=michaellasky" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/commits?author=michaellasky" title="Documentation">๐</a> <a href="#ideas-michaellasky" title="Ideas, Planning, & Feedback">๐ค</a></td> | ||
<td align="center"><a href="https://github.com/shomalgan"><img src="https://avatars0.githubusercontent.com/u/2883620?v=4" width="100px;" alt="Ahmad Esmaeilzadeh"/><br /><sub><b>Ahmad Esmaeilzadeh</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=shomalgan" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://calebeby.ml"><img src="https://avatars1.githubusercontent.com/u/13206945?v=4" width="100px;" alt="Caleb Eby"/><br /><sub><b>Caleb Eby</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=calebeby" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/issues?q=author%3Acalebeby" title="Bug reports">๐</a></td> | ||
<td align="center"><a href="https://twitter.com/Gpx"><img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;" alt=""/><br /><sub><b>Giorgio Polvara</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3AGpx" title="Bug reports">๐</a> <a href="https://github.com/testing-library/user-event/commits?author=Gpx" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/commits?author=Gpx" title="Documentation">๐</a> <a href="#ideas-Gpx" title="Ideas, Planning, & Feedback">๐ค</a> <a href="#infra-Gpx" title="Infrastructure (Hosting, Build-Tools, etc)">๐</a> <a href="https://github.com/testing-library/user-event/pulls?q=is%3Apr+reviewed-by%3AGpx" title="Reviewed Pull Requests">๐</a> <a href="https://github.com/testing-library/user-event/commits?author=Gpx" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/weyert"><img src="https://avatars3.githubusercontent.com/u/7049?v=4" width="100px;" alt=""/><br /><sub><b>Weyert de Boer</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=weyert" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/commits?author=weyert" title="Tests">โ ๏ธ</a></td> | ||
<td align="center"><a href="https://github.com/twhitbeck"><img src="https://avatars2.githubusercontent.com/u/762471?v=4" width="100px;" alt=""/><br /><sub><b>Tim Whitbeck</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Atwhitbeck" title="Bug reports">๐</a> <a href="https://github.com/testing-library/user-event/commits?author=twhitbeck" title="Code">๐ป</a></td> | ||
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4" width="100px;" alt=""/><br /><sub><b>Michaรซl De Boey</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=MichaelDeBoey" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://github.com/michaellasky"><img src="https://avatars2.githubusercontent.com/u/6646599?v=4" width="100px;" alt=""/><br /><sub><b>Michael Lasky</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=michaellasky" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/commits?author=michaellasky" title="Documentation">๐</a> <a href="#ideas-michaellasky" title="Ideas, Planning, & Feedback">๐ค</a></td> | ||
<td align="center"><a href="https://github.com/shomalgan"><img src="https://avatars0.githubusercontent.com/u/2883620?v=4" width="100px;" alt=""/><br /><sub><b>Ahmad Esmaeilzadeh</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=shomalgan" title="Documentation">๐</a></td> | ||
<td align="center"><a href="https://calebeby.ml"><img src="https://avatars1.githubusercontent.com/u/13206945?v=4" width="100px;" alt=""/><br /><sub><b>Caleb Eby</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=calebeby" title="Code">๐ป</a> <a href="https://github.com/testing-library/user-event/issues?q=author%3Acalebeby" title="Bug reports">๐</a></td> | ||
</tr> | ||
<tr> | ||
<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> | ||
<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> | ||
<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> | ||
<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> | ||
<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> | ||
<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> | ||
<td align="center"><a href="https://afontcu.dev"><img src="https://avatars0.githubusercontent.com/u/9197791?v=4" width="100px;" alt=""/><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> | ||
<td align="center"><a href="https://github.com/skywickenden"><img src="https://avatars2.githubusercontent.com/u/4930551?v=4" width="100px;" alt=""/><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> | ||
<td align="center"><a href="https://github.com/bogdanbodnar"><img src="https://avatars2.githubusercontent.com/u/9034868?v=4" width="100px;" alt=""/><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> | ||
<td align="center"><a href="https://zach.website"><img src="https://avatars0.githubusercontent.com/u/1699281?v=4" width="100px;" alt=""/><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> | ||
<td align="center"><a href="https://twitter.com/ryanastelly"><img src="https://avatars1.githubusercontent.com/u/4138357?v=4" width="100px;" alt=""/><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> | ||
<td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4" width="100px;" alt=""/><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> | ||
<td align="center"><a href="https://github.com/GentlemanHal"><img src="https://avatars2.githubusercontent.com/u/415521?v=4" width="100px;" alt=""/><br /><sub><b>Christopher Martin</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=GentlemanHal" title="Code">๐ป</a></td> | ||
</tr> | ||
<tr> | ||
<td align="center"><a href="http://fullgallop.me"><img src="https://avatars0.githubusercontent.com/u/32252769?v=4" width="100px;" alt=""/><br /><sub><b>Yuancheng Wu</b></sub></a><br /><a href="https://github.com/testing-library/user-event/pulls?q=is%3Apr+reviewed-by%3AYuanchengWu" title="Reviewed Pull Requests">๐</a></td> | ||
<td align="center"><a href="https://github.com/maheshjag"><img src="https://avatars0.githubusercontent.com/u/1705603?v=4" width="100px;" alt=""/><br /><sub><b>MJ</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=maheshjag" title="Documentation">๐</a></td> | ||
</tr> | ||
</table> | ||
@@ -253,3 +264,2 @@ | ||
<!-- prettier-ignore-end --> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
@@ -256,0 +266,0 @@ |
import { fireEvent } from "@testing-library/dom"; | ||
function wait(time) { | ||
return new Promise(function(resolve) { | ||
return new Promise(function (resolve) { | ||
setTimeout(() => resolve(), time); | ||
@@ -235,17 +235,20 @@ }); | ||
); | ||
const list = Array.prototype.filter | ||
.call(focusableElements, function(item) { | ||
return item.getAttribute("tabindex") !== "-1"; | ||
}) | ||
let list = Array.prototype.filter.call(focusableElements, function (item) { | ||
return item.getAttribute("tabindex") !== "-1"; | ||
}).map((el, idx) => ({ el, idx })) | ||
.sort((a, b) => { | ||
const tabIndexA = a.getAttribute("tabindex"); | ||
const tabIndexB = b.getAttribute("tabindex"); | ||
return tabIndexA < tabIndexB ? -1 : tabIndexA > tabIndexB ? 1 : 0; | ||
}); | ||
const index = list.indexOf(document.activeElement); | ||
const tabIndexA = a.el.getAttribute("tabindex"); | ||
const tabIndexB = b.el.getAttribute("tabindex"); | ||
const diff = tabIndexA - tabIndexB; | ||
return diff !== 0 ? diff : a.idx - b.idx; | ||
}) | ||
const index = list.findIndex(({ el }) => el === document.activeElement); | ||
let nextIndex = shift ? index - 1 : index + 1; | ||
let defaultIndex = shift ? list.length - 1 : 0; | ||
const next = list[nextIndex] || list[defaultIndex]; | ||
const { el: next } = (list[nextIndex] || list[defaultIndex]); | ||
@@ -252,0 +255,0 @@ if (next.getAttribute("tabindex") === null) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12861319
2134
267