Skip to content

Commit

Permalink
feat(click): Add support for ctrl click. (#271)
Browse files Browse the repository at this point in the history
We pass the `init` argument through so that the underlying
click event can be created with ctrlKey set to true if desired.
  • Loading branch information
Raynos committed May 12, 2020
1 parent 2bb7def commit 87adb56
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -87,6 +87,15 @@ test("click", () => {
});
```

You can also ctrlClick / shiftClick etc with

```js
userEvent.click(elem, { ctrlKey: true, shiftKey: true })
```

See the [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent)
constructor documentation for more options.

### `dblClick(element)`

Clicks `element` twice, depending on what `element` is it can have different
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Expand Up @@ -41,7 +41,7 @@ function clickBooleanElement(element) {
fireEvent.click(element);
}

function clickElement(element, previousElement) {
function clickElement(element, previousElement, init) {
fireEvent.mouseOver(element);
fireEvent.mouseMove(element);
const continueDefaultHandling = fireEvent.mouseDown(element);
Expand All @@ -50,7 +50,7 @@ function clickElement(element, previousElement) {
element.focus();
}
fireEvent.mouseUp(element);
fireEvent.click(element);
fireEvent.click(element, init);

const labelAncestor = findTagInParents(element, "LABEL");
labelAncestor && clickLabel(labelAncestor);
Expand Down Expand Up @@ -132,7 +132,7 @@ function selectAll(element) {
}

const userEvent = {
click(element) {
click(element, init) {
const focusedElement = element.ownerDocument.activeElement;
const wasAnotherElementFocused =
focusedElement !== element.ownerDocument.body &&
Expand All @@ -152,7 +152,7 @@ const userEvent = {
break;
}
default:
clickElement(element, wasAnotherElementFocused && focusedElement);
clickElement(element, wasAnotherElementFocused && focusedElement, init);
}
},

Expand Down

0 comments on commit 87adb56

Please sign in to comment.