Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tab function to docs and typings #184

Merged
merged 6 commits into from Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -167,6 +167,30 @@ expect(getByTestId("val3").selected).toBe(true);
The `values` parameter can be either an array of values or a singular scalar
value.

### `tab([options])`

Focuses elements in the correct tab order.

```jsx
import React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

test("tab", () => {
const { getByText, getByTestId } = render(
<div>
<label htmlFor="checkbox">Check</label>
<input id="checkbox" data-testid="checkbox" type="checkbox" />
</div>
);

userEvent.tab();
expect(getByTestId("checkbox")).toBe(document.activeElement);
});
```

If `options.shift` is `true`, `tab` will cycle backwards through the tab order.

## Contributors

Thanks goes to these wonderful people
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Expand Up @@ -15,6 +15,7 @@ declare const userEvent: {
text: string,
userOpts?: IUserOptions
) => Promise<void>;
tab: (userOpts?: { shift: boolean }) => void;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is no longer up to date. Could you add focusTrap to it?
better yet, define an interface and add defaults to the options (edit: can't have defaults in interface, oops)

interface TabUserOptions {
  shift?: boolean;
  focusTrap?: Document;
}

declare const userEvent: {
  ...
  tab: (userOpts?: TabUserOptions) => void;
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the type of focusTrap be TargetElement which is defined as type TargetElement = Element | Window;?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe it needs to be Document | Element?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at lib.dom.d.ts querySelectorAll is defined in ParentNode which Document and Element both extend, so Document | Element seems like the correct choice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you happy with the current state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, then I'm going to merge it

};

export default userEvent;